compiler-sfc.d.ts 6.7 KB

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