Skip to content

Commit b10dafa

Browse files
hannoeruantfu
andauthored
feat: generate declaration hmr support (#61)
Co-authored-by: Anthony Fu <[email protected]>
1 parent 5c08429 commit b10dafa

File tree

5 files changed

+19
-6
lines changed

5 files changed

+19
-6
lines changed

src/context.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ResolvedConfig, UpdatePayload, ViteDevServer } from 'vite'
55
import { Options, ComponentInfo, ResolvedOptions } from './types'
66
import { pascalCase, toArray, getNameFromFilePath, resolveAlias, resolveOptions, matchGlobs, slash } from './utils'
77
import { searchComponents } from './fs/glob'
8+
import { generateDeclaration } from './declaration'
89

910
const debug = {
1011
components: Debug('vite-plugin-components:context:components'),
@@ -117,6 +118,9 @@ export class Context {
117118

118119
if (payload.updates.length)
119120
this._server.ws.send(payload)
121+
122+
if (this.options.globalComponentsDeclaration)
123+
generateDeclaration(this, this.options.root, this.options.globalComponentsDeclaration)
120124
}
121125

122126
private updateComponentNameMap() {
@@ -127,6 +131,7 @@ export class Context {
127131
.forEach((path) => {
128132
const name = pascalCase(getNameFromFilePath(path, this.options))
129133
if (this._componentNameMap[name]) {
134+
// eslint-disable-next-line no-console
130135
console.warn(`[vite-plugin-components] component "${name}"(${path}) has naming conflicts with other components, ignored.`)
131136
return
132137
}

src/declaration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { resolve, dirname, relative } from 'path'
2-
import fs from 'fs/promises'
2+
import { promises as fs } from 'fs'
33
import { Context } from './context'
44
import { slash } from './utils'
55

src/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import { resolve } from 'path'
2-
import fs from 'fs/promises'
31
import type { Plugin } from 'vite'
42
import { Options, Transformer } from './types'
53
import { Context } from './context'
64
import { parseId } from './utils'
75
import { Vue3Transformer } from './transforms/vue3'
86
import { Vue2Transformer } from './transforms/vue2'
9-
import { generateDeclaration } from './declaration'
107

118
function VitePluginComponents(options: Options = {}): Plugin {
129
let ctx: Context
@@ -26,8 +23,7 @@ function VitePluginComponents(options: Options = {}): Plugin {
2623

2724
if (options.globalComponentsDeclaration) {
2825
ctx.searchGlob()
29-
const path = resolve(config.root, typeof options.globalComponentsDeclaration === 'string' ? options.globalComponentsDeclaration : 'components.d.ts')
30-
generateDeclaration(ctx, config.root, path)
26+
ctx.generateDeclaration()
3127
}
3228
},
3329
configureServer(server) {

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ Required<Options>,
112112
dirs: string[]
113113
resolvedDirs: string[]
114114
globs: string[]
115+
globalComponentsDeclaration: string | false
116+
root: string
115117
}
116118

117119
export type ComponentsImportMap = Record<string, string[] | undefined>

src/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ export function resolveOptions(options: Options, viteConfig: ResolvedConfig): Re
120120
if (!resolved.extensions.length)
121121
throw new Error('[vite-plugin-components] extensions are required to search for components')
122122

123+
resolved.globalComponentsDeclaration = !options.globalComponentsDeclaration
124+
? false
125+
: resolve(
126+
viteConfig.root,
127+
typeof options.globalComponentsDeclaration === 'string'
128+
? options.globalComponentsDeclaration
129+
: 'components.d.ts',
130+
)
131+
resolved.root = viteConfig.root
132+
123133
return resolved
124134
}
125135

0 commit comments

Comments
 (0)