Skip to content

Commit bb6dbeb

Browse files
hannoeruhaoziqaq
authored andcommitted
fix: shouldn't using component resolver to resolve directive
1 parent 54349e3 commit bb6dbeb

26 files changed

+248
-166
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ Components({
288288
// auto import for directives
289289
// default: `true` for Vue 3, `false` for Vue 2
290290
// Babel is needed to do the transformation for Vue 2, it's disabled by default for performance concerns.
291+
// To install Babel, run: `npm install -D @babel/parser @babel/traverse`
291292
directives: true,
292293

293294
// filters for transforming targets

package.json

+10
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,18 @@
6060
"test:update": "jest --u"
6161
},
6262
"peerDependencies": {
63+
"@babel/parser": "^7.15.8",
64+
"@babel/traverse": "^7.15.4",
6365
"vue": "2 || 3"
6466
},
67+
"peerDependenciesMeta": {
68+
"@babel/parser": {
69+
"optional": true
70+
},
71+
"@babel/traverse": {
72+
"optional": true
73+
}
74+
},
6575
"dependencies": {
6676
"@antfu/utils": "^0.3.0",
6777
"@rollup/pluginutils": "^4.1.1",

src/core/context.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,10 @@ export class Context {
186186

187187
// custom resolvers
188188
for (const resolver of this.options.resolvers) {
189-
const result = await resolver(name, type)
189+
if (resolver.type !== type)
190+
continue
191+
192+
const result = await resolver.resolve(name)
190193
if (result) {
191194
if (typeof result === 'string') {
192195
info = {

src/core/helpers/libraryResolver.ts

+19-13
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,33 @@ export function LibraryResolver(options: UILibraryOptions): ComponentResolver {
3636
if (!entries) {
3737
// eslint-disable-next-line no-console
3838
console.warn(`[unplugin-vue-components] Failed to load Vetur tags from library "${libraryName}"`)
39-
return () => {}
39+
return {
40+
type: 'component',
41+
resolve: () => {},
42+
}
4043
}
4144

4245
debug(entries)
4346

4447
const prefixKebab = kebabCase(prefix)
4548
const kebabEntries = entries.map(name => ({ name, kebab: kebabCase(name) }))
4649

47-
return (name) => {
48-
const kebab = kebabCase(name)
49-
let componentName = kebab
50+
return {
51+
type: 'component',
52+
resolve: (name) => {
53+
const kebab = kebabCase(name)
54+
let componentName = kebab
5055

51-
if (prefixKebab) {
52-
if (!kebab.startsWith(`${prefixKebab}-`))
53-
return
54-
componentName = kebab.slice(prefixKebab.length + 1)
55-
}
56+
if (prefixKebab) {
57+
if (!kebab.startsWith(`${prefixKebab}-`))
58+
return
59+
componentName = kebab.slice(prefixKebab.length + 1)
60+
}
5661

57-
for (const entry of kebabEntries) {
58-
if (entry.kebab === componentName)
59-
return { path: libraryName, importName: entry.name }
60-
}
62+
for (const entry of kebabEntries) {
63+
if (entry.kebab === componentName)
64+
return { path: libraryName, importName: entry.name }
65+
}
66+
},
6167
}
6268
}

src/core/options.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const defaultOptions: Omit<Required<Options>, 'include' | 'exclude' | 'tr
2424
export function resolveOptions(options: Options, root: string): ResolvedOptions {
2525
const resolved = Object.assign({}, defaultOptions, options) as ResolvedOptions
2626
resolved.libraries = toArray(resolved.libraries).map(i => typeof i === 'string' ? { name: i } : i)
27-
resolved.resolvers = toArray(resolved.resolvers)
27+
resolved.resolvers = toArray(resolved.resolvers).flat()
2828
resolved.resolvers.push(...resolved.libraries.map(lib => LibraryResolver(lib)))
2929
resolved.extensions = toArray(resolved.extensions)
3030

src/core/resolvers/antdv.ts

+16-13
Original file line numberDiff line numberDiff line change
@@ -228,21 +228,24 @@ function getSideEffects(compName: string, options: AntDesignVueResolverOptions):
228228
* @link https://antdv.com/
229229
*/
230230
export function AntDesignVueResolver(options: AntDesignVueResolverOptions = {}): ComponentResolver {
231-
return (name: string) => {
232-
if (options.resolveIcons && name.match(/(Outlined|Filled|TwoTone)$/)) {
233-
return {
234-
importName: name,
235-
path: '@ant-design/icons-vue',
231+
return {
232+
type: 'component',
233+
resolve: (name: string) => {
234+
if (options.resolveIcons && name.match(/(Outlined|Filled|TwoTone)$/)) {
235+
return {
236+
importName: name,
237+
path: '@ant-design/icons-vue',
238+
}
236239
}
237-
}
238240

239-
if (name.match(/^A[A-Z]/) && !options?.exclude?.includes(name)) {
240-
const importName = name.slice(1)
241-
return {
242-
importName,
243-
path: 'ant-design-vue/es',
244-
sideEffects: getSideEffects(importName, options),
241+
if (name.match(/^A[A-Z]/) && !options?.exclude?.includes(name)) {
242+
const importName = name.slice(1)
243+
return {
244+
importName,
245+
path: 'ant-design-vue/es',
246+
sideEffects: getSideEffects(importName, options),
247+
}
245248
}
246-
}
249+
},
247250
}
248251
}

src/core/resolvers/element-plus.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ function resolveDirective(name: string, options: ElementPlusResolverOptionsResol
139139
*/
140140
export function ElementPlusResolver(
141141
options: ElementPlusResolverOptions = {},
142-
): ComponentResolver {
142+
): ComponentResolver[] {
143143
const optionsResolved: ElementPlusResolverOptionsResolved = {
144144
ssr: false,
145145
version: getPkgVersion('element-plus', '1.1.0-beta.21'),
@@ -148,12 +148,18 @@ export function ElementPlusResolver(
148148
...options,
149149
}
150150

151-
return (name: string, type) => {
152-
switch (type) {
153-
case 'component':
151+
return [
152+
{
153+
type: 'component',
154+
resolve: (name: string) => {
154155
return resolveComponent(name, optionsResolved)
155-
case 'directive':
156+
},
157+
},
158+
{
159+
type: 'directive',
160+
resolve: (name: string) => {
156161
return resolveDirective(name, optionsResolved)
157-
}
158-
}
162+
},
163+
},
164+
]
159165
}

src/core/resolvers/element-ui.ts

+14-11
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,22 @@ function getSideEffects(
4343
* @author @nabaonan
4444
*/
4545
export function ElementUiResolver(options: ElementUiResolverOptions = {}): ComponentResolver {
46-
return (name: string) => {
47-
if (name.startsWith('El')) {
48-
const compName = name.slice(2)
49-
const partialName = kebabCase(compName)
50-
if (partialName === 'collapse-transition') {
46+
return {
47+
type: 'component',
48+
resolve: (name: string) => {
49+
if (name.startsWith('El')) {
50+
const compName = name.slice(2)
51+
const partialName = kebabCase(compName)
52+
if (partialName === 'collapse-transition') {
53+
return {
54+
path: `element-ui/lib/transitions/${partialName}`,
55+
}
56+
}
5157
return {
52-
path: `element-ui/lib/transitions/${partialName}`,
58+
path: `element-ui/lib/${partialName}`,
59+
sideEffects: getSideEffects(partialName, options),
5360
}
5461
}
55-
return {
56-
path: `element-ui/lib/${partialName}`,
57-
sideEffects: getSideEffects(partialName, options),
58-
}
59-
}
62+
},
6063
}
6164
}

src/core/resolvers/headless-ui.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@ const components = [
4848
* @link https://github.com/tailwindlabs/headlessui
4949
*/
5050
export function HeadlessUiResolver(): ComponentResolver {
51-
return (name: string) => {
52-
if (components.includes(name))
53-
return { importName: name, path: '@headlessui/vue' }
51+
return {
52+
type: 'component',
53+
resolve: (name: string) => {
54+
if (components.includes(name))
55+
return { importName: name, path: '@headlessui/vue' }
56+
}
5457
}
5558
}

src/core/resolvers/idux.ts

+16-13
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,22 @@ export interface IduxResolverOptions {
3030
* @link https://idux.site
3131
*/
3232
export function IduxResolver(options: IduxResolverOptions = {}): ComponentResolver {
33-
return (name: string) => {
34-
if (name.match(/^Ix[A-Z]/)) {
35-
const { importStyle } = options
36-
const compName = name.slice(2)
37-
const kebabCaseName = kebabCase(compName)
38-
const isCdk = cdkNames.includes(kebabCaseName)
39-
const packageName = isCdk ? 'cdk' : 'components'
40-
const dirname = getDirname(kebabCaseName)
41-
const path = `@idux/${packageName}/${dirname}`
42-
const sideEffects = isCdk || !importStyle ? undefined : `${path}/style/${importStyle === 'css' ? 'css' : 'index'}`
43-
44-
return { importName: name, path, sideEffects }
45-
}
33+
return {
34+
type: 'component',
35+
resolve: (name: string) => {
36+
if (name.match(/^Ix[A-Z]/)) {
37+
const { importStyle } = options
38+
const compName = name.slice(2)
39+
const kebabCaseName = kebabCase(compName)
40+
const isCdk = cdkNames.includes(kebabCaseName)
41+
const packageName = isCdk ? 'cdk' : 'components'
42+
const dirname = getDirname(kebabCaseName)
43+
const path = `@idux/${packageName}/${dirname}`
44+
const sideEffects = isCdk || !importStyle ? undefined : `${path}/style/${importStyle === 'css' ? 'css' : 'index'}`
45+
46+
return { importName: name, path, sideEffects }
47+
}
48+
},
4649
}
4750
}
4851

src/core/resolvers/naive-ui.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ import { ComponentResolver } from '../../types'
77
* @link https://www.naiveui.com/
88
*/
99
export function NaiveUiResolver(): ComponentResolver {
10-
return (name: string) => {
11-
if (name.match(/^N[A-Z]/))
12-
return { importName: name, path: 'naive-ui' }
10+
return {
11+
type: 'component',
12+
resolve: (name: string) => {
13+
if (name.match(/^N[A-Z]/))
14+
return { importName: name, path: 'naive-ui' }
15+
},
1316
}
1417
}

src/core/resolvers/prime-vue.ts

+19-16
Original file line numberDiff line numberDiff line change
@@ -126,26 +126,29 @@ export interface PrimeVueResolverOptions {
126126
* @link https://github.com/primefaces/primevue
127127
*/
128128
export function PrimeVueResolver(options: PrimeVueResolverOptions = {}): ComponentResolver {
129-
return (name: string) => {
130-
const sideEffects: SideEffectsInfo = []
129+
return {
130+
type: 'component',
131+
resolve: (name: string) => {
132+
const sideEffects: SideEffectsInfo = []
131133

132-
if (options.importStyle)
133-
sideEffects.push('primevue/resources/primevue.min.css')
134+
if (options.importStyle)
135+
sideEffects.push('primevue/resources/primevue.min.css')
134136

135-
if (options.importIcons)
136-
sideEffects.push('primeicons/primeicons.css')
137+
if (options.importIcons)
138+
sideEffects.push('primeicons/primeicons.css')
137139

138-
if (options.importTheme) {
139-
sideEffects.push(
140-
`primevue/resources/themes/${options.importTheme}/theme.css`,
141-
)
142-
}
140+
if (options.importTheme) {
141+
sideEffects.push(
142+
`primevue/resources/themes/${options.importTheme}/theme.css`,
143+
)
144+
}
143145

144-
if (components.includes(name)) {
145-
return {
146-
path: `primevue/${name.toLowerCase()}/${name}.vue`,
147-
sideEffects,
146+
if (components.includes(name)) {
147+
return {
148+
path: `primevue/${name.toLowerCase()}/${name}.vue`,
149+
sideEffects,
150+
}
148151
}
149-
}
152+
},
150153
}
151154
}

src/core/resolvers/quasar.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,20 @@ import { ComponentResolver } from '../../types'
77
* @link https://github.com/quasarframework/quasar
88
*/
99
export function QuasarResolver(): ComponentResolver {
10-
return (name: string) => {
11-
let components = []
10+
return {
11+
type: 'component',
12+
resolve: (name: string) => {
13+
let components = []
1214

13-
try {
14-
/* eslint-disable @typescript-eslint/no-var-requires */
15-
components = require('quasar/dist/transforms/api-list.json')
16-
}
17-
catch (e) {
18-
}
15+
try {
16+
/* eslint-disable @typescript-eslint/no-var-requires */
17+
components = require('quasar/dist/transforms/api-list.json')
18+
}
19+
catch (e) {
20+
}
1921

20-
if (components.includes(name))
21-
return { importName: name, path: 'quasar' }
22+
if (components.includes(name))
23+
return { importName: name, path: 'quasar' }
24+
},
2225
}
2326
}

src/core/resolvers/vant.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,19 @@ export interface VantResolverOptions {
1616
* @link https://github.com/youzan/vant
1717
*/
1818
export function VantResolver(options: VantResolverOptions = {}): ComponentResolver {
19-
return (name: string) => {
20-
const { importStyle = true } = options
19+
return {
20+
type: 'component',
21+
resolve: (name: string) => {
22+
const { importStyle = true } = options
2123

22-
if (name.startsWith('Van')) {
23-
const partialName = name.slice(3)
24-
return {
25-
importName: partialName,
26-
path: 'vant/es',
27-
sideEffects: importStyle ? `vant/es/${kebabCase(partialName)}/style` : undefined,
24+
if (name.startsWith('Van')) {
25+
const partialName = name.slice(3)
26+
return {
27+
importName: partialName,
28+
path: 'vant/es',
29+
sideEffects: importStyle ? `vant/es/${kebabCase(partialName)}/style` : undefined,
30+
}
2831
}
29-
}
32+
},
3033
}
3134
}

0 commit comments

Comments
 (0)