2019-09-18 07:08:47 +08:00
|
|
|
import {
|
|
|
|
RootNode,
|
|
|
|
NodeTypes,
|
|
|
|
ParentNode,
|
2019-10-01 21:27:34 +08:00
|
|
|
TemplateChildNode,
|
2019-09-18 07:08:47 +08:00
|
|
|
ElementNode,
|
2019-09-22 05:42:12 +08:00
|
|
|
DirectiveNode,
|
2019-09-24 01:25:18 +08:00
|
|
|
Property,
|
2019-09-26 02:13:33 +08:00
|
|
|
ExpressionNode,
|
2019-09-27 23:42:02 +08:00
|
|
|
createSimpleExpression,
|
2019-10-01 09:17:12 +08:00
|
|
|
JSChildNode,
|
2019-10-03 01:11:07 +08:00
|
|
|
SimpleExpressionNode,
|
2019-10-08 22:50:00 +08:00
|
|
|
ElementTypes,
|
|
|
|
ElementCodegenNode,
|
|
|
|
ComponentCodegenNode,
|
|
|
|
createCallExpression
|
2019-09-18 07:08:47 +08:00
|
|
|
} from './ast'
|
2019-09-24 01:25:18 +08:00
|
|
|
import { isString, isArray } from '@vue/shared'
|
2019-09-21 00:16:19 +08:00
|
|
|
import { CompilerError, defaultOnError } from './errors'
|
2019-10-06 05:18:25 +08:00
|
|
|
import {
|
|
|
|
TO_STRING,
|
|
|
|
COMMENT,
|
|
|
|
CREATE_VNODE,
|
|
|
|
FRAGMENT,
|
|
|
|
RuntimeHelper,
|
2019-10-08 22:50:00 +08:00
|
|
|
helperNameMap,
|
|
|
|
APPLY_DIRECTIVES,
|
|
|
|
CREATE_BLOCK
|
2019-10-06 05:18:25 +08:00
|
|
|
} from './runtimeHelpers'
|
2019-10-08 05:12:22 +08:00
|
|
|
import { isVSlot, createBlockExpression } from './utils'
|
|
|
|
import { hoistStatic, isSingleElementRoot } from './transforms/hoistStatic'
|
2019-09-18 07:08:47 +08:00
|
|
|
|
2019-09-22 05:42:12 +08:00
|
|
|
// There are two types of transforms:
|
|
|
|
//
|
|
|
|
// - NodeTransform:
|
|
|
|
// Transforms that operate directly on a ChildNode. NodeTransforms may mutate,
|
|
|
|
// replace or remove the node being processed.
|
2019-09-24 01:25:18 +08:00
|
|
|
export type NodeTransform = (
|
2019-10-01 21:27:34 +08:00
|
|
|
node: RootNode | TemplateChildNode,
|
2019-09-24 01:25:18 +08:00
|
|
|
context: TransformContext
|
|
|
|
) => void | (() => void) | (() => void)[]
|
2019-09-18 07:08:47 +08:00
|
|
|
|
2019-09-22 05:42:12 +08:00
|
|
|
// - DirectiveTransform:
|
|
|
|
// Transforms that handles a single directive attribute on an element.
|
|
|
|
// It translates the raw directive into actual props for the VNode.
|
2019-09-18 07:08:47 +08:00
|
|
|
export type DirectiveTransform = (
|
2019-09-22 05:42:12 +08:00
|
|
|
dir: DirectiveNode,
|
2019-10-09 03:35:57 +08:00
|
|
|
node: ElementNode,
|
2019-09-22 05:42:12 +08:00
|
|
|
context: TransformContext
|
|
|
|
) => {
|
|
|
|
props: Property | Property[]
|
|
|
|
needRuntime: boolean
|
|
|
|
}
|
|
|
|
|
2019-10-05 22:38:02 +08:00
|
|
|
// A structural directive transform is a technically a NodeTransform;
|
2019-09-22 05:42:12 +08:00
|
|
|
// Only v-if and v-for fall into this category.
|
|
|
|
export type StructuralDirectiveTransform = (
|
2019-09-18 07:08:47 +08:00
|
|
|
node: ElementNode,
|
|
|
|
dir: DirectiveNode,
|
|
|
|
context: TransformContext
|
2019-09-24 01:25:18 +08:00
|
|
|
) => void | (() => void)
|
2019-09-18 07:08:47 +08:00
|
|
|
|
|
|
|
export interface TransformOptions {
|
2019-09-22 05:42:12 +08:00
|
|
|
nodeTransforms?: NodeTransform[]
|
|
|
|
directiveTransforms?: { [name: string]: DirectiveTransform }
|
2019-09-24 01:29:41 +08:00
|
|
|
prefixIdentifiers?: boolean
|
2019-10-04 21:03:00 +08:00
|
|
|
hoistStatic?: boolean
|
2019-09-18 07:08:47 +08:00
|
|
|
onError?: (error: CompilerError) => void
|
|
|
|
}
|
|
|
|
|
2019-09-22 05:42:12 +08:00
|
|
|
export interface TransformContext extends Required<TransformOptions> {
|
2019-09-25 03:49:02 +08:00
|
|
|
root: RootNode
|
2019-10-06 05:18:25 +08:00
|
|
|
helpers: Set<RuntimeHelper>
|
|
|
|
components: Set<string>
|
|
|
|
directives: Set<string>
|
2019-09-26 02:13:33 +08:00
|
|
|
hoists: JSChildNode[]
|
2019-09-24 01:56:56 +08:00
|
|
|
identifiers: { [name: string]: number | undefined }
|
2019-10-04 04:27:46 +08:00
|
|
|
scopes: {
|
|
|
|
vFor: number
|
|
|
|
vSlot: number
|
|
|
|
vPre: number
|
|
|
|
vOnce: number
|
|
|
|
}
|
2019-10-01 02:51:55 +08:00
|
|
|
parent: ParentNode | null
|
2019-09-18 07:08:47 +08:00
|
|
|
childIndex: number
|
2019-10-01 21:27:34 +08:00
|
|
|
currentNode: RootNode | TemplateChildNode | null
|
2019-10-06 05:18:25 +08:00
|
|
|
helper<T extends RuntimeHelper>(name: T): T
|
|
|
|
helperString(name: RuntimeHelper): string
|
2019-10-01 21:27:34 +08:00
|
|
|
replaceNode(node: TemplateChildNode): void
|
|
|
|
removeNode(node?: TemplateChildNode): void
|
2019-09-20 03:41:17 +08:00
|
|
|
onNodeRemoved: () => void
|
2019-10-04 05:47:00 +08:00
|
|
|
addIdentifiers(exp: ExpressionNode | string): void
|
|
|
|
removeIdentifiers(exp: ExpressionNode | string): void
|
2019-10-01 09:17:12 +08:00
|
|
|
hoist(exp: JSChildNode): SimpleExpressionNode
|
2019-09-18 07:08:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function createTransformContext(
|
|
|
|
root: RootNode,
|
2019-09-24 01:25:18 +08:00
|
|
|
{
|
2019-09-24 01:29:41 +08:00
|
|
|
prefixIdentifiers = false,
|
2019-10-04 21:03:00 +08:00
|
|
|
hoistStatic = false,
|
2019-09-24 01:25:18 +08:00
|
|
|
nodeTransforms = [],
|
|
|
|
directiveTransforms = {},
|
|
|
|
onError = defaultOnError
|
|
|
|
}: TransformOptions
|
2019-09-18 07:08:47 +08:00
|
|
|
): TransformContext {
|
|
|
|
const context: TransformContext = {
|
2019-09-25 03:49:02 +08:00
|
|
|
root,
|
2019-10-06 05:18:25 +08:00
|
|
|
helpers: new Set(),
|
|
|
|
components: new Set(),
|
|
|
|
directives: new Set(),
|
2019-09-26 02:13:33 +08:00
|
|
|
hoists: [],
|
2019-09-23 11:07:36 +08:00
|
|
|
identifiers: {},
|
2019-10-04 04:27:46 +08:00
|
|
|
scopes: {
|
|
|
|
vFor: 0,
|
|
|
|
vSlot: 0,
|
|
|
|
vPre: 0,
|
|
|
|
vOnce: 0
|
|
|
|
},
|
2019-09-24 01:29:41 +08:00
|
|
|
prefixIdentifiers,
|
2019-10-04 21:03:00 +08:00
|
|
|
hoistStatic,
|
2019-09-24 01:25:18 +08:00
|
|
|
nodeTransforms,
|
|
|
|
directiveTransforms,
|
|
|
|
onError,
|
2019-10-01 02:51:55 +08:00
|
|
|
parent: null,
|
|
|
|
currentNode: root,
|
2019-09-18 07:08:47 +08:00
|
|
|
childIndex: 0,
|
2019-09-26 07:17:45 +08:00
|
|
|
helper(name) {
|
2019-10-06 05:18:25 +08:00
|
|
|
context.helpers.add(name)
|
|
|
|
return name
|
|
|
|
},
|
|
|
|
helperString(name) {
|
|
|
|
return (
|
|
|
|
(context.prefixIdentifiers ? `` : `_`) +
|
|
|
|
helperNameMap[context.helper(name)]
|
|
|
|
)
|
2019-09-26 07:17:45 +08:00
|
|
|
},
|
2019-09-18 07:08:47 +08:00
|
|
|
replaceNode(node) {
|
2019-09-25 04:35:01 +08:00
|
|
|
/* istanbul ignore if */
|
2019-10-01 02:51:55 +08:00
|
|
|
if (__DEV__) {
|
|
|
|
if (!context.currentNode) {
|
|
|
|
throw new Error(`Node being replaced is already removed.`)
|
|
|
|
}
|
|
|
|
if (!context.parent) {
|
|
|
|
throw new Error(`Cannot replace root node.`)
|
|
|
|
}
|
2019-09-20 00:20:59 +08:00
|
|
|
}
|
2019-10-01 02:51:55 +08:00
|
|
|
context.parent!.children[context.childIndex] = context.currentNode = node
|
2019-09-18 07:08:47 +08:00
|
|
|
},
|
2019-09-20 03:41:17 +08:00
|
|
|
removeNode(node) {
|
2019-10-01 02:51:55 +08:00
|
|
|
if (__DEV__ && !context.parent) {
|
|
|
|
throw new Error(`Cannot remove root node.`)
|
|
|
|
}
|
|
|
|
const list = context.parent!.children
|
2019-09-20 03:41:17 +08:00
|
|
|
const removalIndex = node
|
2019-10-08 22:50:00 +08:00
|
|
|
? list.indexOf(node)
|
2019-09-20 03:41:17 +08:00
|
|
|
: context.currentNode
|
|
|
|
? context.childIndex
|
|
|
|
: -1
|
2019-09-25 04:35:01 +08:00
|
|
|
/* istanbul ignore if */
|
2019-09-20 03:41:17 +08:00
|
|
|
if (__DEV__ && removalIndex < 0) {
|
|
|
|
throw new Error(`node being removed is not a child of current parent`)
|
|
|
|
}
|
|
|
|
if (!node || node === context.currentNode) {
|
|
|
|
// current node removed
|
|
|
|
context.currentNode = null
|
|
|
|
context.onNodeRemoved()
|
|
|
|
} else {
|
|
|
|
// sibling node removed
|
|
|
|
if (context.childIndex > removalIndex) {
|
|
|
|
context.childIndex--
|
|
|
|
context.onNodeRemoved()
|
|
|
|
}
|
|
|
|
}
|
2019-10-01 02:51:55 +08:00
|
|
|
context.parent!.children.splice(removalIndex, 1)
|
2019-09-18 07:08:47 +08:00
|
|
|
},
|
2019-09-24 01:25:18 +08:00
|
|
|
onNodeRemoved: () => {},
|
2019-09-29 04:02:08 +08:00
|
|
|
addIdentifiers(exp) {
|
|
|
|
// identifier tracking only happens in non-browser builds.
|
|
|
|
if (!__BROWSER__) {
|
2019-10-04 05:47:00 +08:00
|
|
|
if (isString(exp)) {
|
|
|
|
addId(exp)
|
|
|
|
} else if (exp.identifiers) {
|
2019-09-29 04:02:08 +08:00
|
|
|
exp.identifiers.forEach(addId)
|
|
|
|
} else if (exp.type === NodeTypes.SIMPLE_EXPRESSION) {
|
|
|
|
addId(exp.content)
|
|
|
|
}
|
2019-09-24 01:56:56 +08:00
|
|
|
}
|
2019-09-24 01:25:18 +08:00
|
|
|
},
|
2019-09-29 04:02:08 +08:00
|
|
|
removeIdentifiers(exp) {
|
|
|
|
if (!__BROWSER__) {
|
2019-10-04 05:47:00 +08:00
|
|
|
if (isString(exp)) {
|
|
|
|
removeId(exp)
|
|
|
|
} else if (exp.identifiers) {
|
2019-09-29 04:02:08 +08:00
|
|
|
exp.identifiers.forEach(removeId)
|
|
|
|
} else if (exp.type === NodeTypes.SIMPLE_EXPRESSION) {
|
|
|
|
removeId(exp.content)
|
|
|
|
}
|
|
|
|
}
|
2019-09-26 02:13:33 +08:00
|
|
|
},
|
|
|
|
hoist(exp) {
|
|
|
|
context.hoists.push(exp)
|
2019-09-27 23:42:02 +08:00
|
|
|
return createSimpleExpression(
|
2019-09-26 02:13:33 +08:00
|
|
|
`_hoisted_${context.hoists.length}`,
|
|
|
|
false,
|
|
|
|
exp.loc
|
|
|
|
)
|
2019-09-24 01:25:18 +08:00
|
|
|
}
|
2019-09-18 07:08:47 +08:00
|
|
|
}
|
2019-09-29 04:02:08 +08:00
|
|
|
|
|
|
|
function addId(id: string) {
|
|
|
|
const { identifiers } = context
|
|
|
|
if (identifiers[id] === undefined) {
|
|
|
|
identifiers[id] = 0
|
|
|
|
}
|
2019-10-05 22:38:02 +08:00
|
|
|
identifiers[id]!++
|
2019-09-29 04:02:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeId(id: string) {
|
2019-10-05 22:38:02 +08:00
|
|
|
context.identifiers[id]!--
|
2019-09-29 04:02:08 +08:00
|
|
|
}
|
|
|
|
|
2019-09-18 07:08:47 +08:00
|
|
|
return context
|
|
|
|
}
|
|
|
|
|
2019-09-23 11:07:36 +08:00
|
|
|
export function transform(root: RootNode, options: TransformOptions) {
|
|
|
|
const context = createTransformContext(root, options)
|
2019-10-01 02:51:55 +08:00
|
|
|
traverseNode(root, context)
|
2019-10-04 21:03:00 +08:00
|
|
|
if (options.hoistStatic) {
|
|
|
|
hoistStatic(root, context)
|
2019-10-04 11:30:25 +08:00
|
|
|
}
|
2019-10-03 01:11:07 +08:00
|
|
|
finalizeRoot(root, context)
|
|
|
|
}
|
|
|
|
|
|
|
|
function finalizeRoot(root: RootNode, context: TransformContext) {
|
|
|
|
const { helper } = context
|
|
|
|
const { children } = root
|
2019-10-08 05:12:22 +08:00
|
|
|
const child = children[0]
|
2019-10-08 22:50:00 +08:00
|
|
|
if (children.length === 1) {
|
|
|
|
// if the single child is an element, turn it into a block.
|
|
|
|
if (isSingleElementRoot(root, child) && child.codegenNode) {
|
|
|
|
// single element root is never hoisted so codegenNode will never be
|
|
|
|
// SimpleExpressionNode
|
|
|
|
const codegenNode = child.codegenNode as
|
|
|
|
| ElementCodegenNode
|
|
|
|
| ComponentCodegenNode
|
|
|
|
if (codegenNode.callee === APPLY_DIRECTIVES) {
|
|
|
|
codegenNode.arguments[0].callee = helper(CREATE_BLOCK)
|
|
|
|
} else {
|
|
|
|
codegenNode.callee = helper(CREATE_BLOCK)
|
|
|
|
}
|
|
|
|
root.codegenNode = createBlockExpression(codegenNode, context)
|
|
|
|
} else {
|
|
|
|
// - single <slot/>, IfNode, ForNode: already blocks.
|
|
|
|
// - single text node: always patched.
|
|
|
|
// root codegen falls through via genNode()
|
|
|
|
root.codegenNode = child
|
|
|
|
}
|
2019-10-03 01:11:07 +08:00
|
|
|
} else if (children.length > 1) {
|
|
|
|
// root has multiple nodes - return a fragment block.
|
|
|
|
root.codegenNode = createBlockExpression(
|
2019-10-08 22:50:00 +08:00
|
|
|
createCallExpression(helper(CREATE_BLOCK), [
|
|
|
|
helper(FRAGMENT),
|
|
|
|
`null`,
|
|
|
|
root.children
|
|
|
|
]),
|
2019-10-03 01:11:07 +08:00
|
|
|
context
|
|
|
|
)
|
2019-10-08 22:50:00 +08:00
|
|
|
} else {
|
|
|
|
// no children = noop. codegen will return null.
|
2019-10-03 01:11:07 +08:00
|
|
|
}
|
|
|
|
// finalize meta information
|
2019-10-06 05:18:25 +08:00
|
|
|
root.helpers = [...context.helpers]
|
|
|
|
root.components = [...context.components]
|
|
|
|
root.directives = [...context.directives]
|
2019-09-26 02:13:33 +08:00
|
|
|
root.hoists = context.hoists
|
2019-09-23 11:07:36 +08:00
|
|
|
}
|
|
|
|
|
2019-09-23 08:55:18 +08:00
|
|
|
export function traverseChildren(
|
2019-09-18 07:08:47 +08:00
|
|
|
parent: ParentNode,
|
2019-09-23 08:55:18 +08:00
|
|
|
context: TransformContext
|
2019-09-18 07:08:47 +08:00
|
|
|
) {
|
2019-09-20 03:41:17 +08:00
|
|
|
let i = 0
|
|
|
|
const nodeRemoved = () => {
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
for (; i < parent.children.length; i++) {
|
2019-09-23 14:52:54 +08:00
|
|
|
const child = parent.children[i]
|
|
|
|
if (isString(child)) continue
|
|
|
|
context.currentNode = child
|
2019-09-18 07:08:47 +08:00
|
|
|
context.parent = parent
|
|
|
|
context.childIndex = i
|
2019-09-20 03:41:17 +08:00
|
|
|
context.onNodeRemoved = nodeRemoved
|
2019-09-23 14:52:54 +08:00
|
|
|
traverseNode(child, context)
|
2019-09-18 07:08:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-01 02:51:55 +08:00
|
|
|
export function traverseNode(
|
2019-10-01 21:27:34 +08:00
|
|
|
node: RootNode | TemplateChildNode,
|
2019-10-01 02:51:55 +08:00
|
|
|
context: TransformContext
|
|
|
|
) {
|
2019-09-18 07:08:47 +08:00
|
|
|
// apply transform plugins
|
2019-09-22 05:42:12 +08:00
|
|
|
const { nodeTransforms } = context
|
2019-09-24 01:25:18 +08:00
|
|
|
const exitFns = []
|
2019-09-22 05:42:12 +08:00
|
|
|
for (let i = 0; i < nodeTransforms.length; i++) {
|
2019-10-03 01:11:07 +08:00
|
|
|
const onExit = nodeTransforms[i](node, context)
|
2019-09-24 01:25:18 +08:00
|
|
|
if (onExit) {
|
|
|
|
if (isArray(onExit)) {
|
|
|
|
exitFns.push(...onExit)
|
|
|
|
} else {
|
|
|
|
exitFns.push(onExit)
|
|
|
|
}
|
|
|
|
}
|
2019-09-23 08:55:18 +08:00
|
|
|
if (!context.currentNode) {
|
|
|
|
// node was removed
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
// node may have been replaced
|
|
|
|
node = context.currentNode
|
|
|
|
}
|
2019-09-18 07:08:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (node.type) {
|
2019-09-25 03:49:02 +08:00
|
|
|
case NodeTypes.COMMENT:
|
|
|
|
// inject import for the Comment symbol, which is needed for creating
|
|
|
|
// comment nodes with `createVNode`
|
2019-10-03 01:11:07 +08:00
|
|
|
context.helper(CREATE_VNODE)
|
2019-09-26 07:17:45 +08:00
|
|
|
context.helper(COMMENT)
|
2019-09-25 03:49:02 +08:00
|
|
|
break
|
2019-09-27 23:42:02 +08:00
|
|
|
case NodeTypes.INTERPOLATION:
|
2019-09-24 03:36:30 +08:00
|
|
|
// no need to traverse, but we need to inject toString helper
|
2019-09-27 23:42:02 +08:00
|
|
|
context.helper(TO_STRING)
|
2019-09-24 03:36:30 +08:00
|
|
|
break
|
|
|
|
|
|
|
|
// for container types, further traverse downwards
|
2019-09-18 07:08:47 +08:00
|
|
|
case NodeTypes.IF:
|
|
|
|
for (let i = 0; i < node.branches.length; i++) {
|
2019-09-23 08:55:18 +08:00
|
|
|
traverseChildren(node.branches[i], context)
|
2019-09-18 07:08:47 +08:00
|
|
|
}
|
|
|
|
break
|
|
|
|
case NodeTypes.FOR:
|
|
|
|
case NodeTypes.ELEMENT:
|
2019-10-01 02:51:55 +08:00
|
|
|
case NodeTypes.ROOT:
|
2019-09-23 08:55:18 +08:00
|
|
|
traverseChildren(node, context)
|
2019-09-18 07:08:47 +08:00
|
|
|
break
|
|
|
|
}
|
2019-09-24 01:25:18 +08:00
|
|
|
|
|
|
|
// exit transforms
|
|
|
|
for (let i = 0; i < exitFns.length; i++) {
|
|
|
|
exitFns[i]()
|
|
|
|
}
|
2019-09-18 07:08:47 +08:00
|
|
|
}
|
|
|
|
|
2019-09-22 05:42:12 +08:00
|
|
|
export function createStructuralDirectiveTransform(
|
2019-09-18 07:08:47 +08:00
|
|
|
name: string | RegExp,
|
2019-09-22 05:42:12 +08:00
|
|
|
fn: StructuralDirectiveTransform
|
|
|
|
): NodeTransform {
|
2019-09-18 07:08:47 +08:00
|
|
|
const matches = isString(name)
|
|
|
|
? (n: string) => n === name
|
|
|
|
: (n: string) => name.test(n)
|
|
|
|
|
|
|
|
return (node, context) => {
|
|
|
|
if (node.type === NodeTypes.ELEMENT) {
|
2019-09-22 05:42:12 +08:00
|
|
|
const { props } = node
|
2019-10-03 05:18:11 +08:00
|
|
|
// structural directive transforms are not concerned with slots
|
|
|
|
// as they are handled separately in vSlot.ts
|
|
|
|
if (node.tagType === ElementTypes.TEMPLATE && props.some(isVSlot)) {
|
|
|
|
return
|
|
|
|
}
|
2019-09-24 01:25:18 +08:00
|
|
|
const exitFns = []
|
2019-09-22 05:42:12 +08:00
|
|
|
for (let i = 0; i < props.length; i++) {
|
|
|
|
const prop = props[i]
|
|
|
|
if (prop.type === NodeTypes.DIRECTIVE && matches(prop.name)) {
|
2019-09-23 08:55:18 +08:00
|
|
|
// structural directives are removed to avoid infinite recursion
|
|
|
|
// also we remove them *before* applying so that it can further
|
|
|
|
// traverse itself in case it moves the node around
|
2019-09-22 05:42:12 +08:00
|
|
|
props.splice(i, 1)
|
|
|
|
i--
|
2019-09-24 01:25:18 +08:00
|
|
|
const onExit = fn(node, prop, context)
|
|
|
|
if (onExit) exitFns.push(onExit)
|
2019-09-18 07:08:47 +08:00
|
|
|
}
|
|
|
|
}
|
2019-09-24 01:25:18 +08:00
|
|
|
return exitFns
|
2019-09-18 07:08:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|