Skip to content

Commit bdfee5b

Browse files
committed
feat: generate declarations for resolvers
1 parent 63d574e commit bdfee5b

File tree

8 files changed

+54
-32
lines changed

8 files changed

+54
-32
lines changed

examples/naive-ui/components.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
declare module 'vue' {
55
export interface GlobalComponents {
6-
6+
NInput: typeof import('naive-ui')['NInput']
7+
NDatePicker: typeof import('naive-ui')['NDatePicker']
8+
NSpace: typeof import('naive-ui')['NSpace']
9+
NConfigProvider: typeof import('naive-ui')['NConfigProvider']
710
}
811
}
912

examples/naive-ui/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../tsconfig.json"
3+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
},
4040
"devDependencies": {
4141
"@antfu/eslint-config": "^0.6.6",
42-
"@antfu/utils": "^0.2.0",
42+
"@antfu/utils": "^0.2.1",
4343
"@types/debug": "^4.1.5",
4444
"@types/jest": "^26.0.23",
4545
"@types/minimatch": "^3.0.4",

pnpm-lock.yaml

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

src/context.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { relative } from 'path'
22
import Debug from 'debug'
33
import chokidar from 'chokidar'
44
import { ResolvedConfig, UpdatePayload, ViteDevServer } from 'vite'
5+
import { throttle } from '@antfu/utils'
56
import { Options, ComponentInfo, ResolvedOptions } from './types'
67
import { pascalCase, toArray, getNameFromFilePath, resolveAlias, resolveOptions, matchGlobs, slash } from './utils'
78
import { searchComponents } from './fs/glob'
@@ -11,6 +12,7 @@ const debug = {
1112
components: Debug('vite-plugin-components:context:components'),
1213
search: Debug('vite-plugin-components:context:search'),
1314
hmr: Debug('vite-plugin-components:context:hmr'),
15+
decleration: Debug('vite-plugin-components:decleration'),
1416
}
1517

1618
export class Context {
@@ -19,6 +21,7 @@ export class Context {
1921
private _componentPaths = new Set<string>()
2022
private _componentNameMap: Record<string, ComponentInfo> = {}
2123
private _componentUsageMap: Record<string, Set<string>> = {}
24+
private _componentCustomMap: Record<string, ComponentInfo> = {}
2225
private _server: ViteDevServer | undefined
2326

2427
constructor(
@@ -44,6 +47,8 @@ export class Context {
4447
}
4548
})
4649
}
50+
51+
this.generateDeclaration = throttle(500, false, this.generateDeclaration.bind(this))
4752
}
4853

4954
get root() {
@@ -80,6 +85,11 @@ export class Context {
8085
return false
8186
}
8287

88+
addCustomComponents(info: ComponentInfo) {
89+
if (info.name)
90+
this._componentCustomMap[info.name] = info
91+
}
92+
8393
removeComponents(paths: string | string[]) {
8494
debug.components('remove', paths)
8595

@@ -144,7 +154,7 @@ export class Context {
144154

145155
findComponent(name: string, excludePaths: string[] = []): ComponentInfo | undefined {
146156
// resolve from fs
147-
const info = this._componentNameMap[name]
157+
let info = this._componentNameMap[name]
148158
if (info && !excludePaths.includes(info.path) && !excludePaths.includes(info.path.slice(1)))
149159
return info
150160

@@ -153,16 +163,20 @@ export class Context {
153163
const result = resolver(name)
154164
if (result) {
155165
if (typeof result === 'string') {
156-
return {
166+
info = {
157167
name,
158168
path: result,
159169
}
170+
this.addCustomComponents(info)
171+
return info
160172
}
161173
else {
162-
return {
174+
info = {
163175
name,
164176
...result,
165177
}
178+
this.addCustomComponents(info)
179+
return info
166180
}
167181
}
168182
}
@@ -207,11 +221,18 @@ export class Context {
207221
}
208222

209223
generateDeclaration() {
210-
if (this.options.globalComponentsDeclaration)
211-
generateDeclaration(this, this.options.root, this.options.globalComponentsDeclaration)
224+
if (!this.options.globalComponentsDeclaration)
225+
return
226+
227+
debug.decleration('generating')
228+
generateDeclaration(this, this.options.root, this.options.globalComponentsDeclaration)
212229
}
213230

214231
get componentNameMap() {
215232
return this._componentNameMap
216233
}
234+
235+
get componentCustomMap() {
236+
return this._componentCustomMap
237+
}
217238
}

src/declaration.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { Context } from './context'
44
import { slash } from './utils'
55

66
export async function generateDeclaration(ctx: Context, root: string, filepath: string) {
7-
const lines = Object.values(ctx.componentNameMap)
7+
const lines = Object.values({
8+
...ctx.componentNameMap,
9+
...ctx.componentCustomMap,
10+
})
811
.map(({ path, name, importName }) => {
912
const related = slash(path).startsWith('/')
1013
? `./${relative(dirname(filepath), resolve(root, path.slice(1)))}`
@@ -17,6 +20,9 @@ export async function generateDeclaration(ctx: Context, root: string, filepath:
1720
return entry
1821
})
1922

23+
if (!lines.length)
24+
return
25+
2026
const code = `// generated by vite-plugin-components
2127
// read more https://github.com/vuejs/vue-next/pull/3399
2228

src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ function VitePluginComponents(options: Options = {}): Plugin {
2929
configureServer(server) {
3030
ctx.setServer(server)
3131
},
32-
transform(code, id) {
32+
async transform(code, id) {
3333
const { path, query } = parseId(id)
34-
return transformer(code, id, path, query)
34+
const result = await transformer(code, id, path, query)
35+
ctx.generateDeclaration()
36+
return result
3537
},
3638
}
3739
}

src/utils.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
import { join, parse, resolve } from 'path'
22
import minimatch from 'minimatch'
33
import { ResolvedConfig } from 'vite'
4+
import { slash, toArray } from '@antfu/utils'
45
import { ComponentInfo, ResolvedOptions, Options, ImportInfo } from './types'
56
import { LibraryResolver } from './helpers/libraryResolver'
67
import { defaultOptions } from './constants'
78
import { Context } from './context'
89

10+
export { slash, toArray }
11+
912
export interface ResolveComponent {
1013
filename: string
1114
namespace?: string
1215
}
1316

14-
export function slash(str: string) {
15-
return str.replace(/\\/g, '/')
16-
}
17-
1817
export function pascalCase(str: string) {
1918
return capitalize(camelCase(str))
2019
}
@@ -32,12 +31,6 @@ export function capitalize(str: string) {
3231
return str.charAt(0).toUpperCase() + str.slice(1)
3332
}
3433

35-
export function toArray<T>(arr: T | T[]): T[] {
36-
if (Array.isArray(arr))
37-
return arr
38-
return [arr]
39-
}
40-
4134
export function parseId(id: string) {
4235
const index = id.indexOf('?')
4336
if (index < 0) {

0 commit comments

Comments
 (0)