Skip to content

Commit 085bbd5

Browse files
committed
fix(compiler-sfc): named imports from .vue file should not be treated as constant
fix #2699
1 parent f2b0a8e commit 085bbd5

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,10 @@ return { ref }
195195
`;
196196
197197
exports[`SFC compile <script setup> inlineTemplate mode avoid unref() when necessary 1`] = `
198-
"import { createVNode as _createVNode, toDisplayString as _toDisplayString, unref as _unref, Fragment as _Fragment, openBlock as _openBlock, createBlock as _createBlock } from \\"vue\\"
198+
"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\\"
199199
200200
import { ref } from 'vue'
201-
import Foo from './Foo.vue'
201+
import Foo, { bar } from './Foo.vue'
202202
import other from './util'
203203
204204
export default {
@@ -213,7 +213,12 @@ export default {
213213
214214
return (_ctx, _cache) => {
215215
return (_openBlock(), _createBlock(_Fragment, null, [
216-
_createVNode(Foo),
216+
_createVNode(Foo, null, {
217+
default: _withCtx(() => [
218+
_createTextVNode(_toDisplayString(_unref(bar)), 1 /* TEXT */)
219+
]),
220+
_: 1 /* STABLE */
221+
}),
217222
_createVNode(\\"div\\", { onClick: fn }, _toDisplayString(count.value) + \\" \\" + _toDisplayString(constant) + \\" \\" + _toDisplayString(_unref(maybe)) + \\" \\" + _toDisplayString(_unref(lett)) + \\" \\" + _toDisplayString(_unref(other)), 1 /* TEXT */)
218223
], 64 /* STABLE_FRAGMENT */))
219224
}

packages/compiler-sfc/__tests__/compileScript.spec.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ const myEmit = defineEmit(['foo', 'bar'])
217217
const { content } = compile(
218218
`<script setup>
219219
import { ref } from 'vue'
220-
import Foo from './Foo.vue'
220+
import Foo, { bar } from './Foo.vue'
221221
import other from './util'
222222
const count = ref(0)
223223
const constant = {}
@@ -226,14 +226,16 @@ const myEmit = defineEmit(['foo', 'bar'])
226226
function fn() {}
227227
</script>
228228
<template>
229-
<Foo/>
229+
<Foo>{{ bar }}</Foo>
230230
<div @click="fn">{{ count }} {{ constant }} {{ maybe }} {{ lett }} {{ other }}</div>
231231
</template>
232232
`,
233233
{ inlineTemplate: true }
234234
)
235235
// no need to unref vue component import
236-
expect(content).toMatch(`createVNode(Foo)`)
236+
expect(content).toMatch(`createVNode(Foo,`)
237+
// #2699 should unref named imports from .vue
238+
expect(content).toMatch(`unref(bar)`)
237239
// should unref other imports
238240
expect(content).toMatch(`unref(other)`)
239241
// no need to unref constant literals

packages/compiler-sfc/src/compileScript.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export function compileScript(
168168
string,
169169
{
170170
isType: boolean
171-
imported: string | null
171+
imported: string
172172
source: string
173173
}
174174
> = Object.create(null)
@@ -246,7 +246,7 @@ export function compileScript(
246246
}
247247
userImports[local] = {
248248
isType,
249-
imported: imported || null,
249+
imported: imported || 'default',
250250
source
251251
}
252252
}
@@ -807,10 +807,12 @@ export function compileScript(
807807
for (const key in typeDeclaredProps) {
808808
bindingMetadata[key] = BindingTypes.PROPS
809809
}
810-
for (const [key, { isType, source }] of Object.entries(userImports)) {
810+
for (const [key, { isType, imported, source }] of Object.entries(
811+
userImports
812+
)) {
811813
if (isType) continue
812814
bindingMetadata[key] =
813-
source.endsWith('.vue') || source === 'vue'
815+
(imported === 'default' && source.endsWith('.vue')) || source === 'vue'
814816
? BindingTypes.SETUP_CONST
815817
: BindingTypes.SETUP_MAYBE_REF
816818
}

0 commit comments

Comments
 (0)