)
assertCode(content)
})
+
+ test('should not compile unrecognized language', () => {
+ const { content, lang, scriptAst } = compile(
+ `<script lang="coffee">
+ export default
+ data: ->
+ myVal: 0
+ </script>`,
+ )
+ expect(content).toMatch(`export default
+ data: ->
+ myVal: 0`)
+ expect(lang).toBe('coffee')
+ expect(scriptAst).not.toBeDefined()
+ })
})
import { DEFINE_OPTIONS, processDefineOptions } from './script/defineOptions'
import { DEFINE_SLOTS, processDefineSlots } from './script/defineSlots'
import { DEFINE_MODEL, processDefineModel } from './script/defineModel'
-import { getImportedName, isCallOf, isLiteralNode } from './script/utils'
+import {
+ getImportedName,
+ isCallOf,
+ isJS,
+ isLiteralNode,
+ isTS,
+} from './script/utils'
import { analyzeScriptBindings } from './script/analyzeScriptBindings'
import { isImportUsed } from './script/importUsageCheck'
import { processAwait } from './script/topLevelAwait'
const scopeId = options.id ? options.id.replace(/^data-v-/, '') : ''
const scriptLang = script && script.lang
const scriptSetupLang = scriptSetup && scriptSetup.lang
+ const isJSOrTS =
+ isJS(scriptLang, scriptSetupLang) || isTS(scriptLang, scriptSetupLang)
if (script && scriptSetup && scriptLang !== scriptSetupLang) {
throw new Error(
)
}
- const ctx = new ScriptCompileContext(sfc, options)
-
if (!scriptSetup) {
if (!script) {
throw new Error(`[@vue/compiler-sfc] SFC contains no <script> tags.`)
}
+
// normal <script> only
+ if (script.lang && !isJSOrTS) {
+ // do not process non js/ts script blocks
+ return script
+ }
+
+ const ctx = new ScriptCompileContext(sfc, options)
return processNormalScript(ctx, scopeId)
}
- if (scriptSetupLang && !ctx.isJS && !ctx.isTS) {
+ if (scriptSetupLang && !isJSOrTS) {
// do not process non js/ts script blocks
return scriptSetup
}
+ const ctx = new ScriptCompileContext(sfc, options)
+
// metadata that needs to be returned
// const ctx.bindingMetadata: BindingMetadata = {}
const scriptBindings: Record<string, BindingTypes> = Object.create(null)
import MagicString from 'magic-string'
import type { TypeScope } from './resolveType'
import { warn } from '../warn'
+import { isJS, isTS } from './utils'
export class ScriptCompileContext {
isJS: boolean
const scriptLang = script && script.lang
const scriptSetupLang = scriptSetup && scriptSetup.lang
- this.isJS =
- scriptLang === 'js' ||
- scriptLang === 'jsx' ||
- scriptSetupLang === 'js' ||
- scriptSetupLang === 'jsx'
- this.isTS =
- scriptLang === 'ts' ||
- scriptLang === 'tsx' ||
- scriptSetupLang === 'ts' ||
- scriptSetupLang === 'tsx'
+ this.isJS = isJS(scriptLang, scriptSetupLang)
+ this.isTS = isTS(scriptLang, scriptSetupLang)
const customElement = options.customElement
const filename = this.descriptor.filename
export function getEscapedPropName(key: string): string {
return propNameEscapeSymbolsRE.test(key) ? JSON.stringify(key) : key
}
+
+export const isJS = (...langs: (string | null | undefined)[]): boolean =>
+ langs.some(lang => lang === 'js' || lang === 'jsx')
+export const isTS = (...langs: (string | null | undefined)[]): boolean =>
+ langs.some(lang => lang === 'ts' || lang === 'tsx')