compiler-sfc.d.ts 8.4 KB

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