Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-sfc): allow <script> with lang='js' #7398

Merged
merged 2 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
11 changes: 11 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
9 changes: 7 additions & 2 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,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' ||
Expand Down Expand Up @@ -195,7 +200,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
}
Expand Down Expand Up @@ -263,7 +268,7 @@ export function compileScript(
)
}

if (scriptSetupLang && !isTS && scriptSetupLang !== 'jsx') {
if (scriptSetupLang && !isJS && !isTS) {
// do not process non js/ts script blocks
return scriptSetup
}
Expand Down