Skip to content

Commit 755ce70

Browse files
committed
fix(compiler-sfc): throw mismatched script langs error before invoking babel
Previously, babel would throw a misleading error if <script> and <script setup> used different lang values. Now, the compiler checks for language mismatch first and throws a clear error before invoking babel Close vuejs#13193
1 parent 32bc647 commit 755ce70

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
@@ -886,6 +886,13 @@ describe('SFC compile <script setup>', () => {
886886
expect(() =>
887887
compile(`<script>foo()</script><script setup lang="ts">bar()</script>`),
888888
).toThrow(`<script> and <script setup> must have the same language type`)
889+
890+
// #13193 must check lang before parsing with babel
891+
expect(() =>
892+
compile(
893+
`<script lang="ts">const a = 1</script><script setup lang="tsx">const Comp = () => <p>test</p></script>`,
894+
),
895+
).toThrow(`<script> and <script setup> must have the same language type`)
889896
})
890897

891898
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
@@ -163,13 +163,21 @@ export function compileScript(
163163
)
164164
}
165165

166-
const ctx = new ScriptCompileContext(sfc, options)
167166
const { script, scriptSetup, source, filename } = sfc
168167
const hoistStatic = options.hoistStatic !== false && !script
169168
const scopeId = options.id ? options.id.replace(/^data-v-/, '') : ''
170169
const scriptLang = script && script.lang
171170
const scriptSetupLang = scriptSetup && scriptSetup.lang
172171

172+
if (script && scriptSetup && scriptLang !== scriptSetupLang) {
173+
throw new Error(
174+
`[@vue/compiler-sfc] <script> and <script setup> must have the same ` +
175+
`language type.`,
176+
)
177+
}
178+
179+
const ctx = new ScriptCompileContext(sfc, options)
180+
173181
if (!scriptSetup) {
174182
if (!script) {
175183
throw new Error(`[@vue/compiler-sfc] SFC contains no <script> tags.`)
@@ -178,13 +186,6 @@ export function compileScript(
178186
return processNormalScript(ctx, scopeId)
179187
}
180188

181-
if (script && scriptLang !== scriptSetupLang) {
182-
throw new Error(
183-
`[@vue/compiler-sfc] <script> and <script setup> must have the same ` +
184-
`language type.`,
185-
)
186-
}
187-
188189
if (scriptSetupLang && !ctx.isJS && !ctx.isTS) {
189190
// do not process non js/ts script blocks
190191
return scriptSetup

0 commit comments

Comments
 (0)