wip(compiler): tweak codegen, avoid duplicated asset resolution, improve formatting

This commit is contained in:
Evan You 2019-09-27 22:49:20 -04:00
parent 32666c7708
commit 262be6733c
3 changed files with 18 additions and 8 deletions

View File

@ -296,7 +296,13 @@ function genNodeListAsArray(
nodes: (string | CodegenNode | ChildNode[])[], nodes: (string | CodegenNode | ChildNode[])[],
context: CodegenContext context: CodegenContext
) { ) {
const multilines = nodes.length > 1 const multilines =
nodes.length > 1 ||
((!__BROWSER__ || __DEV__) &&
nodes.some(
n =>
isArray(n) || (!isString(n) && n.type !== NodeTypes.SIMPLE_EXPRESSION)
))
context.push(`[`) context.push(`[`)
multilines && context.indent() multilines && context.indent()
genNodeList(nodes, context, multilines) genNodeList(nodes, context, multilines)
@ -552,7 +558,10 @@ function genCallExpression(
function genObjectExpression(node: ObjectExpression, context: CodegenContext) { function genObjectExpression(node: ObjectExpression, context: CodegenContext) {
const { push, indent, deindent, newline, resetMapping } = context const { push, indent, deindent, newline, resetMapping } = context
const { properties } = node const { properties } = node
const multilines = properties.length > 1 const multilines =
properties.length > 1 ||
((!__BROWSER__ || __DEV__) &&
properties.some(p => p.value.type !== NodeTypes.SIMPLE_EXPRESSION))
push(multilines ? `{` : `{ `) push(multilines ? `{` : `{ `)
multilines && indent() multilines && indent()
for (let i = 0; i < properties.length; i++) { for (let i = 0; i < properties.length; i++) {
@ -570,7 +579,8 @@ function genObjectExpression(node: ObjectExpression, context: CodegenContext) {
} }
} }
multilines && deindent() multilines && deindent()
push(multilines ? `}` : ` }`) const lastChar = context.code[context.code.length - 1]
push(multilines || /[\])}]/.test(lastChar) ? `}` : ` }`)
} }
function genArrayExpression(node: ArrayExpression, context: CodegenContext) { function genArrayExpression(node: ArrayExpression, context: CodegenContext) {

View File

@ -54,7 +54,7 @@ export interface TransformOptions {
export interface TransformContext extends Required<TransformOptions> { export interface TransformContext extends Required<TransformOptions> {
root: RootNode root: RootNode
imports: Set<string> imports: Set<string>
statements: string[] statements: Set<string>
hoists: JSChildNode[] hoists: JSChildNode[]
identifiers: { [name: string]: number | undefined } identifiers: { [name: string]: number | undefined }
parent: ParentNode parent: ParentNode
@ -81,7 +81,7 @@ function createTransformContext(
const context: TransformContext = { const context: TransformContext = {
root, root,
imports: new Set(), imports: new Set(),
statements: [], statements: new Set(),
hoists: [], hoists: [],
identifiers: {}, identifiers: {},
prefixIdentifiers, prefixIdentifiers,
@ -153,7 +153,7 @@ export function transform(root: RootNode, options: TransformOptions) {
const context = createTransformContext(root, options) const context = createTransformContext(root, options)
traverseChildren(root, context) traverseChildren(root, context)
root.imports = [...context.imports] root.imports = [...context.imports]
root.statements = context.statements root.statements = [...context.statements]
root.hoists = context.hoists root.hoists = context.hoists
} }

View File

@ -46,7 +46,7 @@ export const transformElement: NodeTransform = (node, context) => {
if (isComponent) { if (isComponent) {
componentIdentifier = `_component_${toValidId(node.tag)}` componentIdentifier = `_component_${toValidId(node.tag)}`
context.statements.push( context.statements.add(
`const ${componentIdentifier} = ${context.helper( `const ${componentIdentifier} = ${context.helper(
RESOLVE_COMPONENT RESOLVE_COMPONENT
)}(${JSON.stringify(node.tag)})` )}(${JSON.stringify(node.tag)})`
@ -294,7 +294,7 @@ function createDirectiveArgs(
): ArrayExpression { ): ArrayExpression {
// inject statement for resolving directive // inject statement for resolving directive
const dirIdentifier = `_directive_${toValidId(dir.name)}` const dirIdentifier = `_directive_${toValidId(dir.name)}`
context.statements.push( context.statements.add(
`const ${dirIdentifier} = ${context.helper( `const ${dirIdentifier} = ${context.helper(
RESOLVE_DIRECTIVE RESOLVE_DIRECTIVE
)}(${JSON.stringify(dir.name)})` )}(${JSON.stringify(dir.name)})`