mirror of https://github.com/vuejs/core.git
fix(compiler-sfc): allow `<script>` with lang='js' (#7398)
This commit is contained in:
parent
4355d2492d
commit
9f5e20ccff
|
@ -1277,6 +1277,21 @@ return () => {}
|
|||
}"
|
||||
`;
|
||||
|
||||
exports[`SFC compile <script setup> > should compile JS syntax 1`] = `
|
||||
"const a = 1
|
||||
const b = 2
|
||||
|
||||
export default {
|
||||
setup(__props, { expose }) {
|
||||
expose();
|
||||
|
||||
|
||||
return { a, b }
|
||||
}
|
||||
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`SFC compile <script setup> > should expose top level declarations 1`] = `
|
||||
"import { x } from './x'
|
||||
|
||||
|
|
|
@ -2,6 +2,17 @@ import { BindingTypes } from '@vue/compiler-core'
|
|||
import { compileSFCScript as compile, assertCode, mockId } from './utils'
|
||||
|
||||
describe('SFC compile <script setup>', () => {
|
||||
test('should compile JS syntax', () => {
|
||||
const { content } = compile(`
|
||||
<script setup lang='js'>
|
||||
const a = 1
|
||||
const b = 2
|
||||
</script>
|
||||
`)
|
||||
expect(content).toMatch(`return { a, b }`)
|
||||
assertCode(content)
|
||||
})
|
||||
|
||||
test('should expose top level declarations', () => {
|
||||
const { content, bindings } = compile(`
|
||||
<script setup>
|
||||
|
|
|
@ -167,6 +167,11 @@ export function compileScript(
|
|||
const cssVars = sfc.cssVars
|
||||
const scriptLang = script && script.lang
|
||||
const scriptSetupLang = scriptSetup && scriptSetup.lang
|
||||
const isJS =
|
||||
scriptLang === 'js' ||
|
||||
scriptLang === 'jsx' ||
|
||||
scriptSetupLang === 'js' ||
|
||||
scriptSetupLang === 'jsx'
|
||||
const isTS =
|
||||
scriptLang === 'ts' ||
|
||||
scriptLang === 'tsx' ||
|
||||
|
@ -196,7 +201,7 @@ export function compileScript(
|
|||
if (!script) {
|
||||
throw new Error(`[@vue/compiler-sfc] SFC contains no <script> tags.`)
|
||||
}
|
||||
if (scriptLang && !isTS && scriptLang !== 'jsx') {
|
||||
if (scriptLang && !isJS && !isTS) {
|
||||
// do not process non js/ts script blocks
|
||||
return script
|
||||
}
|
||||
|
@ -264,7 +269,7 @@ export function compileScript(
|
|||
)
|
||||
}
|
||||
|
||||
if (scriptSetupLang && !isTS && scriptSetupLang !== 'jsx') {
|
||||
if (scriptSetupLang && !isJS && !isTS) {
|
||||
// do not process non js/ts script blocks
|
||||
return scriptSetup
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue