Skip to content

Commit 2423418

Browse files
userquinantfu
andauthored
feat: async component resolver (#151)
Co-authored-by: Anthony Fu <[email protected]>
1 parent a5dc2f2 commit 2423418

File tree

6 files changed

+14
-19
lines changed

6 files changed

+14
-19
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"test:update": "jest --u"
6161
},
6262
"dependencies": {
63+
"@antfu/utils": "^0.3.0",
6364
"@rollup/pluginutils": "^4.1.1",
6465
"chokidar": "^3.5.2",
6566
"debug": "^4.3.2",
@@ -72,7 +73,6 @@
7273
},
7374
"devDependencies": {
7475
"@antfu/eslint-config": "^0.9.0",
75-
"@antfu/utils": "^0.3.0",
7676
"@types/debug": "^4.1.7",
7777
"@types/jest": "^27.0.2",
7878
"@types/minimatch": "^3.0.5",

pnpm-lock.yaml

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/context.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,15 @@ export class Context {
181181
})
182182
}
183183

184-
findComponent(name: string, excludePaths: string[] = [], rawName?: string): ComponentInfo | undefined {
184+
async findComponent(name: string, excludePaths: string[] = [], rawName?: string): Promise<ComponentInfo | undefined> {
185185
// resolve from fs
186186
let info = this._componentNameMap[name]
187187
if (info && !excludePaths.includes(info.path) && !excludePaths.includes(info.path.slice(1)))
188188
return info
189189

190190
// custom resolvers
191191
for (const resolver of this.options.resolvers) {
192-
const result = resolver(name)
192+
const result = await resolver(name)
193193
if (result) {
194194
if (typeof result === 'string') {
195195
info = {
@@ -213,12 +213,6 @@ export class Context {
213213
return undefined
214214
}
215215

216-
findComponents(names: string[], excludePaths: string[] = []) {
217-
return names
218-
.map(name => this.findComponent(name, excludePaths))
219-
.filter(Boolean) as ComponentInfo[]
220-
}
221-
222216
normalizePath(path: string) {
223217
// @ts-expect-error backward compatibility
224218
return resolveAlias(path, this.viteConfig?.resolve?.alias || this.viteConfig?.alias || [])

src/core/transforms/vue2.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { pascalCase, stringifyComponentImport } from '../utils'
99
const debug = Debug('unplugin-vue-components:transform:vue2')
1010

1111
export function Vue2Transformer(ctx: Context): Transformer {
12-
return (code, id, path, query) => {
12+
return async(code, id, path, query) => {
1313
ctx.searchGlob()
1414

1515
const sfcPath = ctx.normalizePath(path)
@@ -30,7 +30,7 @@ export function Vue2Transformer(ctx: Context): Transformer {
3030
debug(`| ${matchedName}`)
3131
const name = pascalCase(matchedName)
3232
componentPaths.push(name)
33-
const component = ctx.findComponent(name, [sfcPath], matchedName)
33+
const component = await ctx.findComponent(name, [sfcPath], matchedName)
3434
if (component) {
3535
const var_name = `__unplugin_components_${no}`
3636
head.push(stringifyComponentImport({ ...component, name: var_name }, ctx))

src/core/transforms/vue3.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { pascalCase, stringifyComponentImport } from '../utils'
99
const debug = Debug('unplugin-vue-components:transform:vue3')
1010

1111
export function Vue3Transformer(ctx: Context): Transformer {
12-
return (code, id, path, query) => {
12+
return async(code, id, path, query) => {
1313
ctx.searchGlob()
1414

1515
const sfcPath = ctx.normalizePath(path)
@@ -29,7 +29,7 @@ export function Vue3Transformer(ctx: Context): Transformer {
2929
debug(`| ${matchedName}`)
3030
const name = pascalCase(matchedName)
3131
componentPaths.push(name)
32-
const component = ctx.findComponent(name, [sfcPath], matchedName)
32+
const component = await ctx.findComponent(name, [sfcPath], matchedName)
3333
if (component) {
3434
const var_name = `__unplugin_components_${no}`
3535
head.push(stringifyComponentImport({ ...component, name: var_name }, ctx))

src/types.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { FilterPattern } from '@rollup/pluginutils'
22
import type { TransformResult } from 'unplugin'
3+
import type { Awaitable } from '@antfu/utils'
34

45
export interface ImportInfo {
56
name?: string
@@ -13,9 +14,9 @@ export interface ComponentInfo extends ImportInfo {
1314
sideEffects?: SideEffectsInfo
1415
}
1516

16-
export type ComponentResolveResult = string | ComponentInfo
17+
export type ComponentResolveResult = Awaitable<string | ComponentInfo | null | undefined | void>
1718

18-
export type ComponentResolver = (name: string) => ComponentResolveResult | null | undefined | void
19+
export type ComponentResolver = (name: string) => ComponentResolveResult
1920

2021
export interface UILibraryOptions {
2122
name: string
@@ -25,7 +26,7 @@ export interface UILibraryOptions {
2526

2627
export type Matcher = (id: string) => boolean | null | undefined
2728

28-
export type Transformer = (code: string, id: string, path: string, query: Record<string, string>) => TransformResult | null | Promise<null | TransformResult>
29+
export type Transformer = (code: string, id: string, path: string, query: Record<string, string>) => Awaitable<TransformResult | null>
2930

3031
/**
3132
* Plugin options.

0 commit comments

Comments
 (0)