Skip to content

Commit

Permalink
fix(compiler-sfc): fix dev regression for dot / namespace component u…
Browse files Browse the repository at this point in the history
…sage

close #9947
  • Loading branch information
yyx990803 committed Dec 30, 2023
1 parent 63c3e62 commit dce99c1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ return { get FooBar() { return FooBar }, get foo() { return foo }, get bar() { r
})"
`;

exports[`import namespace 1`] = `
"import { defineComponent as _defineComponent } from 'vue'
import * as Foo from './foo'
export default /*#__PURE__*/_defineComponent({
setup(__props, { expose: __expose }) {
__expose();
return { get Foo() { return Foo } }
}
})"
`;

exports[`js template string interpolations 1`] = `
"import { defineComponent as _defineComponent } from 'vue'
import { VAR, VAR2, VAR3 } from './x'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,17 @@ test('property access (whitespace)', () => {
expect(content).toMatch('return { get Foo() { return Foo } }')
assertCode(content)
})

// #9974
test('namespace / dot component usage', () => {
const { content } = compile(`
<script setup lang="ts">
import * as Foo from './foo'
</script>
<template>
<Foo.Bar />
</template>
`)
expect(content).toMatch('return { get Foo() { return Foo } }')
assertCode(content)
})
14 changes: 8 additions & 6 deletions packages/compiler-sfc/src/script/importUsageCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import { camelize, capitalize, isBuiltInDirective } from '@vue/shared'
* when not using inline mode.
*/
export function isImportUsed(local: string, sfc: SFCDescriptor): boolean {
return resolveTemplateUsageCheckString(sfc).has(local)
return resolveTemplateUsedIdentifiers(sfc).has(local)
}

const templateUsageCheckCache = createCache<Set<string>>()

function resolveTemplateUsageCheckString(sfc: SFCDescriptor) {
function resolveTemplateUsedIdentifiers(sfc: SFCDescriptor): Set<string> {
const { content, ast } = sfc.template!
const cached = templateUsageCheckCache.get(content)
if (cached) {
Expand All @@ -35,12 +35,14 @@ function resolveTemplateUsageCheckString(sfc: SFCDescriptor) {
function walk(node: TemplateChildNode) {
switch (node.type) {
case NodeTypes.ELEMENT:
let tag = node.tag
if (tag.includes('.')) tag = tag.split('.')[0].trim()
if (
!parserOptions.isNativeTag!(node.tag) &&
!parserOptions.isBuiltInComponent!(node.tag)
!parserOptions.isNativeTag!(tag) &&
!parserOptions.isBuiltInComponent!(tag)
) {
ids.add(camelize(node.tag))
ids.add(capitalize(camelize(node.tag)))
ids.add(camelize(tag))
ids.add(capitalize(camelize(tag)))
}
for (let i = 0; i < node.props.length; i++) {
const prop = node.props[i]
Expand Down

0 comments on commit dce99c1

Please sign in to comment.