Skip to content

Commit 8a123ac

Browse files
committed
fix(compiler-sfc): fix treeshaking of namespace import when used in template
fix #5209
1 parent 8c51c65 commit 8a123ac

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,7 @@ exports[`SFC compile <script setup> inlineTemplate mode avoid unref() when neces
777777
import { ref } from 'vue'
778778
import Foo, { bar } from './Foo.vue'
779779
import other from './util'
780+
import * as tree from './tree'
780781

781782
export default {
782783
setup(__props) {
@@ -795,7 +796,8 @@ return (_ctx, _cache) => {
795796
]),
796797
_: 1 /* STABLE */
797798
}),
798-
_createElementVNode(\\"div\\", { onClick: fn }, _toDisplayString(count.value) + \\" \\" + _toDisplayString(constant) + \\" \\" + _toDisplayString(_unref(maybe)) + \\" \\" + _toDisplayString(_unref(lett)) + \\" \\" + _toDisplayString(_unref(other)), 1 /* TEXT */)
799+
_createElementVNode(\\"div\\", { onClick: fn }, _toDisplayString(count.value) + \\" \\" + _toDisplayString(constant) + \\" \\" + _toDisplayString(_unref(maybe)) + \\" \\" + _toDisplayString(_unref(lett)) + \\" \\" + _toDisplayString(_unref(other)), 1 /* TEXT */),
800+
_createTextVNode(\\" \\" + _toDisplayString(tree.foo()), 1 /* TEXT */)
799801
], 64 /* STABLE_FRAGMENT */))
800802
}
801803
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ defineExpose({ foo: 123 })
519519
import { ref } from 'vue'
520520
import Foo, { bar } from './Foo.vue'
521521
import other from './util'
522+
import * as tree from './tree'
522523
const count = ref(0)
523524
const constant = {}
524525
const maybe = foo()
@@ -528,6 +529,7 @@ defineExpose({ foo: 123 })
528529
<template>
529530
<Foo>{{ bar }}</Foo>
530531
<div @click="fn">{{ count }} {{ constant }} {{ maybe }} {{ lett }} {{ other }}</div>
532+
{{ tree.foo() }}
531533
</template>
532534
`,
533535
{ inlineTemplate: true }
@@ -546,6 +548,8 @@ defineExpose({ foo: 123 })
546548
expect(content).toMatch(`unref(maybe)`)
547549
// should unref() on let bindings
548550
expect(content).toMatch(`unref(lett)`)
551+
// no need to unref namespace import (this also preserves tree-shaking)
552+
expect(content).toMatch(`tree.foo()`)
549553
// no need to unref function declarations
550554
expect(content).toMatch(`{ onClick: fn }`)
551555
// no need to mark constant fns in patch flag

packages/compiler-sfc/src/compileScript.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1011,10 +1011,13 @@ export function compileScript(
10111011
for (let i = 0; i < node.specifiers.length; i++) {
10121012
const specifier = node.specifiers[i]
10131013
const local = specifier.local.name
1014-
const imported =
1014+
let imported =
10151015
specifier.type === 'ImportSpecifier' &&
10161016
specifier.imported.type === 'Identifier' &&
10171017
specifier.imported.name
1018+
if (specifier.type === 'ImportNamespaceSpecifier') {
1019+
imported = '*'
1020+
}
10181021
const source = node.source.value
10191022
const existing = userImports[local]
10201023
if (
@@ -1257,7 +1260,9 @@ export function compileScript(
12571260
)) {
12581261
if (isType) continue
12591262
bindingMetadata[key] =
1260-
(imported === 'default' && source.endsWith('.vue')) || source === 'vue'
1263+
imported === '*' ||
1264+
(imported === 'default' && source.endsWith('.vue')) ||
1265+
source === 'vue'
12611266
? BindingTypes.SETUP_CONST
12621267
: BindingTypes.SETUP_MAYBE_REF
12631268
}

0 commit comments

Comments
 (0)