Skip to content

Commit 06e9e38

Browse files
alex-snezhko9romise
authored andcommitted
fix(compiler-sfc): check lang before attempt to compile script (#13508)
close #8368
1 parent f42d1c2 commit 06e9e38

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,4 +1695,19 @@ describe('compileScript', () => {
16951695
)
16961696
assertCode(content)
16971697
})
1698+
1699+
test('should not compile unrecognized language', () => {
1700+
const { content, lang, scriptAst } = compile(
1701+
`<script lang="coffee">
1702+
export default
1703+
data: ->
1704+
myVal: 0
1705+
</script>`,
1706+
)
1707+
expect(content).toMatch(`export default
1708+
data: ->
1709+
myVal: 0`)
1710+
expect(lang).toBe('coffee')
1711+
expect(scriptAst).not.toBeDefined()
1712+
})
16981713
})

packages/compiler-sfc/src/script/context.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { BindingMetadata } from '../../../compiler-core/src'
99
import MagicString from 'magic-string'
1010
import type { TypeScope } from './resolveType'
1111
import { warn } from '../warn'
12+
import { isJS, isTS } from './utils'
1213

1314
export class ScriptCompileContext {
1415
isJS: boolean
@@ -87,16 +88,8 @@ export class ScriptCompileContext {
8788
const scriptLang = script && script.lang
8889
const scriptSetupLang = scriptSetup && scriptSetup.lang
8990

90-
this.isJS =
91-
scriptLang === 'js' ||
92-
scriptLang === 'jsx' ||
93-
scriptSetupLang === 'js' ||
94-
scriptSetupLang === 'jsx'
95-
this.isTS =
96-
scriptLang === 'ts' ||
97-
scriptLang === 'tsx' ||
98-
scriptSetupLang === 'ts' ||
99-
scriptSetupLang === 'tsx'
91+
this.isJS = isJS(scriptLang, scriptSetupLang)
92+
this.isTS = isTS(scriptLang, scriptSetupLang)
10093

10194
const customElement = options.customElement
10295
const filename = this.descriptor.filename

packages/compiler-sfc/src/script/normalScript.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ export function processNormalScript(
1212
scopeId: string,
1313
): SFCScriptBlock {
1414
const script = ctx.descriptor.script!
15-
if (script.lang && !ctx.isJS && !ctx.isTS) {
16-
// do not process non js/ts script blocks
17-
return script
18-
}
1915
try {
2016
let content = script.content
2117
let map = script.map

packages/compiler-sfc/src/script/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,8 @@ export const propNameEscapeSymbolsRE: RegExp =
121121
export function getEscapedPropName(key: string): string {
122122
return propNameEscapeSymbolsRE.test(key) ? JSON.stringify(key) : key
123123
}
124+
125+
export const isJS = (...langs: (string | null | undefined)[]): boolean =>
126+
langs.some(lang => lang === 'js' || lang === 'jsx')
127+
export const isTS = (...langs: (string | null | undefined)[]): boolean =>
128+
langs.some(lang => lang === 'ts' || lang === 'tsx')

0 commit comments

Comments
 (0)