From 36c51eeabc8ba9dd6fe9527905da6a9a14e1ef5a Mon Sep 17 00:00:00 2001 From: hanlee Date: Fri, 11 Jun 2021 15:50:41 +0900 Subject: [PATCH 1/4] feat: generate declaration hmr support --- src/context.ts | 7 +++++++ src/index.ts | 4 +--- src/types.ts | 2 ++ src/utils.ts | 4 ++++ 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/context.ts b/src/context.ts index 7ff30251..e60e7b39 100644 --- a/src/context.ts +++ b/src/context.ts @@ -5,6 +5,7 @@ import { ResolvedConfig, UpdatePayload, ViteDevServer } from 'vite' import { Options, ComponentInfo, ResolvedOptions } from './types' import { pascalCase, toArray, getNameFromFilePath, resolveAlias, resolveOptions, matchGlobs, slash } from './utils' import { searchComponents } from './fs/glob' +import { generateDeclaration } from './declaration' const debug = { components: Debug('vite-plugin-components:context:components'), @@ -117,6 +118,8 @@ export class Context { if (payload.updates.length) this._server.ws.send(payload) + + this.generateDeclaration() } private updateComponentNameMap() { @@ -202,6 +205,10 @@ export class Context { this._searched = true } + generateDeclaration() { + generateDeclaration(this, this.options.root, this.options.globalComponentsDeclaration) + } + get componentNameMap() { return this._componentNameMap } diff --git a/src/index.ts b/src/index.ts index 3d2631a4..accd33d0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,6 @@ import { Context } from './context' import { parseId } from './utils' import { Vue3Transformer } from './transforms/vue3' import { Vue2Transformer } from './transforms/vue2' -import { generateDeclaration } from './declaration' function VitePluginComponents(options: Options = {}): Plugin { let ctx: Context @@ -26,8 +25,7 @@ function VitePluginComponents(options: Options = {}): Plugin { if (options.globalComponentsDeclaration) { ctx.searchGlob() - const path = resolve(config.root, typeof options.globalComponentsDeclaration === 'string' ? options.globalComponentsDeclaration : 'components.d.ts') - generateDeclaration(ctx, config.root, path) + ctx.generateDeclaration() } }, configureServer(server) { diff --git a/src/types.ts b/src/types.ts index 9c27531a..93beff56 100644 --- a/src/types.ts +++ b/src/types.ts @@ -112,6 +112,8 @@ Required, dirs: string[] resolvedDirs: string[] globs: string[] + globalComponentsDeclaration: string + root: string } export type ComponentsImportMap = Record diff --git a/src/utils.ts b/src/utils.ts index 51af20c2..a21019d0 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -120,6 +120,10 @@ export function resolveOptions(options: Options, viteConfig: ResolvedConfig): Re if (!resolved.extensions.length) throw new Error('[vite-plugin-components] extensions are required to search for components') + resolved.globalComponentsDeclaration = resolve(viteConfig.root, typeof options.globalComponentsDeclaration === 'string' ? options.globalComponentsDeclaration : 'components.d.ts') + + resolved.root = viteConfig.root + return resolved } From bf420ce4cf0604186f5b71baca4ea6d34bcd64a8 Mon Sep 17 00:00:00 2001 From: hanlee Date: Fri, 11 Jun 2021 16:13:32 +0900 Subject: [PATCH 2/4] fix: support for node v12 --- src/context.ts | 3 ++- src/declaration.ts | 2 +- src/index.ts | 2 -- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/context.ts b/src/context.ts index e60e7b39..f0243dcc 100644 --- a/src/context.ts +++ b/src/context.ts @@ -130,6 +130,7 @@ export class Context { .forEach((path) => { const name = pascalCase(getNameFromFilePath(path, this.options)) if (this._componentNameMap[name]) { + // eslint-disable-next-line no-console console.warn(`[vite-plugin-components] component "${name}"(${path}) has naming conflicts with other components, ignored.`) return } @@ -208,7 +209,7 @@ export class Context { generateDeclaration() { generateDeclaration(this, this.options.root, this.options.globalComponentsDeclaration) } - + get componentNameMap() { return this._componentNameMap } diff --git a/src/declaration.ts b/src/declaration.ts index 5a9e2633..08568f23 100644 --- a/src/declaration.ts +++ b/src/declaration.ts @@ -1,5 +1,5 @@ import { resolve, dirname, relative } from 'path' -import fs from 'fs/promises' +import { promises as fs } from 'fs' import { Context } from './context' import { slash } from './utils' diff --git a/src/index.ts b/src/index.ts index accd33d0..34dfc4a4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,3 @@ -import { resolve } from 'path' -import fs from 'fs/promises' import type { Plugin } from 'vite' import { Options, Transformer } from './types' import { Context } from './context' From fab7157caf2808209f6c5afd75114fe8c52bbf7a Mon Sep 17 00:00:00 2001 From: hanlee Date: Fri, 11 Jun 2021 16:58:11 +0900 Subject: [PATCH 3/4] fix: resolve falsy & check enable --- src/context.ts | 3 ++- src/types.ts | 2 +- src/utils.ts | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/context.ts b/src/context.ts index f0243dcc..e91dc1da 100644 --- a/src/context.ts +++ b/src/context.ts @@ -207,7 +207,8 @@ export class Context { } generateDeclaration() { - generateDeclaration(this, this.options.root, this.options.globalComponentsDeclaration) + if (this.options.globalComponentsDeclaration) + generateDeclaration(this, this.options.root, this.options.globalComponentsDeclaration) } get componentNameMap() { diff --git a/src/types.ts b/src/types.ts index 93beff56..b9800963 100644 --- a/src/types.ts +++ b/src/types.ts @@ -112,7 +112,7 @@ Required, dirs: string[] resolvedDirs: string[] globs: string[] - globalComponentsDeclaration: string + globalComponentsDeclaration: string | false root: string } diff --git a/src/utils.ts b/src/utils.ts index a21019d0..4021395d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -120,7 +120,7 @@ export function resolveOptions(options: Options, viteConfig: ResolvedConfig): Re if (!resolved.extensions.length) throw new Error('[vite-plugin-components] extensions are required to search for components') - resolved.globalComponentsDeclaration = resolve(viteConfig.root, typeof options.globalComponentsDeclaration === 'string' ? options.globalComponentsDeclaration : 'components.d.ts') + resolved.globalComponentsDeclaration = !options.globalComponentsDeclaration ? false : resolve(viteConfig.root, typeof options.globalComponentsDeclaration === 'string' ? options.globalComponentsDeclaration : 'components.d.ts') resolved.root = viteConfig.root From cc67edb22961e1f46b697c453e050c547a643318 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Fri, 11 Jun 2021 16:03:58 +0800 Subject: [PATCH 4/4] chore: update --- src/context.ts | 8 ++------ src/utils.ts | 10 ++++++++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/context.ts b/src/context.ts index e91dc1da..535c3467 100644 --- a/src/context.ts +++ b/src/context.ts @@ -119,7 +119,8 @@ export class Context { if (payload.updates.length) this._server.ws.send(payload) - this.generateDeclaration() + if (this.options.globalComponentsDeclaration) + generateDeclaration(this, this.options.root, this.options.globalComponentsDeclaration) } private updateComponentNameMap() { @@ -206,11 +207,6 @@ export class Context { this._searched = true } - generateDeclaration() { - if (this.options.globalComponentsDeclaration) - generateDeclaration(this, this.options.root, this.options.globalComponentsDeclaration) - } - get componentNameMap() { return this._componentNameMap } diff --git a/src/utils.ts b/src/utils.ts index 4021395d..7ea15be1 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -120,8 +120,14 @@ export function resolveOptions(options: Options, viteConfig: ResolvedConfig): Re if (!resolved.extensions.length) throw new Error('[vite-plugin-components] extensions are required to search for components') - resolved.globalComponentsDeclaration = !options.globalComponentsDeclaration ? false : resolve(viteConfig.root, typeof options.globalComponentsDeclaration === 'string' ? options.globalComponentsDeclaration : 'components.d.ts') - + resolved.globalComponentsDeclaration = !options.globalComponentsDeclaration + ? false + : resolve( + viteConfig.root, + typeof options.globalComponentsDeclaration === 'string' + ? options.globalComponentsDeclaration + : 'components.d.ts', + ) resolved.root = viteConfig.root return resolved