Skip to content

Commit 9f5e20c

Browse files
authored
fix(compiler-sfc): allow <script> with lang='js' (#7398)
1 parent 4355d24 commit 9f5e20c

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,21 @@ return () => {}
12771277
}"
12781278
`;
12791279

1280+
exports[`SFC compile <script setup> > should compile JS syntax 1`] = `
1281+
"const a = 1
1282+
const b = 2
1283+
1284+
export default {
1285+
setup(__props, { expose }) {
1286+
expose();
1287+
1288+
1289+
return { a, b }
1290+
}
1291+
1292+
}"
1293+
`;
1294+
12801295
exports[`SFC compile <script setup> > should expose top level declarations 1`] = `
12811296
"import { x } from './x'
12821297

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ import { BindingTypes } from '@vue/compiler-core'
22
import { compileSFCScript as compile, assertCode, mockId } from './utils'
33

44
describe('SFC compile <script setup>', () => {
5+
test('should compile JS syntax', () => {
6+
const { content } = compile(`
7+
<script setup lang='js'>
8+
const a = 1
9+
const b = 2
10+
</script>
11+
`)
12+
expect(content).toMatch(`return { a, b }`)
13+
assertCode(content)
14+
})
15+
516
test('should expose top level declarations', () => {
617
const { content, bindings } = compile(`
718
<script setup>

packages/compiler-sfc/src/compileScript.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ export function compileScript(
167167
const cssVars = sfc.cssVars
168168
const scriptLang = script && script.lang
169169
const scriptSetupLang = scriptSetup && scriptSetup.lang
170+
const isJS =
171+
scriptLang === 'js' ||
172+
scriptLang === 'jsx' ||
173+
scriptSetupLang === 'js' ||
174+
scriptSetupLang === 'jsx'
170175
const isTS =
171176
scriptLang === 'ts' ||
172177
scriptLang === 'tsx' ||
@@ -196,7 +201,7 @@ export function compileScript(
196201
if (!script) {
197202
throw new Error(`[@vue/compiler-sfc] SFC contains no <script> tags.`)
198203
}
199-
if (scriptLang && !isTS && scriptLang !== 'jsx') {
204+
if (scriptLang && !isJS && !isTS) {
200205
// do not process non js/ts script blocks
201206
return script
202207
}
@@ -264,7 +269,7 @@ export function compileScript(
264269
)
265270
}
266271

267-
if (scriptSetupLang && !isTS && scriptSetupLang !== 'jsx') {
272+
if (scriptSetupLang && !isJS && !isTS) {
268273
// do not process non js/ts script blocks
269274
return scriptSetup
270275
}

0 commit comments

Comments
 (0)