]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): allow `<script>` with lang='js' (#7398)
author三咲智子 Kevin Deng <sxzz@sxzz.moe>
Tue, 28 Mar 2023 07:48:41 +0000 (15:48 +0800)
committerGitHub <noreply@github.com>
Tue, 28 Mar 2023 07:48:41 +0000 (15:48 +0800)
packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap
packages/compiler-sfc/__tests__/compileScript.spec.ts
packages/compiler-sfc/src/compileScript.ts

index 2b10b20a2d68eb9eb9979ee59a4378a8c4f9078f..c8bdb1573e947ac58af59b9031bd0ce90cc92ed4 100644 (file)
@@ -1277,6 +1277,21 @@ return () => {}
 }"
 `;
 
+exports[`SFC compile <script setup> > should compile JS syntax 1`] = `
+"const a = 1
+      const b = 2
+      
+export default {
+  setup(__props, { expose }) {
+  expose();
+
+      
+return { a, b }
+}
+
+}"
+`;
+
 exports[`SFC compile <script setup> > should expose top level declarations 1`] = `
 "import { x } from './x'
       
index 6b5ea437519e8b35686e2805dfbe25cc5734ccf6..b8f80eb8e7a6ed2f57df0de86c4334b4303320b6 100644 (file)
@@ -2,6 +2,17 @@ import { BindingTypes } from '@vue/compiler-core'
 import { compileSFCScript as compile, assertCode, mockId } from './utils'
 
 describe('SFC compile <script setup>', () => {
+  test('should compile JS syntax', () => {
+    const { content } = compile(`
+      <script setup lang='js'>
+      const a = 1
+      const b = 2
+      </script>
+    `)
+    expect(content).toMatch(`return { a, b }`)
+    assertCode(content)
+  })
+
   test('should expose top level declarations', () => {
     const { content, bindings } = compile(`
       <script setup>
index ebaf1eee472b323cd60ae1ac26b535e8b9896885..74d4e96349e9847f025e850725e454136eb52e45 100644 (file)
@@ -167,6 +167,11 @@ export function compileScript(
   const cssVars = sfc.cssVars
   const scriptLang = script && script.lang
   const scriptSetupLang = scriptSetup && scriptSetup.lang
+  const isJS =
+    scriptLang === 'js' ||
+    scriptLang === 'jsx' ||
+    scriptSetupLang === 'js' ||
+    scriptSetupLang === 'jsx'
   const isTS =
     scriptLang === 'ts' ||
     scriptLang === 'tsx' ||
@@ -196,7 +201,7 @@ export function compileScript(
     if (!script) {
       throw new Error(`[@vue/compiler-sfc] SFC contains no <script> tags.`)
     }
-    if (scriptLang && !isTS && scriptLang !== 'jsx') {
+    if (scriptLang && !isJS && !isTS) {
       // do not process non js/ts script blocks
       return script
     }
@@ -264,7 +269,7 @@ export function compileScript(
     )
   }
 
-  if (scriptSetupLang && !isTS && scriptSetupLang !== 'jsx') {
+  if (scriptSetupLang && !isJS && !isTS) {
     // do not process non js/ts script blocks
     return scriptSetup
   }