Skip to content

Commit 0562548

Browse files
authored
fix(compiler-sfc): throw mismatched script langs error before invoking babel (#13194)
Close #13193
1 parent d7283f3 commit 0562548

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,13 @@ describe('SFC compile <script setup>', () => {
913913
expect(() =>
914914
compile(`<script>foo()</script><script setup lang="ts">bar()</script>`),
915915
).toThrow(`<script> and <script setup> must have the same language type`)
916+
917+
// #13193 must check lang before parsing with babel
918+
expect(() =>
919+
compile(
920+
`<script lang="ts">const a = 1</script><script setup lang="tsx">const Comp = () => <p>test</p></script>`,
921+
),
922+
).toThrow(`<script> and <script setup> must have the same language type`)
916923
})
917924

918925
const moduleErrorMsg = `cannot contain ES module exports`

packages/compiler-sfc/src/compileScript.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,21 @@ export function compileScript(
168168
)
169169
}
170170

171-
const ctx = new ScriptCompileContext(sfc, options)
172171
const { script, scriptSetup, source, filename } = sfc
173172
const hoistStatic = options.hoistStatic !== false && !script
174173
const scopeId = options.id ? options.id.replace(/^data-v-/, '') : ''
175174
const scriptLang = script && script.lang
176175
const scriptSetupLang = scriptSetup && scriptSetup.lang
177176

177+
if (script && scriptSetup && scriptLang !== scriptSetupLang) {
178+
throw new Error(
179+
`[@vue/compiler-sfc] <script> and <script setup> must have the same ` +
180+
`language type.`,
181+
)
182+
}
183+
184+
const ctx = new ScriptCompileContext(sfc, options)
185+
178186
if (!scriptSetup) {
179187
if (!script) {
180188
throw new Error(`[@vue/compiler-sfc] SFC contains no <script> tags.`)
@@ -183,13 +191,6 @@ export function compileScript(
183191
return processNormalScript(ctx, scopeId)
184192
}
185193

186-
if (script && scriptLang !== scriptSetupLang) {
187-
throw new Error(
188-
`[@vue/compiler-sfc] <script> and <script setup> must have the same ` +
189-
`language type.`,
190-
)
191-
}
192-
193194
if (scriptSetupLang && !ctx.isJS && !ctx.isTS) {
194195
// do not process non js/ts script blocks
195196
return scriptSetup

0 commit comments

Comments
 (0)