diff --git a/packages/compiler-sfc/__tests__/parse.spec.ts b/packages/compiler-sfc/__tests__/parse.spec.ts
index a55e2e6b288..fcb59061aeb 100644
--- a/packages/compiler-sfc/__tests__/parse.spec.ts
+++ b/packages/compiler-sfc/__tests__/parse.spec.ts
@@ -73,6 +73,13 @@ h1 { color: red }
expect(parse(``).descriptor.customBlocks.length).toBe(0)
})
+ test('handle empty nodes with src attribute', () => {
+ const { descriptor } = parse(``)
+ expect(descriptor.script).toBeTruthy()
+ expect(descriptor.script!.content).toBeFalsy()
+ expect(descriptor.script!.attrs['src']).toBe('com')
+ })
+
test('nested templates', () => {
const content = `
ok
diff --git a/packages/compiler-sfc/src/parse.ts b/packages/compiler-sfc/src/parse.ts
index 1953027c903..da3753643f3 100644
--- a/packages/compiler-sfc/src/parse.ts
+++ b/packages/compiler-sfc/src/parse.ts
@@ -108,7 +108,7 @@ export function parse(
if (node.type !== NodeTypes.ELEMENT) {
return
}
- if (!node.children.length) {
+ if (!node.children.length && !hasSrc(node)) {
return
}
switch (node.tag) {
@@ -188,9 +188,13 @@ function createBlock(
pad: SFCParseOptions['pad']
): SFCBlock {
const type = node.tag
- const start = node.children[0].loc.start
- const end = node.children[node.children.length - 1].loc.end
- const content = source.slice(start.offset, end.offset)
+ let { start, end } = node.loc
+ let content = ''
+ if (node.children.length) {
+ start = node.children[0].loc.start
+ end = node.children[node.children.length - 1].loc.end
+ content = source.slice(start.offset, end.offset)
+ }
const loc = {
source: content,
start,
@@ -275,3 +279,12 @@ function padContent(
return Array(offset).join(padChar)
}
}
+
+function hasSrc(node: ElementNode) {
+ return node.props.some(p => {
+ if (p.type !== NodeTypes.ATTRIBUTE) {
+ return false
+ }
+ return p.name === 'src'
+ })
+}