瀏覽代碼

fix(compiler-sfc): should keep template nodes with no content (#2468)

close #2463
meteorlxy 5 年之前
父節點
當前提交
5b9b37fc9b
共有 2 個文件被更改,包括 8 次插入3 次删除
  1. 7 2
      packages/compiler-sfc/__tests__/parse.spec.ts
  2. 1 1
      packages/compiler-sfc/src/parse.ts

+ 7 - 2
packages/compiler-sfc/__tests__/parse.spec.ts

@@ -111,8 +111,13 @@ h1 { color: red }
     )
   })
 
-  test('should ignore nodes with no content', () => {
-    expect(parse(`<template/>`).descriptor.template).toBe(null)
+  test('should keep template nodes with no content', () => {
+    const { descriptor } = parse(`<template/>`)
+    expect(descriptor.template).toBeTruthy()
+    expect(descriptor.template!.content).toBeFalsy()
+  })
+
+  test('should ignore other nodes with no content', () => {
     expect(parse(`<script/>`).descriptor.script).toBe(null)
     expect(parse(`<style/>`).descriptor.styles.length).toBe(0)
     expect(parse(`<custom/>`).descriptor.customBlocks.length).toBe(0)

+ 1 - 1
packages/compiler-sfc/src/parse.ts

@@ -138,7 +138,7 @@ export function parse(
     if (node.type !== NodeTypes.ELEMENT) {
       return
     }
-    if (!node.children.length && !hasSrc(node)) {
+    if (!node.children.length && !hasSrc(node) && node.tag !== 'template') {
       return
     }
     switch (node.tag) {