Skip to content

Commit a0c7f27

Browse files
authored
fix(compiler-sfc): require <template> or <script> in SFC (#6781)
fix #6676
1 parent 9768949 commit a0c7f27

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

packages/compiler-sfc/__tests__/parse.spec.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,9 @@ h1 { color: red }
268268
})
269269

270270
test('treat custom blocks as raw text', () => {
271-
const { errors, descriptor } = parse(`<foo> <-& </foo>`)
271+
const { errors, descriptor } = parse(
272+
`<template><input></template><foo> <-& </foo>`
273+
)
272274
expect(errors.length).toBe(0)
273275
expect(descriptor.customBlocks[0].content).toBe(` <-& `)
274276
})
@@ -309,5 +311,13 @@ h1 { color: red }
309311
).errors.length
310312
).toBe(0)
311313
})
314+
315+
// # 6676
316+
test('should throw error if no <template> or <script> is present', () => {
317+
assertWarning(
318+
parse(`import { ref } from 'vue'`).errors,
319+
`At least one <template> or <script> is required in a single file component`
320+
)
321+
})
312322
})
313323
})

packages/compiler-sfc/src/parse.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ export function parse(
149149
errors.push(e)
150150
}
151151
})
152-
153152
ast.children.forEach(node => {
154153
if (node.type !== NodeTypes.ELEMENT) {
155154
return
@@ -218,7 +217,13 @@ export function parse(
218217
break
219218
}
220219
})
221-
220+
if (!descriptor.template && !descriptor.script && !descriptor.scriptSetup) {
221+
errors.push(
222+
new SyntaxError(
223+
`At least one <template> or <script> is required in a single file component.`
224+
)
225+
)
226+
}
222227
if (descriptor.scriptSetup) {
223228
if (descriptor.scriptSetup.src) {
224229
errors.push(

0 commit comments

Comments
 (0)