compiler-sfc.d.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. import { LazyResult } from 'postcss';
  2. import { ParserPlugin } from '@babel/parser';
  3. declare interface AssetURLOptions {
  4. [name: string]: string | string[];
  5. }
  6. declare type ASTAttr = {
  7. name: string;
  8. value: any;
  9. dynamic?: boolean;
  10. start?: number;
  11. end?: number;
  12. };
  13. declare type ASTDirective = {
  14. name: string;
  15. rawName: string;
  16. value: string;
  17. arg: string | null;
  18. isDynamicArg: boolean;
  19. modifiers: ASTModifiers | null;
  20. start?: number;
  21. end?: number;
  22. };
  23. declare type ASTElement = {
  24. type: 1;
  25. tag: string;
  26. attrsList: Array<ASTAttr>;
  27. attrsMap: {
  28. [key: string]: any;
  29. };
  30. rawAttrsMap: {
  31. [key: string]: ASTAttr;
  32. };
  33. parent: ASTElement | void;
  34. children: Array<ASTNode>;
  35. start?: number;
  36. end?: number;
  37. processed?: true;
  38. static?: boolean;
  39. staticRoot?: boolean;
  40. staticInFor?: boolean;
  41. staticProcessed?: boolean;
  42. hasBindings?: boolean;
  43. text?: string;
  44. attrs?: Array<ASTAttr>;
  45. dynamicAttrs?: Array<ASTAttr>;
  46. props?: Array<ASTAttr>;
  47. plain?: boolean;
  48. pre?: true;
  49. ns?: string;
  50. component?: string;
  51. inlineTemplate?: true;
  52. transitionMode?: string | null;
  53. slotName?: string | null;
  54. slotTarget?: string | null;
  55. slotTargetDynamic?: boolean;
  56. slotScope?: string | null;
  57. scopedSlots?: {
  58. [name: string]: ASTElement;
  59. };
  60. ref?: string;
  61. refInFor?: boolean;
  62. if?: string;
  63. ifProcessed?: boolean;
  64. elseif?: string;
  65. else?: true;
  66. ifConditions?: ASTIfConditions;
  67. for?: string;
  68. forProcessed?: boolean;
  69. key?: string;
  70. alias?: string;
  71. iterator1?: string;
  72. iterator2?: string;
  73. staticClass?: string;
  74. classBinding?: string;
  75. staticStyle?: string;
  76. styleBinding?: string;
  77. events?: ASTElementHandlers;
  78. nativeEvents?: ASTElementHandlers;
  79. transition?: string | true;
  80. transitionOnAppear?: boolean;
  81. model?: {
  82. value: string;
  83. callback: string;
  84. expression: string;
  85. };
  86. directives?: Array<ASTDirective>;
  87. forbidden?: true;
  88. once?: true;
  89. onceProcessed?: boolean;
  90. wrapData?: (code: string) => string;
  91. wrapListeners?: (code: string) => string;
  92. ssrOptimizability?: number;
  93. };
  94. declare type ASTElementHandler = {
  95. value: string;
  96. params?: Array<any>;
  97. modifiers: ASTModifiers | null;
  98. dynamic?: boolean;
  99. start?: number;
  100. end?: number;
  101. };
  102. declare type ASTElementHandlers = {
  103. [key: string]: ASTElementHandler | Array<ASTElementHandler>;
  104. };
  105. declare type ASTExpression = {
  106. type: 2;
  107. expression: string;
  108. text: string;
  109. tokens: Array<string | Object>;
  110. static?: boolean;
  111. ssrOptimizability?: number;
  112. start?: number;
  113. end?: number;
  114. };
  115. declare type ASTIfCondition = {
  116. exp: string | null;
  117. block: ASTElement;
  118. };
  119. declare type ASTIfConditions = Array<ASTIfCondition>;
  120. declare type ASTModifiers = {
  121. [key: string]: boolean;
  122. };
  123. declare type ASTNode = ASTElement | ASTText | ASTExpression;
  124. declare type ASTText = {
  125. type: 3;
  126. text: string;
  127. static?: boolean;
  128. isComment?: boolean;
  129. ssrOptimizability?: number;
  130. start?: number;
  131. end?: number;
  132. };
  133. declare type BindingMetadata = {
  134. [key: string]: BindingTypes | undefined;
  135. } & {
  136. __isScriptSetup?: boolean;
  137. };
  138. declare const enum BindingTypes {
  139. /**
  140. * returned from data()
  141. */
  142. DATA = "data",
  143. /**
  144. * declared as a prop
  145. */
  146. PROPS = "props",
  147. /**
  148. * a local alias of a `<script setup>` destructured prop.
  149. * the original is stored in __propsAliases of the bindingMetadata object.
  150. */
  151. PROPS_ALIASED = "props-aliased",
  152. /**
  153. * a let binding (may or may not be a ref)
  154. */
  155. SETUP_LET = "setup-let",
  156. /**
  157. * a const binding that can never be a ref.
  158. * these bindings don't need `unref()` calls when processed in inlined
  159. * template expressions.
  160. */
  161. SETUP_CONST = "setup-const",
  162. /**
  163. * a const binding that does not need `unref()`, but may be mutated.
  164. */
  165. SETUP_REACTIVE_CONST = "setup-reactive-const",
  166. /**
  167. * a const binding that may be a ref.
  168. */
  169. SETUP_MAYBE_REF = "setup-maybe-ref",
  170. /**
  171. * bindings that are guaranteed to be refs
  172. */
  173. SETUP_REF = "setup-ref",
  174. /**
  175. * declared by other options, e.g. computed, inject
  176. */
  177. OPTIONS = "options"
  178. }
  179. declare type CompiledResult = {
  180. ast: ASTElement | null;
  181. render: string;
  182. staticRenderFns: Array<string>;
  183. stringRenderFns?: Array<string>;
  184. errors?: Array<string | WarningMessage>;
  185. tips?: Array<string | WarningMessage>;
  186. };
  187. export declare type CompilerOptions = {
  188. warn?: Function;
  189. modules?: Array<ModuleOptions>;
  190. directives?: {
  191. [key: string]: Function;
  192. };
  193. staticKeys?: string;
  194. isUnaryTag?: (tag: string) => boolean | undefined;
  195. canBeLeftOpenTag?: (tag: string) => boolean | undefined;
  196. isReservedTag?: (tag: string) => boolean | undefined;
  197. preserveWhitespace?: boolean;
  198. whitespace?: 'preserve' | 'condense';
  199. optimize?: boolean;
  200. mustUseProp?: (tag: string, type: string | null, name: string) => boolean;
  201. isPreTag?: (attr: string) => boolean | null;
  202. getTagNamespace?: (tag: string) => string | undefined;
  203. expectHTML?: boolean;
  204. isFromDOM?: boolean;
  205. shouldDecodeTags?: boolean;
  206. shouldDecodeNewlines?: boolean;
  207. shouldDecodeNewlinesForHref?: boolean;
  208. outputSourceRange?: boolean;
  209. shouldKeepComment?: boolean;
  210. delimiters?: [string, string];
  211. comments?: boolean;
  212. scopeId?: string;
  213. bindings?: BindingMetadata;
  214. };
  215. /**
  216. * Compile `<script setup>`
  217. * It requires the whole SFC descriptor because we need to handle and merge
  218. * normal `<script>` + `<script setup>` if both are present.
  219. */
  220. export declare function compileScript(sfc: SFCDescriptor, options?: SFCScriptCompileOptions): SFCScriptBlock;
  221. export declare function compileStyle(options: SFCStyleCompileOptions): SFCStyleCompileResults;
  222. export declare function compileStyleAsync(options: SFCStyleCompileOptions): Promise<SFCStyleCompileResults>;
  223. export declare function compileTemplate(options: SFCTemplateCompileOptions): SFCTemplateCompileResults;
  224. export declare function generateCodeFrame(source: string, start?: number, end?: number): string;
  225. declare interface ImportBinding {
  226. isType: boolean;
  227. imported: string;
  228. source: string;
  229. isFromSetup: boolean;
  230. isUsedInTemplate: boolean;
  231. }
  232. declare type ModuleOptions = {
  233. preTransformNode?: (el: ASTElement) => ASTElement | null | void;
  234. transformNode?: (el: ASTElement) => ASTElement | null | void;
  235. postTransformNode?: (el: ASTElement) => void;
  236. genData?: (el: ASTElement) => string;
  237. transformCode?: (el: ASTElement, code: string) => string;
  238. staticKeys?: Array<string>;
  239. };
  240. export declare function parse(options: SFCParseOptions): SFCDescriptor;
  241. /**
  242. * Parse a single-file component (*.vue) file into an SFC Descriptor Object.
  243. */
  244. export declare function parseComponent(source: string, options?: VueTemplateCompilerParseOptions): SFCDescriptor;
  245. declare interface RawSourceMap extends StartOfSourceMap {
  246. version: string;
  247. sources: string[];
  248. names: string[];
  249. sourcesContent?: string[];
  250. mappings: string;
  251. }
  252. /**
  253. * Utility for rewriting `export default` in a script block into a variable
  254. * declaration so that we can inject things into it
  255. */
  256. export declare function rewriteDefault(input: string, as: string, parserPlugins?: ParserPlugin[]): string;
  257. export declare interface SFCBlock extends SFCCustomBlock {
  258. lang?: string;
  259. scoped?: boolean;
  260. module?: string | boolean;
  261. }
  262. export declare interface SFCCustomBlock {
  263. type: string;
  264. content: string;
  265. attrs: {
  266. [key: string]: string | true;
  267. };
  268. start: number;
  269. end: number;
  270. src?: string;
  271. map?: RawSourceMap;
  272. }
  273. export declare interface SFCDescriptor {
  274. source: string;
  275. filename: string;
  276. template: SFCBlock | null;
  277. script: SFCScriptBlock | null;
  278. scriptSetup: SFCScriptBlock | null;
  279. styles: SFCBlock[];
  280. customBlocks: SFCCustomBlock[];
  281. cssVars: string[];
  282. errors: (string | WarningMessage)[];
  283. /**
  284. * compare with an existing descriptor to determine whether HMR should perform
  285. * a reload vs. re-render.
  286. *
  287. * Note: this comparison assumes the prev/next script are already identical,
  288. * and only checks the special case where `<script setup lang="ts">` unused
  289. * import pruning result changes due to template changes.
  290. */
  291. shouldForceReload: (prevImports: Record<string, ImportBinding>) => boolean;
  292. }
  293. export declare interface SFCParseOptions {
  294. source: string;
  295. filename?: string;
  296. compiler?: TemplateCompiler;
  297. compilerParseOptions?: VueTemplateCompilerParseOptions;
  298. sourceRoot?: string;
  299. sourceMap?: boolean;
  300. /**
  301. * @deprecated use `sourceMap` instead.
  302. */
  303. needMap?: boolean;
  304. }
  305. export declare interface SFCScriptBlock extends SFCBlock {
  306. type: 'script';
  307. setup?: string | boolean;
  308. bindings?: BindingMetadata;
  309. imports?: Record<string, ImportBinding>;
  310. /**
  311. * import('\@babel/types').Statement
  312. */
  313. scriptAst?: any[];
  314. /**
  315. * import('\@babel/types').Statement
  316. */
  317. scriptSetupAst?: any[];
  318. }
  319. export declare interface SFCScriptCompileOptions {
  320. /**
  321. * Scope ID for prefixing injected CSS variables.
  322. * This must be consistent with the `id` passed to `compileStyle`.
  323. */
  324. id: string;
  325. /**
  326. * Production mode. Used to determine whether to generate hashed CSS variables
  327. */
  328. isProd?: boolean;
  329. /**
  330. * Enable/disable source map. Defaults to true.
  331. */
  332. sourceMap?: boolean;
  333. /**
  334. * https://babeljs.io/docs/en/babel-parser#plugins
  335. */
  336. babelParserPlugins?: ParserPlugin[];
  337. }
  338. export declare interface SFCStyleCompileOptions {
  339. source: string;
  340. filename: string;
  341. id: string;
  342. map?: any;
  343. scoped?: boolean;
  344. trim?: boolean;
  345. preprocessLang?: string;
  346. preprocessOptions?: any;
  347. postcssOptions?: any;
  348. postcssPlugins?: any[];
  349. isProd?: boolean;
  350. }
  351. export declare interface SFCStyleCompileResults {
  352. code: string;
  353. map: any | void;
  354. rawResult: LazyResult | void;
  355. errors: string[];
  356. }
  357. export declare interface SFCTemplateCompileOptions {
  358. source: string;
  359. filename: string;
  360. compiler?: TemplateCompiler;
  361. compilerOptions?: CompilerOptions;
  362. transformAssetUrls?: AssetURLOptions | boolean;
  363. transformAssetUrlsOptions?: TransformAssetUrlsOptions;
  364. preprocessLang?: string;
  365. preprocessOptions?: any;
  366. transpileOptions?: any;
  367. isProduction?: boolean;
  368. isFunctional?: boolean;
  369. optimizeSSR?: boolean;
  370. prettify?: boolean;
  371. isTS?: boolean;
  372. bindings?: BindingMetadata;
  373. }
  374. export declare interface SFCTemplateCompileResults {
  375. ast: Object | undefined;
  376. code: string;
  377. source: string;
  378. tips: (string | WarningMessage)[];
  379. errors: (string | WarningMessage)[];
  380. }
  381. declare interface StartOfSourceMap {
  382. file?: string;
  383. sourceRoot?: string;
  384. }
  385. export declare interface TemplateCompiler {
  386. parseComponent(source: string, options?: any): SFCDescriptor;
  387. compile(template: string, options: CompilerOptions): CompiledResult;
  388. ssrCompile(template: string, options: CompilerOptions): CompiledResult;
  389. }
  390. declare interface TransformAssetUrlsOptions {
  391. /**
  392. * If base is provided, instead of transforming relative asset urls into
  393. * imports, they will be directly rewritten to absolute urls.
  394. */
  395. base?: string;
  396. /**
  397. * If true, also processes absolute urls.
  398. */
  399. includeAbsolute?: boolean;
  400. }
  401. declare interface VueTemplateCompilerParseOptions {
  402. pad?: 'line' | 'space' | boolean;
  403. deindent?: boolean;
  404. outputSourceRange?: boolean;
  405. }
  406. export declare type WarningMessage = {
  407. msg: string;
  408. start?: number;
  409. end?: number;
  410. };
  411. export { }