]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): prohibit src usage for `<script setup>` + do not
authorEvan You <yyx990803@gmail.com>
Wed, 15 Jul 2020 21:43:54 +0000 (17:43 -0400)
committerEvan You <yyx990803@gmail.com>
Wed, 15 Jul 2020 21:44:02 +0000 (17:44 -0400)
process non js/ts blocks in compileScript

packages/compiler-sfc/src/compileScript.ts
packages/compiler-sfc/src/parse.ts

index 0433d5b987385edb3d2d9c1b5724abed28fca05f..f6a6399684d0d8e00f5bd42ea99249c32433c33e 100644 (file)
@@ -53,10 +53,9 @@ export function compileScript(
 
   const hasCssVars = styles.some(s => typeof s.attrs.vars === 'string')
 
-  const isTS =
-    (script && script.lang === 'ts') ||
-    (scriptSetup && scriptSetup.lang === 'ts')
-
+  const scriptLang = script && script.lang
+  const scriptSetupLang = scriptSetup && scriptSetup.lang
+  const isTS = scriptLang === 'ts' || scriptSetupLang === 'ts'
   const plugins: ParserPlugin[] = [
     ...(options.babelParserPlugins || []),
     ...babelParserDefautPlugins,
@@ -67,6 +66,10 @@ export function compileScript(
     if (!script) {
       throw new Error(`SFC contains no <script> tags.`)
     }
+    if (scriptLang && scriptLang !== 'ts') {
+      // do not process non js/ts script blocks
+      return script
+    }
     return {
       ...script,
       content: hasCssVars ? injectCssVarsCalls(sfc, plugins) : script.content,
@@ -74,12 +77,17 @@ export function compileScript(
     }
   }
 
-  if (script && script.lang !== scriptSetup.lang) {
+  if (script && scriptLang !== scriptSetupLang) {
     throw new Error(
       `<script> and <script setup> must have the same language type.`
     )
   }
 
+  if (scriptSetupLang && scriptSetupLang !== 'ts') {
+    // do not process non js/ts script blocks
+    return scriptSetup
+  }
+
   const defaultTempVar = `__default__`
   const bindings: BindingMetadata = {}
   const imports: Record<string, string> = {}
index cdc5f74b400e36c842db4a47a92a51e98818cae6..eaeb2bcd6cece9d9596c2ec5c23774a5a7c9c266 100644 (file)
@@ -150,15 +150,6 @@ export function parse(
         const block = createBlock(node, source, pad) as SFCScriptBlock
         const isSetup = !!block.attrs.setup
         if (isSetup && !descriptor.scriptSetup) {
-          if (block.src) {
-            errors.push(
-              new SyntaxError(
-                `<script setup> cannot be used with the "src" attribute since ` +
-                  `its syntax will be ambiguous outside of the component.`
-              )
-            )
-            break
-          }
           descriptor.scriptSetup = block
           break
         }
@@ -177,6 +168,27 @@ export function parse(
     }
   })
 
+  if (descriptor.scriptSetup) {
+    if (descriptor.scriptSetup.src) {
+      errors.push(
+        new SyntaxError(
+          `<script setup> cannot use the "src" attribute because ` +
+            `its syntax will be ambiguous outside of the component.`
+        )
+      )
+      delete descriptor.scriptSetup
+    }
+    if (descriptor.script && descriptor.script.src) {
+      errors.push(
+        new SyntaxError(
+          `<script> cannot use the "src" attribute when <script setup> is ` +
+            `also present because they must be processed together.`
+        )
+      )
+      delete descriptor.script
+    }
+  }
+
   if (sourceMap) {
     const genMap = (block: SFCBlock | null) => {
       if (block && !block.src) {