options.ts 7.5 KB

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