options.ts 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. import type {
  2. ElementNode,
  3. Namespace,
  4. Namespaces,
  5. ParentNode,
  6. TemplateChildNode,
  7. } from './ast'
  8. import type { CompilerError } from './errors'
  9. import type {
  10. DirectiveTransform,
  11. NodeTransform,
  12. TransformContext,
  13. } from './transform'
  14. import type { CompilerCompatOptions } from './compat/compatConfig'
  15. import type { ParserPlugin } from '@babel/parser'
  16. export interface ErrorHandlingOptions {
  17. onWarn?: (warning: CompilerError) => void
  18. onError?: (error: CompilerError) => void
  19. }
  20. export interface ParserOptions
  21. extends ErrorHandlingOptions,
  22. CompilerCompatOptions {
  23. /**
  24. * Base mode is platform agnostic and only parses HTML-like template syntax,
  25. * treating all tags the same way. Specific tag parsing behavior can be
  26. * configured by higher-level compilers.
  27. *
  28. * HTML mode adds additional logic for handling special parsing behavior in
  29. * `<script>`, `<style>`,`<title>` and `<textarea>`.
  30. * The logic is handled inside compiler-core for efficiency.
  31. *
  32. * SFC mode treats content of all root-level tags except `<template>` as plain
  33. * text.
  34. */
  35. parseMode?: 'base' | 'html' | 'sfc'
  36. /**
  37. * Specify the root namespace to use when parsing a template.
  38. * Defaults to `Namespaces.HTML` (0).
  39. */
  40. ns?: Namespaces
  41. /**
  42. * e.g. platform native elements, e.g. `<div>` for browsers
  43. */
  44. isNativeTag?: (tag: string) => boolean
  45. /**
  46. * e.g. native elements that can self-close, e.g. `<img>`, `<br>`, `<hr>`
  47. */
  48. isVoidTag?: (tag: string) => boolean
  49. /**
  50. * e.g. elements that should preserve whitespace inside, e.g. `<pre>`
  51. */
  52. isPreTag?: (tag: string) => boolean
  53. /**
  54. * Platform-specific built-in components e.g. `<Transition>`
  55. */
  56. isBuiltInComponent?: (tag: string) => symbol | void
  57. /**
  58. * Separate option for end users to extend the native elements list
  59. */
  60. isCustomElement?: (tag: string) => boolean | void
  61. /**
  62. * Get tag namespace
  63. */
  64. getNamespace?: (
  65. tag: string,
  66. parent: ElementNode | undefined,
  67. rootNamespace: Namespace,
  68. ) => Namespace
  69. /**
  70. * @default ['{{', '}}']
  71. */
  72. delimiters?: [string, string]
  73. /**
  74. * Whitespace handling strategy
  75. * @default 'condense'
  76. */
  77. whitespace?: 'preserve' | 'condense'
  78. /**
  79. * Only used for DOM compilers that runs in the browser.
  80. * In non-browser builds, this option is ignored.
  81. */
  82. decodeEntities?: (rawText: string, asAttr: boolean) => string
  83. /**
  84. * Whether to keep comments in the templates AST.
  85. * This defaults to `true` in development and `false` in production builds.
  86. */
  87. comments?: boolean
  88. /**
  89. * Parse JavaScript expressions with Babel.
  90. * @default false
  91. */
  92. prefixIdentifiers?: boolean
  93. /**
  94. * A list of parser plugins to enable for `@babel/parser`, which is used to
  95. * parse expressions in bindings and interpolations.
  96. * https://babeljs.io/docs/en/next/babel-parser#plugins
  97. */
  98. expressionPlugins?: ParserPlugin[]
  99. }
  100. export type HoistTransform = (
  101. children: TemplateChildNode[],
  102. context: TransformContext,
  103. parent: ParentNode,
  104. ) => void
  105. export enum BindingTypes {
  106. /**
  107. * returned from data()
  108. */
  109. DATA = 'data',
  110. /**
  111. * declared as a prop
  112. */
  113. PROPS = 'props',
  114. /**
  115. * a local alias of a `<script setup>` destructured prop.
  116. * the original is stored in __propsAliases of the bindingMetadata object.
  117. */
  118. PROPS_ALIASED = 'props-aliased',
  119. /**
  120. * a let binding (may or may not be a ref)
  121. */
  122. SETUP_LET = 'setup-let',
  123. /**
  124. * a const binding that can never be a ref.
  125. * these bindings don't need `unref()` calls when processed in inlined
  126. * template expressions.
  127. */
  128. SETUP_CONST = 'setup-const',
  129. /**
  130. * a const binding that does not need `unref()`, but may be mutated.
  131. */
  132. SETUP_REACTIVE_CONST = 'setup-reactive-const',
  133. /**
  134. * a const binding that may be a ref.
  135. */
  136. SETUP_MAYBE_REF = 'setup-maybe-ref',
  137. /**
  138. * bindings that are guaranteed to be refs
  139. */
  140. SETUP_REF = 'setup-ref',
  141. /**
  142. * declared by other options, e.g. computed, inject
  143. */
  144. OPTIONS = 'options',
  145. /**
  146. * a literal constant, e.g. 'foo', 1, true
  147. */
  148. LITERAL_CONST = 'literal-const',
  149. }
  150. export type BindingMetadata = {
  151. [key: string]: BindingTypes | undefined
  152. } & {
  153. __isScriptSetup?: boolean
  154. __propsAliases?: Record<string, string>
  155. }
  156. interface SharedTransformCodegenOptions {
  157. /**
  158. * Transform expressions like {{ foo }} to `_ctx.foo`.
  159. * If this option is false, the generated code will be wrapped in a
  160. * `with (this) { ... }` block.
  161. * - This is force-enabled in module mode, since modules are by default strict
  162. * and cannot use `with`
  163. * @default mode === 'module'
  164. */
  165. prefixIdentifiers?: boolean
  166. /**
  167. * A list of parser plugins to enable for `@babel/parser`, which is used to
  168. * parse expressions in bindings and interpolations.
  169. * https://babeljs.io/docs/en/next/babel-parser#plugins
  170. */
  171. expressionPlugins?: ParserPlugin[]
  172. /**
  173. * Control whether generate SSR-optimized render functions instead.
  174. * The resulting function must be attached to the component via the
  175. * `ssrRender` option instead of `render`.
  176. *
  177. * When compiler generates code for SSR's fallback branch, we need to set it to false:
  178. * - context.ssr = false
  179. *
  180. * see `subTransform` in `ssrTransformComponent.ts`
  181. */
  182. ssr?: boolean
  183. /**
  184. * Indicates whether the compiler generates code for SSR,
  185. * it is always true when generating code for SSR,
  186. * regardless of whether we are generating code for SSR's fallback branch,
  187. * this means that when the compiler generates code for SSR's fallback branch:
  188. * - context.ssr = false
  189. * - context.inSSR = true
  190. */
  191. inSSR?: boolean
  192. /**
  193. * Optional binding metadata analyzed from script - used to optimize
  194. * binding access when `prefixIdentifiers` is enabled.
  195. */
  196. bindingMetadata?: BindingMetadata
  197. /**
  198. * Compile the function for inlining inside setup().
  199. * This allows the function to directly access setup() local bindings.
  200. */
  201. inline?: boolean
  202. /**
  203. * Indicates that transforms and codegen should try to output valid TS code
  204. */
  205. isTS?: boolean
  206. /**
  207. * Filename for source map generation.
  208. * Also used for self-recursive reference in templates
  209. * @default 'template.vue.html'
  210. */
  211. filename?: string
  212. }
  213. export interface TransformOptions
  214. extends SharedTransformCodegenOptions,
  215. ErrorHandlingOptions,
  216. CompilerCompatOptions {
  217. /**
  218. * An array of node transforms to be applied to every AST node.
  219. */
  220. nodeTransforms?: NodeTransform[]
  221. /**
  222. * An object of { name: transform } to be applied to every directive attribute
  223. * node found on element nodes.
  224. */
  225. directiveTransforms?: Record<string, DirectiveTransform | undefined>
  226. /**
  227. * An optional hook to transform a node being hoisted.
  228. * used by compiler-dom to turn hoisted nodes into stringified HTML vnodes.
  229. * @default null
  230. */
  231. transformHoist?: HoistTransform | null
  232. /**
  233. * If the pairing runtime provides additional built-in elements, use this to
  234. * mark them as built-in so the compiler will generate component vnodes
  235. * for them.
  236. */
  237. isBuiltInComponent?: (tag: string) => symbol | void
  238. /**
  239. * Used by some transforms that expects only native elements
  240. */
  241. isCustomElement?: (tag: string) => boolean | void
  242. /**
  243. * Hoist static VNodes and props objects to `_hoisted_x` constants
  244. * @default false
  245. */
  246. hoistStatic?: boolean
  247. /**
  248. * Cache v-on handlers to avoid creating new inline functions on each render,
  249. * also avoids the need for dynamically patching the handlers by wrapping it.
  250. * e.g `@click="foo"` by default is compiled to `{ onClick: foo }`. With this
  251. * option it's compiled to:
  252. * ```js
  253. * { onClick: _cache[0] || (_cache[0] = e => _ctx.foo(e)) }
  254. * ```
  255. * - Requires "prefixIdentifiers" to be enabled because it relies on scope
  256. * analysis to determine if a handler is safe to cache.
  257. * @default false
  258. */
  259. cacheHandlers?: boolean
  260. /**
  261. * SFC scoped styles ID
  262. */
  263. scopeId?: string | null
  264. /**
  265. * Indicates this SFC template has used :slotted in its styles
  266. * Defaults to `true` for backwards compatibility - SFC tooling should set it
  267. * to `false` if no `:slotted` usage is detected in `<style>`
  268. */
  269. slotted?: boolean
  270. /**
  271. * SFC `<style vars>` injection string
  272. * Should already be an object expression, e.g. `{ 'xxxx-color': color }`
  273. * needed to render inline CSS variables on component root
  274. */
  275. ssrCssVars?: string
  276. /**
  277. * Whether to compile the template assuming it needs to handle HMR.
  278. * Some edge cases may need to generate different code for HMR to work
  279. * correctly, e.g. #6938, #7138
  280. */
  281. hmr?: boolean
  282. }
  283. export interface CodegenOptions extends SharedTransformCodegenOptions {
  284. /**
  285. * - `module` mode will generate ES module import statements for helpers
  286. * and export the render function as the default export.
  287. * - `function` mode will generate a single `const { helpers... } = Vue`
  288. * statement and return the render function. It expects `Vue` to be globally
  289. * available (or passed by wrapping the code with an IIFE). It is meant to be
  290. * used with `new Function(code)()` to generate a render function at runtime.
  291. * @default 'function'
  292. */
  293. mode?: 'module' | 'function'
  294. /**
  295. * Generate source map?
  296. * @default false
  297. */
  298. sourceMap?: boolean
  299. /**
  300. * SFC scoped styles ID
  301. */
  302. scopeId?: string | null
  303. /**
  304. * Option to optimize helper import bindings via variable assignment
  305. * (only used for webpack code-split)
  306. * @default false
  307. */
  308. optimizeImports?: boolean
  309. /**
  310. * Customize where to import runtime helpers from.
  311. * @default 'vue'
  312. */
  313. runtimeModuleName?: string
  314. /**
  315. * Customize where to import runtime helpers from.
  316. * @default 'vue/vapor'
  317. */
  318. vaporRuntimeModuleName?: string
  319. /**
  320. * Customize where to import ssr runtime helpers from/**
  321. * @default 'vue/server-renderer'
  322. */
  323. ssrRuntimeModuleName?: string
  324. /**
  325. * Customize the global variable name of `Vue` to get helpers from
  326. * in function mode
  327. * @default 'Vue'
  328. */
  329. runtimeGlobalName?: string
  330. }
  331. export type CompilerOptions = ParserOptions & TransformOptions & CodegenOptions