Skip to content

Commit b763174

Browse files
committed
fix: lifecycle order
1 parent 46dc2bf commit b763174

File tree

8 files changed

+55
-28
lines changed

8 files changed

+55
-28
lines changed

src/context.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { ComponentsInfo, ComponentsImportMap, Options } from './types'
2+
3+
export class Context {
4+
importMap: ComponentsImportMap = {}
5+
components: ComponentsInfo[] = []
6+
_searchingPromise?: Promise<any>
7+
8+
private importMapPromises: Record<string, [(null | Promise<string[]>), (null | ((result: string[]) => void))]> = {}
9+
10+
constructor(
11+
public readonly options: Options,
12+
) {}
13+
14+
async getImportMap(key: string) {
15+
if (this.importMap[key])
16+
return this.importMap[key]
17+
18+
if (!this.importMapPromises[key]) {
19+
this.importMapPromises[key] = [null, null]
20+
const p = new Promise<string[]>((resolve) => {
21+
this.importMapPromises[key][1] = resolve
22+
})
23+
this.importMapPromises[key][0] = p
24+
}
25+
26+
return await Promise.resolve(this.importMapPromises[key][0])
27+
}
28+
29+
setImportMap(key: string, names: string[]) {
30+
this.importMap[key] = names
31+
if (this.importMapPromises[key])
32+
this.importMapPromises[key][1]?.(names)
33+
}
34+
}

src/generator/resolver.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Debug from 'debug'
2-
import { Context } from '../types'
2+
import { Context } from '../context'
33
import { RESOLVER_EXT } from '../constants'
44

55
const debug = Debug('vite-plugin-components:resolver')
@@ -8,13 +8,13 @@ export function isResolverPath(reqPath: string) {
88
return reqPath.endsWith(RESOLVER_EXT)
99
}
1010

11-
export function generateResolver(ctx: Context, reqPath: string) {
11+
export async function generateResolver(ctx: Context, reqPath: string) {
1212
const sfcPath = reqPath.slice(0, -RESOLVER_EXT.length)
13-
const names = ctx.importMap[sfcPath] || []
13+
const names = await ctx.getImportMap(sfcPath) || []
1414
const components = ctx.components.filter(i => names.includes(i[0]) && i[1] !== sfcPath)
1515

16-
debug(`resolving ${sfcPath}`)
17-
debug(`components [${names.join(', ')}]`)
16+
debug(sfcPath)
17+
debug('using', names, 'imported', components.map(i => i[0]))
1818

1919
return `
2020
${components.map(([name, path]) => `import ${name} from "${path}"`).join('\n')}

src/glob.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import path from 'path'
22
import fg from 'fast-glob'
33
import Debug from 'debug'
4-
import { Context, ComponentsInfo } from './types'
4+
import { ComponentsInfo } from './types'
5+
import { Context } from './context'
56

67
const debug = Debug('vite-plugin-components:glob')
78

@@ -52,11 +53,11 @@ export async function searchComponents(ctx: Context, force = false) {
5253
const components: ComponentsInfo[] = files.map(f => [path.parse(f).name, `/${f}`])
5354

5455
debug(`${components.length} components found.`)
55-
debug(`[${components.map(i => i).join(', ')}]`)
56+
debug(components.map(i => i[0]))
5657

5758
ctx.components = components
5859
})()
5960
}
6061

61-
return await ctx._searchingPromise
62+
await Promise.resolve(ctx._searchingPromise)
6263
}

src/index.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import type { Plugin } from 'vite'
22
import { createRollupPlugin } from './plugins/build'
33
import { createServerPlugin } from './plugins/server'
4-
import { Options, Context } from './types'
4+
import { Options } from './types'
55
import { VueScriptTransformer } from './transforms/vueScript'
66
import { VueTemplateTransformer } from './transforms/vueTemplate'
7+
import { Context } from './context'
78

89
const defaultOptions: Options = {
910
dirs: 'src/components',
@@ -13,11 +14,7 @@ const defaultOptions: Options = {
1314

1415
function VitePluginComponents(options: Partial<Options> = {}): Plugin {
1516
const resolvedOptions: Options = Object.assign({}, options, defaultOptions)
16-
const ctx: Context = {
17-
options: resolvedOptions,
18-
importMap: {},
19-
components: [],
20-
}
17+
const ctx: Context = new Context(resolvedOptions)
2118

2219
return {
2320
configureServer: createServerPlugin(ctx),

src/state.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/transforms/vueScript.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Transform } from 'vite'
2-
import { Context } from '../types'
2+
import { Context } from '../context'
33
import { RESOLVER_EXT } from '../constants'
44

55
/**

src/transforms/vueTemplate.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
import { Transform } from 'vite'
2-
import { Context } from '../types'
1+
import Debug from 'debug'
2+
import type { Transform } from 'vite'
3+
import { Context } from '../context'
4+
5+
const debug = Debug('vite-plugin-components:transform:template')
36

47
/**
58
* This transformer does not actually change the code,
@@ -15,7 +18,9 @@ export function VueTemplateTransformer(ctx: Context): Transform {
1518
},
1619
transform({ code, path }) {
1720
const imports = Array.from(code.matchAll(/_resolveComponent\("(.*)"\)/g)).map(i => i[1])
18-
ctx.importMap[path] = imports
21+
ctx.setImportMap(path, imports)
22+
debug(path)
23+
debug(imports)
1924
return code
2025
},
2126
}

src/types.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,3 @@ export interface Options {
2121

2222
export type ComponentsInfo = [string, string]
2323
export type ComponentsImportMap = Record<string, string[] | undefined>
24-
25-
export interface Context {
26-
options: Options
27-
importMap: ComponentsImportMap
28-
components: ComponentsInfo[]
29-
_searchingPromise?: Promise<any>
30-
}

0 commit comments

Comments
 (0)