Skip to content

Commit f161f50

Browse files
azaletasxzz
andauthored
feat: support component and directive in same name (#440)
* feat:support component and directive in same name * Update src/core/constants.ts Co-authored-by: 三咲智子 <sxzz@sxzz.moe>
1 parent f5ddbb4 commit f161f50

File tree

5 files changed

+99
-2
lines changed

5 files changed

+99
-2
lines changed

src/core/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export const MODULE_NAME = 'unplugin-vue-components'
22
export const RESOLVER_EXT = '.unplugin-vue-components'
33
export const DISABLE_COMMENT = '/* unplugin-vue-components disabled */'
4+
export const DIRECTIVE_IMPORT_PREFIX = 'v'

src/core/context.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Debug from 'debug'
44
import type { UpdatePayload, ViteDevServer } from 'vite'
55
import { slash, throttle, toArray } from '@antfu/utils'
66
import type { ComponentInfo, Options, ResolvedOptions, Transformer } from '../types'
7+
import { DIRECTIVE_IMPORT_PREFIX } from './constants'
78
import { getNameFromFilePath, matchGlobs, normalizeComponetInfo, parseId, pascalCase, resolveAlias } from './utils'
89
import { resolveOptions } from './options'
910
import { searchComponents } from './fs/glob'
@@ -218,7 +219,7 @@ export class Context {
218219
if (resolver.type !== type)
219220
continue
220221

221-
const result = await resolver.resolve(name)
222+
const result = await resolver.resolve(type === 'directive' ? name.slice(DIRECTIVE_IMPORT_PREFIX.length) : name)
222223
if (result) {
223224
if (typeof result === 'string') {
224225
info = {

src/core/transforms/directive/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type MagicString from 'magic-string'
33
import { pascalCase, stringifyComponentImport } from '../../utils'
44
import type { Context } from '../../context'
55
import type { SupportedTransformer } from '../../..'
6+
import { DIRECTIVE_IMPORT_PREFIX } from '../../constants'
67
import vue2Resolver from './vue2'
78
import vue3Resolver from './vue3'
89

@@ -14,7 +15,7 @@ export default async function transformDirective(code: string, transformer: Supp
1415
const results = await (transformer === 'vue2' ? vue2Resolver(code, s) : vue3Resolver(code, s))
1516
for (const { rawName, replace } of results) {
1617
debug(`| ${rawName}`)
17-
const name = pascalCase(rawName)
18+
const name = `${DIRECTIVE_IMPORT_PREFIX}${pascalCase(rawName)}`
1819
ctx.updateUsageMap(sfcPath, [name])
1920

2021
const directive = await ctx.findComponent(name, 'directive', [sfcPath])

test/__snapshots__/transform.test.ts.snap

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,47 @@
11
// Vitest Snapshot v1
22

3+
exports[`Component and directive as same name > vue2 transform should work 1`] = `
4+
{
5+
"code": "/* unplugin-vue-components disabled */import __unplugin_directives_0 from 'test/directive/Loading';
6+
import __unplugin_components_0 from 'test/component/Loading';
7+
8+
var render = function () {
9+
this.$options.directives[\\"loading\\"] = __unplugin_directives_0;
10+
var _vm = this
11+
var _h = _vm.$createElement
12+
var _c = _vm._self._c || _h
13+
return _c(__unplugin_components_0, {
14+
directives: [
15+
{ name: \\"loading\\", rawName: \\"v-loading\\", value: 123, expression: \\"123\\" }
16+
]
17+
})
18+
}
19+
var staticRenderFns = []
20+
render._withStripped = true
21+
export { render, staticRenderFns }
22+
",
23+
}
24+
`;
25+
26+
exports[`Component and directive as same name > vue3 transform should work 1`] = `
27+
{
28+
"code": "/* unplugin-vue-components disabled */import __unplugin_directives_0 from 'test/directive/ElInfiniteScroll';
29+
import __unplugin_components_0 from 'test/component/ElInfiniteScroll';
30+
31+
const render = (_ctx, _cache) => {
32+
const _component_el_infinite_scroll = __unplugin_components_0
33+
const _directive_el_infinite_scroll = __unplugin_directives_0
34+
35+
return _withDirectives(
36+
(_openBlock(),
37+
_createBlock(_component_test_comp, null, null, 512 /* NEED_PATCH */)),
38+
[[_directive_loading, 123]]
39+
)
40+
}
41+
",
42+
}
43+
`;
44+
345
exports[`transform > vue2 transform should work 1`] = `
446
{
547
"code": "/* unplugin-vue-components disabled */import __unplugin_directives_0 from 'test/directive/Loading';

test/transform.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,55 @@ describe('transform', () => {
6363
expect(await ctx.transform(code, '')).toMatchSnapshot()
6464
})
6565
})
66+
67+
describe('Component and directive as same name', () => {
68+
it('vue2 transform should work', async () => {
69+
const code = `
70+
var render = function () {
71+
var _vm = this
72+
var _h = _vm.$createElement
73+
var _c = _vm._self._c || _h
74+
return _c("loading", {
75+
directives: [
76+
{ name: "loading", rawName: "v-loading", value: 123, expression: "123" }
77+
]
78+
})
79+
}
80+
var staticRenderFns = []
81+
render._withStripped = true
82+
export { render, staticRenderFns }
83+
`
84+
85+
const ctx = new Context({
86+
resolvers: [resolver],
87+
transformer: 'vue2',
88+
directives: true,
89+
})
90+
ctx.sourcemap = false
91+
expect(await ctx.transform(code, '')).toMatchSnapshot()
92+
})
93+
94+
it('vue3 transform should work', async () => {
95+
const code = `
96+
const render = (_ctx, _cache) => {
97+
const _component_el_infinite_scroll = _resolveComponent("el-infinite-scroll")
98+
const _directive_el_infinite_scroll = _resolveDirective("el-infinite-scroll")
99+
100+
return _withDirectives(
101+
(_openBlock(),
102+
_createBlock(_component_test_comp, null, null, 512 /* NEED_PATCH */)),
103+
[[_directive_loading, 123]]
104+
)
105+
}
106+
`
107+
108+
const ctx = new Context({
109+
resolvers: [resolver],
110+
transformer: 'vue3',
111+
directives: true,
112+
})
113+
ctx.sourcemap = false
114+
expect(await ctx.transform(code, '')).toMatchSnapshot()
115+
})
116+
})
117+

0 commit comments

Comments
 (0)