compiler-sfc.d.ts 9.5 KB

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