Skip to content

Commit

Permalink
fix(compiler-sfc): require <template> or <script> in SFC (#6781)
Browse files Browse the repository at this point in the history
fix #6676
  • Loading branch information
花果山大圣 committed Nov 8, 2022
1 parent 9768949 commit a0c7f27
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
12 changes: 11 additions & 1 deletion packages/compiler-sfc/__tests__/parse.spec.ts
Expand Up @@ -268,7 +268,9 @@ h1 { color: red }
})

test('treat custom blocks as raw text', () => {
const { errors, descriptor } = parse(`<foo> <-& </foo>`)
const { errors, descriptor } = parse(
`<template><input></template><foo> <-& </foo>`
)
expect(errors.length).toBe(0)
expect(descriptor.customBlocks[0].content).toBe(` <-& `)
})
Expand Down Expand Up @@ -309,5 +311,13 @@ h1 { color: red }
).errors.length
).toBe(0)
})

// # 6676
test('should throw error if no <template> or <script> is present', () => {
assertWarning(
parse(`import { ref } from 'vue'`).errors,
`At least one <template> or <script> is required in a single file component`
)
})
})
})
9 changes: 7 additions & 2 deletions packages/compiler-sfc/src/parse.ts
Expand Up @@ -149,7 +149,6 @@ export function parse(
errors.push(e)
}
})

ast.children.forEach(node => {
if (node.type !== NodeTypes.ELEMENT) {
return
Expand Down Expand Up @@ -218,7 +217,13 @@ export function parse(
break
}
})

if (!descriptor.template && !descriptor.script && !descriptor.scriptSetup) {
errors.push(
new SyntaxError(
`At least one <template> or <script> is required in a single file component.`

This comment has been minimized.

Copy link
@guibwl

guibwl Nov 10, 2022

Is this necessary?

It seems incompatible with vite-plugin-pages

https://github.com/hannoeru/vite-plugin-pages#sfc-custom-block-for-route-data

image

)
)
}
if (descriptor.scriptSetup) {
if (descriptor.scriptSetup.src) {
errors.push(
Expand Down

0 comments on commit a0c7f27

Please sign in to comment.