]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): named imports from .vue file should not be treated as constant
authorEvan You <yyx990803@gmail.com>
Tue, 1 Dec 2020 16:52:29 +0000 (11:52 -0500)
committerEvan You <yyx990803@gmail.com>
Tue, 1 Dec 2020 16:52:29 +0000 (11:52 -0500)
fix #2699

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

index a68625d607c7e1f251d4da9fc7d38a887cb9b991..01e2b80981cf4028a57eadc442c464558f4319ac 100644 (file)
@@ -195,10 +195,10 @@ return { ref }
 `;
 
 exports[`SFC compile <script setup> inlineTemplate mode avoid unref() when necessary 1`] = `
-"import { createVNode as _createVNode, toDisplayString as _toDisplayString, unref as _unref, Fragment as _Fragment, openBlock as _openBlock, createBlock as _createBlock } from \\"vue\\"
+"import { unref as _unref, toDisplayString as _toDisplayString, createTextVNode as _createTextVNode, withCtx as _withCtx, createVNode as _createVNode, Fragment as _Fragment, openBlock as _openBlock, createBlock as _createBlock } from \\"vue\\"
 
 import { ref } from 'vue'
-        import Foo from './Foo.vue'
+        import Foo, { bar } from './Foo.vue'
         import other from './util'
         
 export default {
@@ -213,7 +213,12 @@ export default {
         
 return (_ctx, _cache) => {
   return (_openBlock(), _createBlock(_Fragment, null, [
-    _createVNode(Foo),
+    _createVNode(Foo, null, {
+      default: _withCtx(() => [
+        _createTextVNode(_toDisplayString(_unref(bar)), 1 /* TEXT */)
+      ]),
+      _: 1 /* STABLE */
+    }),
     _createVNode(\\"div\\", { onClick: fn }, _toDisplayString(count.value) + \\" \\" + _toDisplayString(constant) + \\" \\" + _toDisplayString(_unref(maybe)) + \\" \\" + _toDisplayString(_unref(lett)) + \\" \\" + _toDisplayString(_unref(other)), 1 /* TEXT */)
   ], 64 /* STABLE_FRAGMENT */))
 }
index b15f5b9719b7e31b647128fdba5603bbd890c632..218614795f131a5e5825fc08ed07b1c3d820b788 100644 (file)
@@ -217,7 +217,7 @@ const myEmit = defineEmit(['foo', 'bar'])
       const { content } = compile(
         `<script setup>
         import { ref } from 'vue'
-        import Foo from './Foo.vue'
+        import Foo, { bar } from './Foo.vue'
         import other from './util'
         const count = ref(0)
         const constant = {}
@@ -226,14 +226,16 @@ const myEmit = defineEmit(['foo', 'bar'])
         function fn() {}
         </script>
         <template>
-          <Foo/>
+          <Foo>{{ bar }}</Foo>
           <div @click="fn">{{ count }} {{ constant }} {{ maybe }} {{ lett }} {{ other }}</div>
         </template>
         `,
         { inlineTemplate: true }
       )
       // no need to unref vue component import
-      expect(content).toMatch(`createVNode(Foo)`)
+      expect(content).toMatch(`createVNode(Foo,`)
+      // #2699 should unref named imports from .vue
+      expect(content).toMatch(`unref(bar)`)
       // should unref other imports
       expect(content).toMatch(`unref(other)`)
       // no need to unref constant literals
index 7dca8606e2a639649f166a0f2f23ca848176778d..b51ae34f218cfa0218b291d60ce985cb8dc2b983 100644 (file)
@@ -168,7 +168,7 @@ export function compileScript(
     string,
     {
       isType: boolean
-      imported: string | null
+      imported: string
       source: string
     }
   > = Object.create(null)
@@ -246,7 +246,7 @@ export function compileScript(
     }
     userImports[local] = {
       isType,
-      imported: imported || null,
+      imported: imported || 'default',
       source
     }
   }
@@ -807,10 +807,12 @@ export function compileScript(
   for (const key in typeDeclaredProps) {
     bindingMetadata[key] = BindingTypes.PROPS
   }
-  for (const [key, { isType, source }] of Object.entries(userImports)) {
+  for (const [key, { isType, imported, source }] of Object.entries(
+    userImports
+  )) {
     if (isType) continue
     bindingMetadata[key] =
-      source.endsWith('.vue') || source === 'vue'
+      (imported === 'default' && source.endsWith('.vue')) || source === 'vue'
         ? BindingTypes.SETUP_CONST
         : BindingTypes.SETUP_MAYBE_REF
   }