compiler-sfc.d.ts 6.9 KB

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