options.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. * Keep comments in the templates AST, even in production
  63. */
  64. comments?: boolean
  65. }
  66. export type HoistTransform = (
  67. children: TemplateChildNode[],
  68. context: TransformContext,
  69. parent: ParentNode
  70. ) => void
  71. export const enum BindingTypes {
  72. /**
  73. * returned from data()
  74. */
  75. DATA = 'data',
  76. /**
  77. * decalred as a prop
  78. */
  79. PROPS = 'props',
  80. /**
  81. * a let binding (may or may not be a ref)
  82. */
  83. SETUP_LET = 'setup-let',
  84. /**
  85. * a const binding that can never be a ref.
  86. * these bindings don't need `unref()` calls when processed in inlined
  87. * template expressions.
  88. */
  89. SETUP_CONST = 'setup-const',
  90. /**
  91. * a const binding that may be a ref.
  92. */
  93. SETUP_MAYBE_REF = 'setup-maybe-ref',
  94. /**
  95. * bindings that are guaranteed to be refs
  96. */
  97. SETUP_REF = 'setup-ref',
  98. /**
  99. * declared by other options, e.g. computed, inject
  100. */
  101. OPTIONS = 'options'
  102. }
  103. export type BindingMetadata = {
  104. [key: string]: BindingTypes | undefined
  105. } & {
  106. __isScriptSetup?: boolean
  107. }
  108. interface SharedTransformCodegenOptions {
  109. /**
  110. * Transform expressions like {{ foo }} to `_ctx.foo`.
  111. * If this option is false, the generated code will be wrapped in a
  112. * `with (this) { ... }` block.
  113. * - This is force-enabled in module mode, since modules are by default strict
  114. * and cannot use `with`
  115. * @default mode === 'module'
  116. */
  117. prefixIdentifiers?: boolean
  118. /**
  119. * Generate SSR-optimized render functions instead.
  120. * The resulting function must be attached to the component via the
  121. * `ssrRender` option instead of `render`.
  122. */
  123. ssr?: boolean
  124. /**
  125. * Optional binding metadata analyzed from script - used to optimize
  126. * binding access when `prefixIdentifiers` is enabled.
  127. */
  128. bindingMetadata?: BindingMetadata
  129. /**
  130. * Compile the function for inlining inside setup().
  131. * This allows the function to directly access setup() local bindings.
  132. */
  133. inline?: boolean
  134. /**
  135. * Indicates that transforms and codegen should try to output valid TS code
  136. */
  137. isTS?: boolean
  138. /**
  139. * Filename for source map generation.
  140. * Also used for self-recursive reference in templates
  141. * @default 'template.vue.html'
  142. */
  143. filename?: string
  144. }
  145. export interface TransformOptions
  146. extends SharedTransformCodegenOptions,
  147. ErrorHandlingOptions,
  148. CompilerCompatOptions {
  149. /**
  150. * An array of node transforms to be applied to every AST node.
  151. */
  152. nodeTransforms?: NodeTransform[]
  153. /**
  154. * An object of { name: transform } to be applied to every directive attribute
  155. * node found on element nodes.
  156. */
  157. directiveTransforms?: Record<string, DirectiveTransform | undefined>
  158. /**
  159. * An optional hook to transform a node being hoisted.
  160. * used by compiler-dom to turn hoisted nodes into stringified HTML vnodes.
  161. * @default null
  162. */
  163. transformHoist?: HoistTransform | null
  164. /**
  165. * If the pairing runtime provides additional built-in elements, use this to
  166. * mark them as built-in so the compiler will generate component vnodes
  167. * for them.
  168. */
  169. isBuiltInComponent?: (tag: string) => symbol | void
  170. /**
  171. * Used by some transforms that expects only native elements
  172. */
  173. isCustomElement?: (tag: string) => boolean | void
  174. /**
  175. * Transform expressions like {{ foo }} to `_ctx.foo`.
  176. * If this option is false, the generated code will be wrapped in a
  177. * `with (this) { ... }` block.
  178. * - This is force-enabled in module mode, since modules are by default strict
  179. * and cannot use `with`
  180. * @default mode === 'module'
  181. */
  182. prefixIdentifiers?: boolean
  183. /**
  184. * Hoist static VNodes and props objects to `_hoisted_x` constants
  185. * @default false
  186. */
  187. hoistStatic?: boolean
  188. /**
  189. * Cache v-on handlers to avoid creating new inline functions on each render,
  190. * also avoids the need for dynamically patching the handlers by wrapping it.
  191. * e.g `@click="foo"` by default is compiled to `{ onClick: foo }`. With this
  192. * option it's compiled to:
  193. * ```js
  194. * { onClick: _cache[0] || (_cache[0] = e => _ctx.foo(e)) }
  195. * ```
  196. * - Requires "prefixIdentifiers" to be enabled because it relies on scope
  197. * analysis to determine if a handler is safe to cache.
  198. * @default false
  199. */
  200. cacheHandlers?: boolean
  201. /**
  202. * A list of parser plugins to enable for `@babel/parser`, which is used to
  203. * parse expressions in bindings and interpolations.
  204. * https://babeljs.io/docs/en/next/babel-parser#plugins
  205. */
  206. expressionPlugins?: ParserPlugin[]
  207. /**
  208. * SFC scoped styles ID
  209. */
  210. scopeId?: string | null
  211. /**
  212. * Indicates this SFC template has used :slotted in its styles
  213. * Defaults to `true` for backwards compatibility - SFC tooling should set it
  214. * to `false` if no `:slotted` usage is detected in `<style>`
  215. */
  216. slotted?: boolean
  217. /**
  218. * SFC `<style vars>` injection string
  219. * Should already be an object expression, e.g. `{ 'xxxx-color': color }`
  220. * needed to render inline CSS variables on component root
  221. */
  222. ssrCssVars?: string
  223. }
  224. export interface CodegenOptions extends SharedTransformCodegenOptions {
  225. /**
  226. * - `module` mode will generate ES module import statements for helpers
  227. * and export the render function as the default export.
  228. * - `function` mode will generate a single `const { helpers... } = Vue`
  229. * statement and return the render function. It expects `Vue` to be globally
  230. * available (or passed by wrapping the code with an IIFE). It is meant to be
  231. * used with `new Function(code)()` to generate a render function at runtime.
  232. * @default 'function'
  233. */
  234. mode?: 'module' | 'function'
  235. /**
  236. * Generate source map?
  237. * @default false
  238. */
  239. sourceMap?: boolean
  240. /**
  241. * SFC scoped styles ID
  242. */
  243. scopeId?: string | null
  244. /**
  245. * Option to optimize helper import bindings via variable assignment
  246. * (only used for webpack code-split)
  247. * @default false
  248. */
  249. optimizeImports?: boolean
  250. /**
  251. * Customize where to import runtime helpers from.
  252. * @default 'vue'
  253. */
  254. runtimeModuleName?: string
  255. /**
  256. * Customize the global variable name of `Vue` to get helpers from
  257. * in function mode
  258. * @default 'Vue'
  259. */
  260. runtimeGlobalName?: string
  261. }
  262. export type CompilerOptions = ParserOptions & TransformOptions & CodegenOptions