{"id":15062031,"url":"https://github.com/wanwu/san-sfc-compiler","last_synced_at":"2025-10-14T22:02:53.200Z","repository":{"id":42576875,"uuid":"422176798","full_name":"wanwu/san-sfc-compiler","owner":"wanwu","description":"Lower level utilities for compiling `.san` single file components","archived":false,"fork":false,"pushed_at":"2022-12-07T23:31:05.000Z","size":908,"stargazers_count":1,"open_issues_count":4,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-03-15T10:11:52.606Z","etag":null,"topics":["less","pug","san","sass","sfc","ts"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wanwu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-10-28T11:25:58.000Z","updated_at":"2022-04-21T03:46:26.000Z","dependencies_parsed_at":"2023-01-25T00:00:34.218Z","dependency_job_id":null,"html_url":"https://github.com/wanwu/san-sfc-compiler","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wanwu%2Fsan-sfc-compiler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wanwu%2Fsan-sfc-compiler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wanwu%2Fsan-sfc-compiler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wanwu%2Fsan-sfc-compiler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wanwu","download_url":"https://codeload.github.com/wanwu/san-sfc-compiler/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247292901,"owners_count":20915135,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["less","pug","san","sass","sfc","ts"],"created_at":"2024-09-24T23:29:23.058Z","updated_at":"2025-10-14T22:02:48.169Z","avatar_url":"https://github.com/wanwu.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# san-sfc-compiler\n\n\u003e Lower level utilities for compiling `.san` single file components\n\nfork from [vuejs/component-compiler-utils](https://github.com/vuejs/component-compiler-utils)\n\n## API\n\n### `parseSFC`\n\n描述：解析 .san 文件源码\n\n使用：\n\n```ts\nparseSFC({\n  source: '\u003cdiv\u003e\u003cp\u003e{{ helloworld }}\u003c/p\u003e\u003c/div\u003e',\n  filename: 'test.san',\n});\n\n// 选项\nexport interface ParseOptions {\n  source: string;\n  filename?: string;\n  sourceRoot?: string;\n  needMap?: boolean;\n}\n```\n\n方法 / 属性：\n\n| 方法 / 属性 | 描述                          | 类型    |\n| ----------- | ----------------------------- | ------- |\n| source      | 源码                          | string  |\n| filename    | 文件名                        | string  |\n| sourceRoot  | sourcemap 选项                | string  |\n| needMap     | sourcemap 开关（template 没有 | boolean |\n\n返回：\n\n```ts\nexport interface SFCDescriptor {\n  template: SFCBlock | null;\n  script: SFCBlock | null;\n  styles: SFCBlock[];\n  customBlocks: SFCBlock[];\n}\n```\n\n其中 SFCBlock 类型为这样：\n\n```ts\n{\n    type!: string;\n    content!: string;\n    attrs!: Record\u003cstring, string | true\u003e;\n    start!: number;\n    end!: number;\n    lang?: string;\n    src?: string;\n    scoped?: true;\n    module?: string | true;\n}\n```\n\ncontent 就是标签内容了。\n\n### `compileTemplate`\n\n描述：编译模板代码，即 `\u003ctemplate\u003e` 标签中包含的部分\n\n使用：\n\n```ts\nconst result = compileTemplate({\n  source: '\u003cdiv\u003e\u003cimg src=\"../images/profile.png\" /\u003e\u003c/div\u003e',\n  filename: 'example.san',\n  transformAssetUrls: true,\n});\n```\n\n方法 / 属性：\n\n| 方法 / 属性        | 描述                         | 类型                   |\n| ------------------ | ---------------------------- | ---------------------- |\n| source             | 源码                         | string                 |\n| filename           | 文件名                       | string                 |\n| id                 | 一般用在 scoped css 时       | string                 |\n| scoped             | scoped css 开关              | boolean                |\n| transformAssetUrls | 将 url 转换为 require        | boolean                |\n| compileANode       | 编译为 anode / apack         | 'aNode'/'aPack'/'none' |\n| preprocessLang     | 预处理语言，pugjs 等         | string                 |\n| preprocessOptions  | 预处理的选项，传递给预处理器 | object                 |\n\n返回：\n\n```ts\n{\n    code: '\u003cdiv\u003e\u003cimg src=\"require(../images/profile.png);\"\u003e\u003c/div\u003e',\n    source: '\u003cdiv\u003e\u003cimg src=\"../images/profile.png\" /\u003e\u003c/div\u003e'\n}\n```\n\n### `compileStyle / compileStyleAsync`\n\n描述：编译样式代码，即 `\u003cstyle\u003e` 标签中包含的部分\n\n使用：\n\n```ts\nconst style = parseSFC({\n  source: `\u003cstyle lang=\"less\"\u003e@red: rgb(255, 0, 0);\\n.color { color: @red; }\\n\u003c/style\u003e`,\n  filename: 'test.san',\n}).styles[0] as any;\n\nconst result = compileStyle({\n  id: 'test',\n  filename: 'test.san',\n  source: style.content,\n  scoped: true,\n  map: style.map,\n  preprocessLang: style.lang,\n});\n```\n\n方法 / 属性：\n\n| 方法 / 属性       | 描述                                    | 类型    |\n| ----------------- | --------------------------------------- | ------- |\n| source            | 源码                                    | string  |\n| filename          | 文件名                                  | string  |\n| id                | 添加 scoped css id                      | string  |\n| scoped            | scoped css 开关                         | boolean |\n| modules           | css modules 开关                        | boolean |\n| mute              | 去除 postcss 控制台信息，默认为 true    | boolean |\n| preprocessLang    | 预处理语言，less sass 等                | string  |\n| preprocessOptions | 预处理的选项，传递给预处理器            | object  |\n| postcssOptions    | postcss 的选项，直接传递给它            | object  |\n| postcssPlugins    | postcss 的插件，直接传递给它            | object  |\n| map               | sourcemap 的 map，如 less =\u003e css 的映射 | object  |\n\n返回：\n\n```ts\n{\n  code: '.color[test] {\\n  color: #ff0000;\\n}\\n' // 转换后的代码,\n  source: '.color {\\n  color: #ff0000;\\n}\\n' // 源码,\n  rawResult: LazyResult // postcss 异步结果,\n  map: outMap \u0026\u0026 outMap.toJSON() // 经过两步转换的 sourcemap,\n  errors,\n  cssHashMap: {} // css modules 编译结果\n}\n```\n\n### `compileScript`\n\n描述：直接使用 ts 的 transpile 方法\n\n```ts\nexport const compileScript = (source: string, config: CompilerOptions) =\u003e {\n  return typescrtipt.transpile(source, config);\n};\n```\n\n如果不清楚，可以参考 test 目录下的测试文件\n\n## LICENSE\n\nCopyright (c) Baidu Inc. All rights reserved.\n\nThis source code is licensed under the MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwanwu%2Fsan-sfc-compiler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwanwu%2Fsan-sfc-compiler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwanwu%2Fsan-sfc-compiler/lists"}