]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): register exported bindings in normal script when using script...
authoredison <daiwei521@126.com>
Thu, 16 Sep 2021 21:22:29 +0000 (05:22 +0800)
committerGitHub <noreply@github.com>
Thu, 16 Sep 2021 21:22:29 +0000 (17:22 -0400)
fix #4600

packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap
packages/compiler-sfc/__tests__/compileScript.spec.ts
packages/compiler-sfc/src/compileScript.ts

index 74c0ad4670e699949486e6d4b3ec33f1e4a23cc5..b60687d97c47cf8388d2ec5c76eff2d83149c8df 100644 (file)
@@ -11,7 +11,7 @@ export default {
 
       x()
       
-return { x }
+return { n, x }
 }
 
 }"
@@ -26,7 +26,7 @@ export default {
 
       x()
       
-return { x }
+return { n, x }
 }
 
 }
@@ -66,7 +66,7 @@ function setup(__props, { expose }) {
 
         x()
         
-return { x }
+return { n, x }
 }
 
 
@@ -87,7 +87,7 @@ function setup(__props, { expose }) {
 
         x()
         
-return { x }
+return { n, x }
 }
 
 
index 135f6be12682f2921a4d3d4574c97f1a7d23c619..7afc1d5cabb9b2706b48393b059537a58a2c44e2 100644 (file)
@@ -1295,6 +1295,20 @@ describe('SFC analyze <script> bindings', () => {
     expect(bindings!.__isScriptSetup).toBe(false)
   })
 
+  it('recognizes exported vars', () => {
+    const { bindings } = compile(`
+      <script>
+        export const foo = 2
+      </script>
+      <script setup>
+        console.log(foo)
+      </script>
+    `)
+    expect(bindings).toStrictEqual({
+      foo: BindingTypes.SETUP_CONST
+    })
+  })
+
   it('recognizes async setup return', () => {
     const { bindings } = compile(`
       <script>
index 766fe98e70aea7c99128295c28de09dea01a5e21..253abbaed141df1f775d02154f68057c1b4ba169 100644 (file)
@@ -702,7 +702,7 @@ export function compileScript(
         const start = node.start! + scriptStartOffset!
         const end = node.declaration.start! + scriptStartOffset!
         s.overwrite(start, end, `const ${defaultTempVar} = `)
-      } else if (node.type === 'ExportNamedDeclaration' && node.specifiers) {
+      } else if (node.type === 'ExportNamedDeclaration') {
         const defaultSpecifier = node.specifiers.find(
           s => s.exported.type === 'Identifier' && s.exported.name === 'default'
         ) as ExportSpecifier
@@ -735,6 +735,9 @@ export function compileScript(
             )
           }
         }
+        if (node.declaration) {
+          walkDeclaration(node.declaration, setupBindings, userImportAlias)
+        }
       } else if (
         (node.type === 'VariableDeclaration' ||
           node.type === 'FunctionDeclaration' ||