fix(compiler-sfc): fix treeshaking of namespace import when used in template

fix #5209
This commit is contained in:
Evan You
2022-05-12 18:56:54 +08:00
parent 8c51c6514f
commit 8a123ac34f
3 changed files with 14 additions and 3 deletions

View File

@@ -777,6 +777,7 @@ exports[`SFC compile <script setup> inlineTemplate mode avoid unref() when neces
import { ref } from 'vue'
import Foo, { bar } from './Foo.vue'
import other from './util'
import * as tree from './tree'
export default {
setup(__props) {
@@ -795,7 +796,8 @@ return (_ctx, _cache) => {
]),
_: 1 /* STABLE */
}),
_createElementVNode(\\"div\\", { onClick: fn }, _toDisplayString(count.value) + \\" \\" + _toDisplayString(constant) + \\" \\" + _toDisplayString(_unref(maybe)) + \\" \\" + _toDisplayString(_unref(lett)) + \\" \\" + _toDisplayString(_unref(other)), 1 /* TEXT */)
_createElementVNode(\\"div\\", { onClick: fn }, _toDisplayString(count.value) + \\" \\" + _toDisplayString(constant) + \\" \\" + _toDisplayString(_unref(maybe)) + \\" \\" + _toDisplayString(_unref(lett)) + \\" \\" + _toDisplayString(_unref(other)), 1 /* TEXT */),
_createTextVNode(\\" \\" + _toDisplayString(tree.foo()), 1 /* TEXT */)
], 64 /* STABLE_FRAGMENT */))
}
}

View File

@@ -519,6 +519,7 @@ defineExpose({ foo: 123 })
import { ref } from 'vue'
import Foo, { bar } from './Foo.vue'
import other from './util'
import * as tree from './tree'
const count = ref(0)
const constant = {}
const maybe = foo()
@@ -528,6 +529,7 @@ defineExpose({ foo: 123 })
<template>
<Foo>{{ bar }}</Foo>
<div @click="fn">{{ count }} {{ constant }} {{ maybe }} {{ lett }} {{ other }}</div>
{{ tree.foo() }}
</template>
`,
{ inlineTemplate: true }
@@ -546,6 +548,8 @@ defineExpose({ foo: 123 })
expect(content).toMatch(`unref(maybe)`)
// should unref() on let bindings
expect(content).toMatch(`unref(lett)`)
// no need to unref namespace import (this also preserves tree-shaking)
expect(content).toMatch(`tree.foo()`)
// no need to unref function declarations
expect(content).toMatch(`{ onClick: fn }`)
// no need to mark constant fns in patch flag