options.ts 9.8 KB

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