compiler-sfc.d.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. import { parse as babelParse } from '@babel/parser';
  2. import { BindingMetadata } from '@vue/compiler-core';
  3. import { CodegenResult } from '@vue/compiler-core';
  4. import { CompilerError } from '@vue/compiler-core';
  5. import { CompilerOptions } from '@vue/compiler-core';
  6. import { ElementNode } from '@vue/compiler-core';
  7. import { extractIdentifiers } from '@vue/compiler-core';
  8. import { generateCodeFrame } from '@vue/compiler-core';
  9. import { isInDestructureAssignment } from '@vue/compiler-core';
  10. import { isStaticProperty } from '@vue/compiler-core';
  11. import { LazyResult } from 'postcss';
  12. import MagicString from 'magic-string';
  13. import { ParserOptions } from '@vue/compiler-core';
  14. import { ParserPlugin } from '@babel/parser';
  15. import { RawSourceMap } from 'source-map';
  16. import { Result } from 'postcss';
  17. import { RootNode } from '@vue/compiler-core';
  18. import { shouldTransform as shouldTransformRef } from '@vue/reactivity-transform';
  19. import { SourceLocation } from '@vue/compiler-core';
  20. import { Statement } from '@babel/types';
  21. import { transform as transformRef } from '@vue/reactivity-transform';
  22. import { transformAST as transformRefAST } from '@vue/reactivity-transform';
  23. import { walkIdentifiers } from '@vue/compiler-core';
  24. export declare interface AssetURLOptions {
  25. /**
  26. * If base is provided, instead of transforming relative asset urls into
  27. * imports, they will be directly rewritten to absolute urls.
  28. */
  29. base?: string | null;
  30. /**
  31. * If true, also processes absolute urls.
  32. */
  33. includeAbsolute?: boolean;
  34. tags?: AssetURLTagConfig;
  35. }
  36. export declare interface AssetURLTagConfig {
  37. [name: string]: string[];
  38. }
  39. export { babelParse }
  40. export { BindingMetadata }
  41. export { CompilerError }
  42. export { CompilerOptions }
  43. /**
  44. * Compile `<script setup>`
  45. * It requires the whole SFC descriptor because we need to handle and merge
  46. * normal `<script>` + `<script setup>` if both are present.
  47. */
  48. export declare function compileScript(sfc: SFCDescriptor, options: SFCScriptCompileOptions): SFCScriptBlock;
  49. export declare function compileStyle(options: SFCStyleCompileOptions): SFCStyleCompileResults;
  50. export declare function compileStyleAsync(options: SFCAsyncStyleCompileOptions): Promise<SFCStyleCompileResults>;
  51. export declare function compileTemplate(options: SFCTemplateCompileOptions): SFCTemplateCompileResults;
  52. /**
  53. * Aligns with postcss-modules
  54. * https://github.com/css-modules/postcss-modules
  55. */
  56. declare interface CSSModulesOptions {
  57. scopeBehaviour?: 'global' | 'local';
  58. generateScopedName?: string | ((name: string, filename: string, css: string) => string);
  59. hashPrefix?: string;
  60. localsConvention?: 'camelCase' | 'camelCaseOnly' | 'dashes' | 'dashesOnly';
  61. exportGlobals?: boolean;
  62. globalModulePaths?: RegExp[];
  63. }
  64. export { extractIdentifiers }
  65. export { generateCodeFrame }
  66. declare interface ImportBinding {
  67. isType: boolean;
  68. imported: string;
  69. local: string;
  70. source: string;
  71. isFromSetup: boolean;
  72. isUsedInTemplate: boolean;
  73. }
  74. export { isInDestructureAssignment }
  75. export { isStaticProperty }
  76. export { MagicString }
  77. export declare function parse(source: string, { sourceMap, filename, sourceRoot, pad, ignoreEmpty, compiler }?: SFCParseOptions): SFCParseResult;
  78. declare type PreprocessLang = 'less' | 'sass' | 'scss' | 'styl' | 'stylus';
  79. /**
  80. * Utility for rewriting `export default` in a script block into a variable
  81. * declaration so that we can inject things into it
  82. */
  83. export declare function rewriteDefault(input: string, as: string, parserPlugins?: ParserPlugin[]): string;
  84. export declare interface SFCAsyncStyleCompileOptions extends SFCStyleCompileOptions {
  85. isAsync?: boolean;
  86. modules?: boolean;
  87. modulesOptions?: CSSModulesOptions;
  88. }
  89. export declare interface SFCBlock {
  90. type: string;
  91. content: string;
  92. attrs: Record<string, string | true>;
  93. loc: SourceLocation;
  94. map?: RawSourceMap;
  95. lang?: string;
  96. src?: string;
  97. }
  98. export declare interface SFCDescriptor {
  99. filename: string;
  100. source: string;
  101. template: SFCTemplateBlock | null;
  102. script: SFCScriptBlock | null;
  103. scriptSetup: SFCScriptBlock | null;
  104. styles: SFCStyleBlock[];
  105. customBlocks: SFCBlock[];
  106. cssVars: string[];
  107. /**
  108. * whether the SFC uses :slotted() modifier.
  109. * this is used as a compiler optimization hint.
  110. */
  111. slotted: boolean;
  112. /**
  113. * compare with an existing descriptor to determine whether HMR should perform
  114. * a reload vs. re-render.
  115. *
  116. * Note: this comparison assumes the prev/next script are already identical,
  117. * and only checks the special case where <script setup lang="ts"> unused import
  118. * pruning result changes due to template changes.
  119. */
  120. shouldForceReload: (prevImports: Record<string, ImportBinding>) => boolean;
  121. }
  122. export declare interface SFCParseOptions {
  123. filename?: string;
  124. sourceMap?: boolean;
  125. sourceRoot?: string;
  126. pad?: boolean | 'line' | 'space';
  127. ignoreEmpty?: boolean;
  128. compiler?: TemplateCompiler;
  129. }
  130. export declare interface SFCParseResult {
  131. descriptor: SFCDescriptor;
  132. errors: (CompilerError | SyntaxError)[];
  133. }
  134. export declare interface SFCScriptBlock extends SFCBlock {
  135. type: 'script';
  136. setup?: string | boolean;
  137. bindings?: BindingMetadata;
  138. imports?: Record<string, ImportBinding>;
  139. scriptAst?: Statement[];
  140. scriptSetupAst?: Statement[];
  141. }
  142. export declare interface SFCScriptCompileOptions {
  143. /**
  144. * Scope ID for prefixing injected CSS variables.
  145. * This must be consistent with the `id` passed to `compileStyle`.
  146. */
  147. id: string;
  148. /**
  149. * Production mode. Used to determine whether to generate hashed CSS variables
  150. */
  151. isProd?: boolean;
  152. /**
  153. * Enable/disable source map. Defaults to true.
  154. */
  155. sourceMap?: boolean;
  156. /**
  157. * https://babeljs.io/docs/en/babel-parser#plugins
  158. */
  159. babelParserPlugins?: ParserPlugin[];
  160. /**
  161. * (Experimental) Enable syntax transform for using refs without `.value` and
  162. * using destructured props with reactivity
  163. */
  164. reactivityTransform?: boolean;
  165. /**
  166. * (Experimental) Enable syntax transform for using refs without `.value`
  167. * https://github.com/vuejs/rfcs/discussions/369
  168. * @deprecated now part of `reactivityTransform`
  169. * @default false
  170. */
  171. refTransform?: boolean;
  172. /**
  173. * (Experimental) Enable syntax transform for destructuring from defineProps()
  174. * https://github.com/vuejs/rfcs/discussions/394
  175. * @deprecated now part of `reactivityTransform`
  176. * @default false
  177. */
  178. propsDestructureTransform?: boolean;
  179. /**
  180. * @deprecated use `reactivityTransform` instead.
  181. */
  182. refSugar?: boolean;
  183. /**
  184. * Compile the template and inline the resulting render function
  185. * directly inside setup().
  186. * - Only affects `<script setup>`
  187. * - This should only be used in production because it prevents the template
  188. * from being hot-reloaded separately from component state.
  189. */
  190. inlineTemplate?: boolean;
  191. /**
  192. * Options for template compilation when inlining. Note these are options that
  193. * would normally be passed to `compiler-sfc`'s own `compileTemplate()`, not
  194. * options passed to `compiler-dom`.
  195. */
  196. templateOptions?: Partial<SFCTemplateCompileOptions>;
  197. }
  198. export declare interface SFCStyleBlock extends SFCBlock {
  199. type: 'style';
  200. scoped?: boolean;
  201. module?: string | boolean;
  202. }
  203. export declare interface SFCStyleCompileOptions {
  204. source: string;
  205. filename: string;
  206. id: string;
  207. scoped?: boolean;
  208. trim?: boolean;
  209. isProd?: boolean;
  210. inMap?: RawSourceMap;
  211. preprocessLang?: PreprocessLang;
  212. preprocessOptions?: any;
  213. preprocessCustomRequire?: (id: string) => any;
  214. postcssOptions?: any;
  215. postcssPlugins?: any[];
  216. /**
  217. * @deprecated use `inMap` instead.
  218. */
  219. map?: RawSourceMap;
  220. }
  221. export declare interface SFCStyleCompileResults {
  222. code: string;
  223. map: RawSourceMap | undefined;
  224. rawResult: Result | LazyResult | undefined;
  225. errors: Error[];
  226. modules?: Record<string, string>;
  227. dependencies: Set<string>;
  228. }
  229. export declare interface SFCTemplateBlock extends SFCBlock {
  230. type: 'template';
  231. ast: ElementNode;
  232. }
  233. export declare interface SFCTemplateCompileOptions {
  234. source: string;
  235. filename: string;
  236. id: string;
  237. scoped?: boolean;
  238. slotted?: boolean;
  239. isProd?: boolean;
  240. ssr?: boolean;
  241. ssrCssVars?: string[];
  242. inMap?: RawSourceMap;
  243. compiler?: TemplateCompiler;
  244. compilerOptions?: CompilerOptions;
  245. preprocessLang?: string;
  246. preprocessOptions?: any;
  247. /**
  248. * In some cases, compiler-sfc may not be inside the project root (e.g. when
  249. * linked or globally installed). In such cases a custom `require` can be
  250. * passed to correctly resolve the preprocessors.
  251. */
  252. preprocessCustomRequire?: (id: string) => any;
  253. /**
  254. * Configure what tags/attributes to transform into asset url imports,
  255. * or disable the transform altogether with `false`.
  256. */
  257. transformAssetUrls?: AssetURLOptions | AssetURLTagConfig | boolean;
  258. }
  259. export declare interface SFCTemplateCompileResults {
  260. code: string;
  261. ast?: RootNode;
  262. preamble?: string;
  263. source: string;
  264. tips: string[];
  265. errors: (string | CompilerError)[];
  266. map?: RawSourceMap;
  267. }
  268. export { shouldTransformRef }
  269. export declare interface TemplateCompiler {
  270. compile(template: string, options: CompilerOptions): CodegenResult;
  271. parse(template: string, options: ParserOptions): RootNode;
  272. }
  273. export { transformRef }
  274. export { transformRefAST }
  275. export declare const walk: any;
  276. export { walkIdentifiers }
  277. export { }