Browse Source

fix(compiler-core): fix parsing error on comments between v-if in prod

close #6843
Evan You 3 years ago
parent
commit
dd3354c4c7

+ 21 - 0
packages/compiler-core/__tests__/transforms/vIf.spec.ts

@@ -712,6 +712,27 @@ describe('compiler: v-if', () => {
       expect(b1.children[3].type).toBe(NodeTypes.ELEMENT)
       expect((b1.children[3] as ElementNode).tag).toBe(`p`)
     })
+
+    // #6843
+    test('should parse correctly with comments: true in prod', () => {
+      __DEV__ = false
+      parseWithIfTransform(
+        `
+          <template v-if="ok">
+            <!--comment1-->
+            <div v-if="ok2">
+              <!--comment2-->
+            </div>
+            <!--comment3-->
+            <b v-else/>
+            <!--comment4-->
+            <p/>
+          </template>
+        `,
+        { comments: true }
+      )
+      __DEV__ = true
+    })
   })
 
   test('v-on with v-if', () => {

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

@@ -129,9 +129,9 @@ export function processIf(
     let i = siblings.indexOf(node)
     while (i-- >= -1) {
       const sibling = siblings[i]
-      if (__DEV__ && sibling && sibling.type === NodeTypes.COMMENT) {
+      if (sibling && sibling.type === NodeTypes.COMMENT) {
         context.removeNode(sibling)
-        comments.unshift(sibling)
+        __DEV__ && comments.unshift(sibling)
         continue
       }