Przeglądaj źródła

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

Evan You 6 lat temu
rodzic
commit
262be6733c

+ 13 - 3
packages/compiler-core/src/codegen.ts

@@ -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) {

+ 3 - 3
packages/compiler-core/src/transform.ts

@@ -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
 }
 

+ 2 - 2
packages/compiler-core/src/transforms/transformElement.ts

@@ -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)})`