Skip to content

Commit

Permalink
feat: generate declarations for resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jun 11, 2021
1 parent 63d574e commit bdfee5b
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 32 deletions.
5 changes: 4 additions & 1 deletion examples/naive-ui/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

declare module 'vue' {
export interface GlobalComponents {

NInput: typeof import('naive-ui')['NInput']
NDatePicker: typeof import('naive-ui')['NDatePicker']
NSpace: typeof import('naive-ui')['NSpace']
NConfigProvider: typeof import('naive-ui')['NConfigProvider']
}
}

Expand Down
3 changes: 3 additions & 0 deletions examples/naive-ui/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../tsconfig.json"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
"devDependencies": {
"@antfu/eslint-config": "^0.6.6",
"@antfu/utils": "^0.2.0",
"@antfu/utils": "^0.2.1",
"@types/debug": "^4.1.5",
"@types/jest": "^26.0.23",
"@types/minimatch": "^3.0.4",
Expand Down
18 changes: 6 additions & 12 deletions pnpm-lock.yaml

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

31 changes: 26 additions & 5 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { relative } from 'path'
import Debug from 'debug'
import chokidar from 'chokidar'
import { ResolvedConfig, UpdatePayload, ViteDevServer } from 'vite'
import { throttle } from '@antfu/utils'
import { Options, ComponentInfo, ResolvedOptions } from './types'
import { pascalCase, toArray, getNameFromFilePath, resolveAlias, resolveOptions, matchGlobs, slash } from './utils'
import { searchComponents } from './fs/glob'
Expand All @@ -11,6 +12,7 @@ const debug = {
components: Debug('vite-plugin-components:context:components'),
search: Debug('vite-plugin-components:context:search'),
hmr: Debug('vite-plugin-components:context:hmr'),
decleration: Debug('vite-plugin-components:decleration'),
}

export class Context {
Expand All @@ -19,6 +21,7 @@ export class Context {
private _componentPaths = new Set<string>()
private _componentNameMap: Record<string, ComponentInfo> = {}
private _componentUsageMap: Record<string, Set<string>> = {}
private _componentCustomMap: Record<string, ComponentInfo> = {}
private _server: ViteDevServer | undefined

constructor(
Expand All @@ -44,6 +47,8 @@ export class Context {
}
})
}

this.generateDeclaration = throttle(500, false, this.generateDeclaration.bind(this))
}

get root() {
Expand Down Expand Up @@ -80,6 +85,11 @@ export class Context {
return false
}

addCustomComponents(info: ComponentInfo) {
if (info.name)
this._componentCustomMap[info.name] = info
}

removeComponents(paths: string | string[]) {
debug.components('remove', paths)

Expand Down Expand Up @@ -144,7 +154,7 @@ export class Context {

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

Expand All @@ -153,16 +163,20 @@ export class Context {
const result = resolver(name)
if (result) {
if (typeof result === 'string') {
return {
info = {
name,
path: result,
}
this.addCustomComponents(info)
return info
}
else {
return {
info = {
name,
...result,
}
this.addCustomComponents(info)
return info
}
}
}
Expand Down Expand Up @@ -207,11 +221,18 @@ export class Context {
}

generateDeclaration() {
if (this.options.globalComponentsDeclaration)
generateDeclaration(this, this.options.root, this.options.globalComponentsDeclaration)
if (!this.options.globalComponentsDeclaration)
return

debug.decleration('generating')
generateDeclaration(this, this.options.root, this.options.globalComponentsDeclaration)
}

get componentNameMap() {
return this._componentNameMap
}

get componentCustomMap() {
return this._componentCustomMap
}
}
8 changes: 7 additions & 1 deletion src/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { Context } from './context'
import { slash } from './utils'

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

if (!lines.length)
return

const code = `// generated by vite-plugin-components
// read more https://github.com/vuejs/vue-next/pull/3399
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ function VitePluginComponents(options: Options = {}): Plugin {
configureServer(server) {
ctx.setServer(server)
},
transform(code, id) {
async transform(code, id) {
const { path, query } = parseId(id)
return transformer(code, id, path, query)
const result = await transformer(code, id, path, query)
ctx.generateDeclaration()
return result
},
}
}
Expand Down
13 changes: 3 additions & 10 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { join, parse, resolve } from 'path'
import minimatch from 'minimatch'
import { ResolvedConfig } from 'vite'
import { slash, toArray } from '@antfu/utils'
import { ComponentInfo, ResolvedOptions, Options, ImportInfo } from './types'
import { LibraryResolver } from './helpers/libraryResolver'
import { defaultOptions } from './constants'
import { Context } from './context'

export { slash, toArray }

export interface ResolveComponent {
filename: string
namespace?: string
}

export function slash(str: string) {
return str.replace(/\\/g, '/')
}

export function pascalCase(str: string) {
return capitalize(camelCase(str))
}
Expand All @@ -32,12 +31,6 @@ export function capitalize(str: string) {
return str.charAt(0).toUpperCase() + str.slice(1)
}

export function toArray<T>(arr: T | T[]): T[] {
if (Array.isArray(arr))
return arr
return [arr]
}

export function parseId(id: string) {
const index = id.indexOf('?')
if (index < 0) {
Expand Down

0 comments on commit bdfee5b

Please sign in to comment.