options.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { ElementNode, Namespace, JSChildNode, PlainElementNode } 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. // e.g. platform native elements, e.g. <div> for browsers
  12. isNativeTag?: (tag: string) => boolean
  13. // e.g. native elements that can self-close, e.g. <img>, <br>, <hr>
  14. isVoidTag?: (tag: string) => boolean
  15. // e.g. elements that should preserve whitespace inside, e.g. <pre>
  16. isPreTag?: (tag: string) => boolean
  17. // platform-specific built-in components e.g. <Transition>
  18. isBuiltInComponent?: (tag: string) => symbol | void
  19. // separate option for end users to extend the native elements list
  20. isCustomElement?: (tag: string) => boolean
  21. getNamespace?: (tag: string, parent: ElementNode | undefined) => Namespace
  22. getTextMode?: (
  23. tag: string,
  24. ns: Namespace,
  25. parent: ElementNode | undefined
  26. ) => TextModes
  27. delimiters?: [string, string] // ['{{', '}}']
  28. decodeEntities?: (rawText: string, asAttr: boolean) => string
  29. onError?: (error: CompilerError) => void
  30. }
  31. export type HoistTransform = (
  32. node: PlainElementNode,
  33. context: TransformContext
  34. ) => JSChildNode
  35. export interface TransformOptions {
  36. nodeTransforms?: NodeTransform[]
  37. directiveTransforms?: Record<string, DirectiveTransform | undefined>
  38. // an optional hook to transform a node being hoisted.
  39. // used by compiler-dom to turn hoisted nodes into stringified HTML vnodes.
  40. transformHoist?: HoistTransform | null
  41. isBuiltInComponent?: (tag: string) => symbol | void
  42. // Transform expressions like {{ foo }} to `_ctx.foo`.
  43. // If this option is false, the generated code will be wrapped in a
  44. // `with (this) { ... }` block.
  45. // - This is force-enabled in module mode, since modules are by default strict
  46. // and cannot use `with`
  47. // - Default: mode === 'module'
  48. prefixIdentifiers?: boolean
  49. // Hoist static VNodes and props objects to `_hoisted_x` constants
  50. // - Default: false
  51. hoistStatic?: boolean
  52. // Cache v-on handlers to avoid creating new inline functions on each render,
  53. // also avoids the need for dynamically patching the handlers by wrapping it.
  54. // e.g `@click="foo"` by default is compiled to `{ onClick: foo }`. With this
  55. // option it's compiled to:
  56. // `{ onClick: _cache[0] || (_cache[0] = e => _ctx.foo(e)) }`
  57. // - Requires "prefixIdentifiers" to be enabled because it relies on scope
  58. // analysis to determine if a handler is safe to cache.
  59. // - Default: false
  60. cacheHandlers?: boolean
  61. // a list of parser plugins to enable for @babel/parser
  62. // https://babeljs.io/docs/en/next/babel-parser#plugins
  63. expressionPlugins?: ParserPlugin[]
  64. // SFC scoped styles ID
  65. scopeId?: string | null
  66. ssr?: boolean
  67. onError?: (error: CompilerError) => void
  68. }
  69. export interface CodegenOptions {
  70. // - Module mode will generate ES module import statements for helpers
  71. // and export the render function as the default export.
  72. // - Function mode will generate a single `const { helpers... } = Vue`
  73. // statement and return the render function. It is meant to be used with
  74. // `new Function(code)()` to generate a render function at runtime.
  75. // - Default: 'function'
  76. mode?: 'module' | 'function'
  77. // Generate source map?
  78. // - Default: false
  79. sourceMap?: boolean
  80. // Filename for source map generation.
  81. // - Default: `template.vue.html`
  82. filename?: string
  83. // SFC scoped styles ID
  84. scopeId?: string | null
  85. // we need to know about this to generate proper preambles
  86. prefixIdentifiers?: boolean
  87. // option to optimize helper import bindings via variable assignment
  88. // (only used for webpack code-split)
  89. optimizeBindings?: boolean
  90. // for specifying where to import helpers
  91. runtimeModuleName?: string
  92. runtimeGlobalName?: string
  93. // generate ssr-specific code?
  94. ssr?: boolean
  95. }
  96. export type CompilerOptions = ParserOptions & TransformOptions & CodegenOptions