options.ts 8.5 KB

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