]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
wip(compiler): tweak codegen, avoid duplicated asset resolution, improve formatting
authorEvan You <yyx990803@gmail.com>
Sat, 28 Sep 2019 02:49:20 +0000 (22:49 -0400)
committerEvan You <yyx990803@gmail.com>
Sat, 28 Sep 2019 02:49:20 +0000 (22:49 -0400)
packages/compiler-core/src/codegen.ts
packages/compiler-core/src/transform.ts
packages/compiler-core/src/transforms/transformElement.ts

index a2700bbf9fa7a22e96168e5c5a811bdc20f0c438..dbdc9efb57c5abe8cf0cf6f492647a8508d28ac9 100644 (file)
@@ -296,7 +296,13 @@ function genNodeListAsArray(
   nodes: (string | CodegenNode | ChildNode[])[],
   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(`[`)
   multilines && context.indent()
   genNodeList(nodes, context, multilines)
@@ -552,7 +558,10 @@ function genCallExpression(
 function genObjectExpression(node: ObjectExpression, context: CodegenContext) {
   const { push, indent, deindent, newline, resetMapping } = context
   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 ? `{` : `{ `)
   multilines && indent()
   for (let i = 0; i < properties.length; i++) {
@@ -570,7 +579,8 @@ function genObjectExpression(node: ObjectExpression, context: CodegenContext) {
     }
   }
   multilines && deindent()
-  push(multilines ? `}` : ` }`)
+  const lastChar = context.code[context.code.length - 1]
+  push(multilines || /[\])}]/.test(lastChar) ? `}` : ` }`)
 }
 
 function genArrayExpression(node: ArrayExpression, context: CodegenContext) {
index d5a45776155cb04f3fca5139187cb75fcb4dca5c..bf5fdf91706d6ba1e758b0a3ad105ad68c6fd6ec 100644 (file)
@@ -54,7 +54,7 @@ export interface TransformOptions {
 export interface TransformContext extends Required<TransformOptions> {
   root: RootNode
   imports: Set<string>
-  statements: string[]
+  statements: Set<string>
   hoists: JSChildNode[]
   identifiers: { [name: string]: number | undefined }
   parent: ParentNode
@@ -81,7 +81,7 @@ function createTransformContext(
   const context: TransformContext = {
     root,
     imports: new Set(),
-    statements: [],
+    statements: new Set(),
     hoists: [],
     identifiers: {},
     prefixIdentifiers,
@@ -153,7 +153,7 @@ export function transform(root: RootNode, options: TransformOptions) {
   const context = createTransformContext(root, options)
   traverseChildren(root, context)
   root.imports = [...context.imports]
-  root.statements = context.statements
+  root.statements = [...context.statements]
   root.hoists = context.hoists
 }
 
index afbd64b248d5c0c1e5de963505ca54f8a57d6f2a..dab39b438a76a7eb5d19a038b2e6cf12fefc42b4 100644 (file)
@@ -46,7 +46,7 @@ export const transformElement: NodeTransform = (node, context) => {
 
       if (isComponent) {
         componentIdentifier = `_component_${toValidId(node.tag)}`
-        context.statements.push(
+        context.statements.add(
           `const ${componentIdentifier} = ${context.helper(
             RESOLVE_COMPONENT
           )}(${JSON.stringify(node.tag)})`
@@ -294,7 +294,7 @@ function createDirectiveArgs(
 ): ArrayExpression {
   // inject statement for resolving directive
   const dirIdentifier = `_directive_${toValidId(dir.name)}`
-  context.statements.push(
+  context.statements.add(
     `const ${dirIdentifier} = ${context.helper(
       RESOLVE_DIRECTIVE
     )}(${JSON.stringify(dir.name)})`