Skip to content

Commit af4b0c2

Browse files
committed
fix(compiler-sfc): prohibit src usage for <script setup> + do not
process non js/ts blocks in compileScript
1 parent 4535b1b commit af4b0c2

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

packages/compiler-sfc/src/compileScript.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,9 @@ export function compileScript(
5353

5454
const hasCssVars = styles.some(s => typeof s.attrs.vars === 'string')
5555

56-
const isTS =
57-
(script && script.lang === 'ts') ||
58-
(scriptSetup && scriptSetup.lang === 'ts')
59-
56+
const scriptLang = script && script.lang
57+
const scriptSetupLang = scriptSetup && scriptSetup.lang
58+
const isTS = scriptLang === 'ts' || scriptSetupLang === 'ts'
6059
const plugins: ParserPlugin[] = [
6160
...(options.babelParserPlugins || []),
6261
...babelParserDefautPlugins,
@@ -67,19 +66,28 @@ export function compileScript(
6766
if (!script) {
6867
throw new Error(`SFC contains no <script> tags.`)
6968
}
69+
if (scriptLang && scriptLang !== 'ts') {
70+
// do not process non js/ts script blocks
71+
return script
72+
}
7073
return {
7174
...script,
7275
content: hasCssVars ? injectCssVarsCalls(sfc, plugins) : script.content,
7376
bindings: analyzeScriptBindings(script)
7477
}
7578
}
7679

77-
if (script && script.lang !== scriptSetup.lang) {
80+
if (script && scriptLang !== scriptSetupLang) {
7881
throw new Error(
7982
`<script> and <script setup> must have the same language type.`
8083
)
8184
}
8285

86+
if (scriptSetupLang && scriptSetupLang !== 'ts') {
87+
// do not process non js/ts script blocks
88+
return scriptSetup
89+
}
90+
8391
const defaultTempVar = `__default__`
8492
const bindings: BindingMetadata = {}
8593
const imports: Record<string, string> = {}

packages/compiler-sfc/src/parse.ts

+21-9
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,6 @@ export function parse(
150150
const block = createBlock(node, source, pad) as SFCScriptBlock
151151
const isSetup = !!block.attrs.setup
152152
if (isSetup && !descriptor.scriptSetup) {
153-
if (block.src) {
154-
errors.push(
155-
new SyntaxError(
156-
`<script setup> cannot be used with the "src" attribute since ` +
157-
`its syntax will be ambiguous outside of the component.`
158-
)
159-
)
160-
break
161-
}
162153
descriptor.scriptSetup = block
163154
break
164155
}
@@ -177,6 +168,27 @@ export function parse(
177168
}
178169
})
179170

171+
if (descriptor.scriptSetup) {
172+
if (descriptor.scriptSetup.src) {
173+
errors.push(
174+
new SyntaxError(
175+
`<script setup> cannot use the "src" attribute because ` +
176+
`its syntax will be ambiguous outside of the component.`
177+
)
178+
)
179+
delete descriptor.scriptSetup
180+
}
181+
if (descriptor.script && descriptor.script.src) {
182+
errors.push(
183+
new SyntaxError(
184+
`<script> cannot use the "src" attribute when <script setup> is ` +
185+
`also present because they must be processed together.`
186+
)
187+
)
188+
delete descriptor.script
189+
}
190+
}
191+
180192
if (sourceMap) {
181193
const genMap = (block: SFCBlock | null) => {
182194
if (block && !block.src) {

0 commit comments

Comments
 (0)