2020-06-10 18:31:51 +00:00
|
|
|
import { ElementNode, Namespace, TemplateChildNode, ParentNode } from './ast'
|
2019-12-10 17:53:26 +00:00
|
|
|
import { TextModes } from './parse'
|
|
|
|
import { CompilerError } from './errors'
|
2020-02-12 16:56:42 +00:00
|
|
|
import {
|
|
|
|
NodeTransform,
|
|
|
|
DirectiveTransform,
|
|
|
|
TransformContext
|
|
|
|
} from './transform'
|
2021-04-12 23:42:09 +00:00
|
|
|
import { CompilerCompatOptions } from './compat/compatConfig'
|
2020-02-27 21:53:51 +00:00
|
|
|
import { ParserPlugin } from '@babel/parser'
|
2019-12-10 17:53:26 +00:00
|
|
|
|
2021-04-12 23:42:09 +00:00
|
|
|
export interface ErrorHandlingOptions {
|
2021-04-16 15:51:47 +00:00
|
|
|
onWarn?: (warning: CompilerError) => void
|
2021-04-12 23:42:09 +00:00
|
|
|
onError?: (error: CompilerError) => void
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ParserOptions
|
|
|
|
extends ErrorHandlingOptions,
|
|
|
|
CompilerCompatOptions {
|
2020-05-07 15:01:36 +00:00
|
|
|
/**
|
2020-07-17 15:24:12 +00:00
|
|
|
* e.g. platform native elements, e.g. `<div>` for browsers
|
2020-05-07 15:01:36 +00:00
|
|
|
*/
|
2020-03-24 22:29:15 +00:00
|
|
|
isNativeTag?: (tag: string) => boolean
|
2020-05-07 15:01:36 +00:00
|
|
|
/**
|
2020-07-17 15:24:12 +00:00
|
|
|
* e.g. native elements that can self-close, e.g. `<img>`, `<br>`, `<hr>`
|
2020-05-07 15:01:36 +00:00
|
|
|
*/
|
2020-03-24 22:29:15 +00:00
|
|
|
isVoidTag?: (tag: string) => boolean
|
2020-05-07 15:01:36 +00:00
|
|
|
/**
|
2020-07-17 15:24:12 +00:00
|
|
|
* e.g. elements that should preserve whitespace inside, e.g. `<pre>`
|
2020-05-07 15:01:36 +00:00
|
|
|
*/
|
2020-03-24 22:29:15 +00:00
|
|
|
isPreTag?: (tag: string) => boolean
|
2020-05-07 15:01:36 +00:00
|
|
|
/**
|
2020-07-17 15:24:12 +00:00
|
|
|
* Platform-specific built-in components e.g. `<Transition>`
|
2020-05-07 15:01:36 +00:00
|
|
|
*/
|
2019-12-10 17:53:26 +00:00
|
|
|
isBuiltInComponent?: (tag: string) => symbol | void
|
2020-05-07 15:01:36 +00:00
|
|
|
/**
|
|
|
|
* Separate option for end users to extend the native elements list
|
|
|
|
*/
|
2020-07-27 21:09:21 +00:00
|
|
|
isCustomElement?: (tag: string) => boolean | void
|
2020-05-07 15:01:36 +00:00
|
|
|
/**
|
|
|
|
* Get tag namespace
|
|
|
|
*/
|
2019-12-10 17:53:26 +00:00
|
|
|
getNamespace?: (tag: string, parent: ElementNode | undefined) => Namespace
|
2020-05-07 15:01:36 +00:00
|
|
|
/**
|
|
|
|
* Get text parsing mode for this element
|
|
|
|
*/
|
2019-12-23 02:09:39 +00:00
|
|
|
getTextMode?: (
|
2020-05-07 15:01:36 +00:00
|
|
|
node: ElementNode,
|
2019-12-23 02:09:39 +00:00
|
|
|
parent: ElementNode | undefined
|
|
|
|
) => TextModes
|
2020-05-07 15:01:36 +00:00
|
|
|
/**
|
|
|
|
* @default ['{{', '}}']
|
|
|
|
*/
|
|
|
|
delimiters?: [string, string]
|
2020-07-16 13:28:09 +00:00
|
|
|
/**
|
|
|
|
* Whitespace handling strategy
|
|
|
|
*/
|
|
|
|
whitespace?: 'preserve' | 'condense'
|
2020-05-07 15:01:36 +00:00
|
|
|
/**
|
|
|
|
* Only needed for DOM compilers
|
|
|
|
*/
|
2020-04-08 22:51:25 +00:00
|
|
|
decodeEntities?: (rawText: string, asAttr: boolean) => string
|
2020-08-17 15:20:28 +00:00
|
|
|
/**
|
|
|
|
* Keep comments in the templates AST, even in production
|
|
|
|
*/
|
|
|
|
comments?: boolean
|
2019-12-10 17:53:26 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 16:56:42 +00:00
|
|
|
export type HoistTransform = (
|
2020-05-15 16:58:44 +00:00
|
|
|
children: TemplateChildNode[],
|
2020-06-10 18:31:51 +00:00
|
|
|
context: TransformContext,
|
|
|
|
parent: ParentNode
|
2020-05-15 16:58:44 +00:00
|
|
|
) => void
|
2020-02-12 16:56:42 +00:00
|
|
|
|
2020-11-12 21:11:14 +00:00
|
|
|
export const enum BindingTypes {
|
2020-11-18 20:17:50 +00:00
|
|
|
/**
|
|
|
|
* returned from data()
|
|
|
|
*/
|
2020-11-12 21:11:14 +00:00
|
|
|
DATA = 'data',
|
2020-11-18 20:17:50 +00:00
|
|
|
/**
|
|
|
|
* decalred as a prop
|
|
|
|
*/
|
2020-11-12 21:11:14 +00:00
|
|
|
PROPS = 'props',
|
2020-11-18 20:17:50 +00:00
|
|
|
/**
|
|
|
|
* a let binding (may or may not be a ref)
|
|
|
|
*/
|
|
|
|
SETUP_LET = 'setup-let',
|
|
|
|
/**
|
|
|
|
* a const binding that can never be a ref.
|
|
|
|
* these bindings don't need `unref()` calls when processed in inlined
|
|
|
|
* template expressions.
|
|
|
|
*/
|
|
|
|
SETUP_CONST = 'setup-const',
|
|
|
|
/**
|
|
|
|
* a const binding that may be a ref.
|
|
|
|
*/
|
2020-11-19 00:38:18 +00:00
|
|
|
SETUP_MAYBE_REF = 'setup-maybe-ref',
|
|
|
|
/**
|
|
|
|
* bindings that are guaranteed to be refs
|
|
|
|
*/
|
|
|
|
SETUP_REF = 'setup-ref',
|
2020-11-18 20:17:50 +00:00
|
|
|
/**
|
|
|
|
* declared by other options, e.g. computed, inject
|
|
|
|
*/
|
2020-11-12 21:11:14 +00:00
|
|
|
OPTIONS = 'options'
|
|
|
|
}
|
|
|
|
|
2021-03-29 20:11:31 +00:00
|
|
|
export type BindingMetadata = {
|
2020-11-19 00:38:18 +00:00
|
|
|
[key: string]: BindingTypes | undefined
|
2021-03-29 20:11:31 +00:00
|
|
|
} & {
|
|
|
|
__isScriptSetup?: boolean
|
2020-07-11 02:12:25 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 21:28:34 +00:00
|
|
|
interface SharedTransformCodegenOptions {
|
|
|
|
/**
|
|
|
|
* Transform expressions like {{ foo }} to `_ctx.foo`.
|
|
|
|
* If this option is false, the generated code will be wrapped in a
|
|
|
|
* `with (this) { ... }` block.
|
|
|
|
* - This is force-enabled in module mode, since modules are by default strict
|
|
|
|
* and cannot use `with`
|
|
|
|
* @default mode === 'module'
|
|
|
|
*/
|
|
|
|
prefixIdentifiers?: boolean
|
|
|
|
/**
|
2021-06-22 23:15:20 +00:00
|
|
|
* Control whether generate SSR-optimized render functions instead.
|
2020-11-10 21:28:34 +00:00
|
|
|
* The resulting function must be attached to the component via the
|
|
|
|
* `ssrRender` option instead of `render`.
|
2021-06-22 23:15:20 +00:00
|
|
|
*
|
|
|
|
* When compiler generates code for SSR's fallback branch, we need to set it to false:
|
|
|
|
* - context.ssr = false
|
|
|
|
*
|
|
|
|
* see `subTransform` in `ssrTransformCompoent.ts`
|
2020-11-10 21:28:34 +00:00
|
|
|
*/
|
|
|
|
ssr?: boolean
|
2021-06-22 23:15:20 +00:00
|
|
|
/**
|
|
|
|
* Indicates whether the compiler generates code for SSR,
|
|
|
|
* it is always true when generating code for SSR,
|
|
|
|
* regardless of whether we are generating code for SSR's fallback branch,
|
|
|
|
* this means that when the compiler generates code for SSR's fallback branch:
|
|
|
|
* - context.ssr = false
|
|
|
|
* - context.inSSR = true
|
|
|
|
*/
|
|
|
|
inSSR?: boolean
|
2020-11-10 21:28:34 +00:00
|
|
|
/**
|
|
|
|
* Optional binding metadata analyzed from script - used to optimize
|
|
|
|
* binding access when `prefixIdentifiers` is enabled.
|
|
|
|
*/
|
|
|
|
bindingMetadata?: BindingMetadata
|
|
|
|
/**
|
|
|
|
* Compile the function for inlining inside setup().
|
|
|
|
* This allows the function to directly access setup() local bindings.
|
|
|
|
*/
|
|
|
|
inline?: boolean
|
2020-11-19 02:16:09 +00:00
|
|
|
/**
|
|
|
|
* Indicates that transforms and codegen should try to output valid TS code
|
|
|
|
*/
|
|
|
|
isTS?: boolean
|
2020-11-30 17:30:35 +00:00
|
|
|
/**
|
|
|
|
* Filename for source map generation.
|
|
|
|
* Also used for self-recursive reference in templates
|
|
|
|
* @default 'template.vue.html'
|
|
|
|
*/
|
|
|
|
filename?: string
|
2020-11-10 21:28:34 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 23:42:09 +00:00
|
|
|
export interface TransformOptions
|
|
|
|
extends SharedTransformCodegenOptions,
|
|
|
|
ErrorHandlingOptions,
|
|
|
|
CompilerCompatOptions {
|
2020-05-07 15:01:36 +00:00
|
|
|
/**
|
2020-07-08 10:32:42 +00:00
|
|
|
* An array of node transforms to be applied to every AST node.
|
2020-05-07 15:01:36 +00:00
|
|
|
*/
|
2019-12-10 17:53:26 +00:00
|
|
|
nodeTransforms?: NodeTransform[]
|
2020-05-07 15:01:36 +00:00
|
|
|
/**
|
|
|
|
* An object of { name: transform } to be applied to every directive attribute
|
|
|
|
* node found on element nodes.
|
|
|
|
*/
|
2020-02-04 17:20:51 +00:00
|
|
|
directiveTransforms?: Record<string, DirectiveTransform | undefined>
|
2020-05-07 15:01:36 +00:00
|
|
|
/**
|
|
|
|
* An optional hook to transform a node being hoisted.
|
|
|
|
* used by compiler-dom to turn hoisted nodes into stringified HTML vnodes.
|
|
|
|
* @default null
|
|
|
|
*/
|
2020-02-12 16:56:42 +00:00
|
|
|
transformHoist?: HoistTransform | null
|
2020-05-07 15:01:36 +00:00
|
|
|
/**
|
|
|
|
* If the pairing runtime provides additional built-in elements, use this to
|
|
|
|
* mark them as built-in so the compiler will generate component vnodes
|
|
|
|
* for them.
|
|
|
|
*/
|
2019-12-10 17:53:26 +00:00
|
|
|
isBuiltInComponent?: (tag: string) => symbol | void
|
2020-07-27 21:09:21 +00:00
|
|
|
/**
|
|
|
|
* Used by some transforms that expects only native elements
|
|
|
|
*/
|
|
|
|
isCustomElement?: (tag: string) => boolean | void
|
2020-05-07 15:01:36 +00:00
|
|
|
/**
|
|
|
|
* Transform expressions like {{ foo }} to `_ctx.foo`.
|
|
|
|
* If this option is false, the generated code will be wrapped in a
|
|
|
|
* `with (this) { ... }` block.
|
|
|
|
* - This is force-enabled in module mode, since modules are by default strict
|
|
|
|
* and cannot use `with`
|
|
|
|
* @default mode === 'module'
|
|
|
|
*/
|
2019-12-10 17:53:26 +00:00
|
|
|
prefixIdentifiers?: boolean
|
2020-05-07 15:01:36 +00:00
|
|
|
/**
|
|
|
|
* Hoist static VNodes and props objects to `_hoisted_x` constants
|
|
|
|
* @default false
|
|
|
|
*/
|
2019-12-10 17:53:26 +00:00
|
|
|
hoistStatic?: boolean
|
2020-05-07 15:01:36 +00:00
|
|
|
/**
|
|
|
|
* Cache v-on handlers to avoid creating new inline functions on each render,
|
|
|
|
* also avoids the need for dynamically patching the handlers by wrapping it.
|
|
|
|
* e.g `@click="foo"` by default is compiled to `{ onClick: foo }`. With this
|
|
|
|
* option it's compiled to:
|
|
|
|
* ```js
|
|
|
|
* { onClick: _cache[0] || (_cache[0] = e => _ctx.foo(e)) }
|
|
|
|
* ```
|
|
|
|
* - Requires "prefixIdentifiers" to be enabled because it relies on scope
|
|
|
|
* analysis to determine if a handler is safe to cache.
|
|
|
|
* @default false
|
|
|
|
*/
|
2019-12-10 17:53:26 +00:00
|
|
|
cacheHandlers?: boolean
|
2020-05-07 15:01:36 +00:00
|
|
|
/**
|
|
|
|
* A list of parser plugins to enable for `@babel/parser`, which is used to
|
|
|
|
* parse expressions in bindings and interpolations.
|
|
|
|
* https://babeljs.io/docs/en/next/babel-parser#plugins
|
|
|
|
*/
|
2020-02-27 21:53:51 +00:00
|
|
|
expressionPlugins?: ParserPlugin[]
|
2020-05-07 15:01:36 +00:00
|
|
|
/**
|
|
|
|
* SFC scoped styles ID
|
|
|
|
*/
|
2020-02-06 21:51:26 +00:00
|
|
|
scopeId?: string | null
|
2021-03-05 17:12:49 +00:00
|
|
|
/**
|
|
|
|
* Indicates this SFC template has used :slotted in its styles
|
|
|
|
* Defaults to `true` for backwards compatibility - SFC tooling should set it
|
|
|
|
* to `false` if no `:slotted` usage is detected in `<style>`
|
|
|
|
*/
|
|
|
|
slotted?: boolean
|
2020-07-12 22:04:09 +00:00
|
|
|
/**
|
2020-07-17 15:24:12 +00:00
|
|
|
* SFC `<style vars>` injection string
|
2020-11-17 23:54:47 +00:00
|
|
|
* Should already be an object expression, e.g. `{ 'xxxx-color': color }`
|
2020-07-12 22:04:09 +00:00
|
|
|
* needed to render inline CSS variables on component root
|
|
|
|
*/
|
|
|
|
ssrCssVars?: string
|
2019-12-10 17:53:26 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 21:28:34 +00:00
|
|
|
export interface CodegenOptions extends SharedTransformCodegenOptions {
|
2020-05-07 15:01:36 +00:00
|
|
|
/**
|
|
|
|
* - `module` mode will generate ES module import statements for helpers
|
|
|
|
* and export the render function as the default export.
|
|
|
|
* - `function` mode will generate a single `const { helpers... } = Vue`
|
|
|
|
* statement and return the render function. It expects `Vue` to be globally
|
|
|
|
* available (or passed by wrapping the code with an IIFE). It is meant to be
|
|
|
|
* used with `new Function(code)()` to generate a render function at runtime.
|
|
|
|
* @default 'function'
|
|
|
|
*/
|
2020-02-04 20:58:54 +00:00
|
|
|
mode?: 'module' | 'function'
|
2020-05-07 15:01:36 +00:00
|
|
|
/**
|
|
|
|
* Generate source map?
|
|
|
|
* @default false
|
|
|
|
*/
|
2019-12-10 17:53:26 +00:00
|
|
|
sourceMap?: boolean
|
2020-05-07 15:01:36 +00:00
|
|
|
/**
|
|
|
|
* SFC scoped styles ID
|
|
|
|
*/
|
2019-12-16 17:11:51 +00:00
|
|
|
scopeId?: string | null
|
2020-05-07 15:01:36 +00:00
|
|
|
/**
|
|
|
|
* Option to optimize helper import bindings via variable assignment
|
|
|
|
* (only used for webpack code-split)
|
|
|
|
* @default false
|
|
|
|
*/
|
2020-07-11 00:43:52 +00:00
|
|
|
optimizeImports?: boolean
|
2020-05-07 15:01:36 +00:00
|
|
|
/**
|
|
|
|
* Customize where to import runtime helpers from.
|
|
|
|
* @default 'vue'
|
|
|
|
*/
|
2020-02-06 20:29:02 +00:00
|
|
|
runtimeModuleName?: string
|
2020-05-07 15:01:36 +00:00
|
|
|
/**
|
|
|
|
* Customize the global variable name of `Vue` to get helpers from
|
|
|
|
* in function mode
|
|
|
|
* @default 'Vue'
|
|
|
|
*/
|
2020-02-06 20:29:02 +00:00
|
|
|
runtimeGlobalName?: string
|
2019-12-10 17:53:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export type CompilerOptions = ParserOptions & TransformOptions & CodegenOptions
|