Skip to content

feat: async component resolver #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"test:update": "jest --u"
},
"dependencies": {
"@antfu/utils": "^0.3.0",
"@rollup/pluginutils": "^4.1.1",
"chokidar": "^3.5.2",
"debug": "^4.3.2",
Expand All @@ -72,7 +73,6 @@
},
"devDependencies": {
"@antfu/eslint-config": "^0.9.0",
"@antfu/utils": "^0.3.0",
"@types/debug": "^4.1.7",
"@types/jest": "^27.0.2",
"@types/minimatch": "^3.0.5",
Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 2 additions & 8 deletions src/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,15 @@ export class Context {
})
}

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

// custom resolvers
for (const resolver of this.options.resolvers) {
const result = resolver(name)
const result = await resolver(name)
if (result) {
if (typeof result === 'string') {
info = {
Expand All @@ -213,12 +213,6 @@ export class Context {
return undefined
}

findComponents(names: string[], excludePaths: string[] = []) {
return names
.map(name => this.findComponent(name, excludePaths))
.filter(Boolean) as ComponentInfo[]
}

normalizePath(path: string) {
// @ts-expect-error backward compatibility
return resolveAlias(path, this.viteConfig?.resolve?.alias || this.viteConfig?.alias || [])
Expand Down
4 changes: 2 additions & 2 deletions src/core/transforms/vue2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { pascalCase, stringifyComponentImport } from '../utils'
const debug = Debug('unplugin-vue-components:transform:vue2')

export function Vue2Transformer(ctx: Context): Transformer {
return (code, id, path, query) => {
return async(code, id, path, query) => {
ctx.searchGlob()

const sfcPath = ctx.normalizePath(path)
Expand All @@ -30,7 +30,7 @@ export function Vue2Transformer(ctx: Context): Transformer {
debug(`| ${matchedName}`)
const name = pascalCase(matchedName)
componentPaths.push(name)
const component = ctx.findComponent(name, [sfcPath], matchedName)
const component = await ctx.findComponent(name, [sfcPath], matchedName)
if (component) {
const var_name = `__unplugin_components_${no}`
head.push(stringifyComponentImport({ ...component, name: var_name }, ctx))
Expand Down
4 changes: 2 additions & 2 deletions src/core/transforms/vue3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { pascalCase, stringifyComponentImport } from '../utils'
const debug = Debug('unplugin-vue-components:transform:vue3')

export function Vue3Transformer(ctx: Context): Transformer {
return (code, id, path, query) => {
return async(code, id, path, query) => {
ctx.searchGlob()

const sfcPath = ctx.normalizePath(path)
Expand All @@ -29,7 +29,7 @@ export function Vue3Transformer(ctx: Context): Transformer {
debug(`| ${matchedName}`)
const name = pascalCase(matchedName)
componentPaths.push(name)
const component = ctx.findComponent(name, [sfcPath], matchedName)
const component = await ctx.findComponent(name, [sfcPath], matchedName)
if (component) {
const var_name = `__unplugin_components_${no}`
head.push(stringifyComponentImport({ ...component, name: var_name }, ctx))
Expand Down
7 changes: 4 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { FilterPattern } from '@rollup/pluginutils'
import type { TransformResult } from 'unplugin'
import type { Awaitable } from '@antfu/utils'

export interface ImportInfo {
name?: string
Expand All @@ -13,9 +14,9 @@ export interface ComponentInfo extends ImportInfo {
sideEffects?: SideEffectsInfo
}

export type ComponentResolveResult = string | ComponentInfo
export type ComponentResolveResult = Awaitable<string | ComponentInfo | null | undefined | void>

export type ComponentResolver = (name: string) => ComponentResolveResult | null | undefined | void
export type ComponentResolver = (name: string) => ComponentResolveResult

export interface UILibraryOptions {
name: string
Expand All @@ -25,7 +26,7 @@ export interface UILibraryOptions {

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

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

/**
* Plugin options.
Expand Down