Skip to content

Commit 8543b1a

Browse files
committed
chore: cleanup
1 parent 4d8a564 commit 8543b1a

File tree

4 files changed

+35
-33
lines changed

4 files changed

+35
-33
lines changed

src/context.ts

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const debug = {
1414

1515
export class Context {
1616
readonly options: ResolvedOptions
17-
readonly globs: string[]
1817

1918
private _componentPaths = new Set<string>()
2019
private _componentNameMap: Record<string, ComponentInfo> = {}
@@ -26,38 +25,19 @@ export class Context {
2625
public readonly viteConfig: ResolvedConfig,
2726
) {
2827
this.options = resolveOptions(options, viteConfig)
29-
const { extensions, dirs, deep } = this.options
30-
const exts = toArray(extensions)
31-
32-
if (!exts.length)
33-
throw new Error('[vite-plugin-components] extensions are required to search for components')
34-
35-
const extsGlob = exts.length === 1 ? exts[0] : `{${exts.join(',')}}`
36-
37-
this.globs = toArray(dirs).map(i =>
38-
deep
39-
? `${i}/**/*.${extsGlob}`
40-
: `${i}/*.${extsGlob}`,
41-
)
42-
43-
const watchDirs = toArray(dirs).map(i => resolve(viteConfig.root, i))
44-
const watchGlobs = toArray(watchDirs).map(i =>
45-
deep
46-
? join(i, `/**/*.${extsGlob}`)
47-
: join(i, `/*.${extsGlob}`),
48-
)
28+
const { globs, dirs } = this.options
4929

5030
if (viteConfig.command === 'serve') {
5131
// TODO: use vite's watcher instead
52-
chokidar.watch(watchDirs, { ignoreInitial: true })
32+
chokidar.watch(dirs, { ignoreInitial: true })
5333
.on('unlink', (path) => {
54-
if (matchGlobs(path, watchGlobs)) {
34+
if (matchGlobs(path, globs)) {
5535
this.removeComponents(path)
5636
this.onUpdate(path)
5737
}
5838
})
5939
.on('add', (path) => {
60-
if (matchGlobs(path, watchGlobs)) {
40+
if (matchGlobs(path, globs)) {
6141
this.addComponents(path)
6242
this.onUpdate(path)
6343
}

src/fs/glob.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import { Context } from '../context'
55
const debug = Debug('vite-plugin-components:glob')
66

77
export function searchComponents(ctx: Context) {
8-
debug(`started with: [${ctx.globs.join(', ')}]`)
8+
debug(`started with: [${ctx.options.globs.join(', ')}]`)
99
const root = ctx.root
1010

11-
const files = fg.sync(ctx.globs, {
11+
const files = fg.sync(ctx.options.globs, {
1212
ignore: ['node_modules'],
1313
onlyFiles: true,
1414
cwd: root,

src/types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,15 @@ export interface Options {
6767
customComponentResolvers?: ComponentResolver | ComponentResolver[]
6868
}
6969

70-
export type ResolvedOptions = Omit<Required<Options>, 'customComponentResolvers'|'libraries'> & {
70+
export type ResolvedOptions = Omit<
71+
Required<Options>,
72+
'customComponentResolvers'|'libraries'|'extensions'|'dirs'
73+
> & {
7174
customComponentResolvers: ComponentResolver[]
7275
libraries: UILibraryOptions[]
76+
extensions: string[]
77+
dirs: string[]
78+
globs: string[]
7379
}
7480

7581
export interface ComponentInfo {

src/utils.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { parse, resolve } from 'path'
1+
import { join, parse, resolve } from 'path'
22
import minimatch from 'minimatch'
33
import { ResolvedConfig } from 'vite'
44
import { ComponentInfo, ResolvedOptions, Options } from './types'
@@ -72,12 +72,28 @@ export function stringifyComponentImport({ name, path, importName }: ComponentIn
7272
}
7373

7474
export function resolveOptions(options: Options, viteConfig: ResolvedConfig): ResolvedOptions {
75-
const resolvedOptions = Object.assign({}, defaultOptions, options) as ResolvedOptions
76-
resolvedOptions.libraries = toArray(resolvedOptions.libraries).map(i => typeof i === 'string' ? { name: i } : i)
77-
resolvedOptions.customComponentResolvers = toArray(resolvedOptions.customComponentResolvers)
78-
resolvedOptions.customComponentResolvers.push(...resolvedOptions.libraries.map(lib => LibraryResolver(lib)))
75+
const resolved = Object.assign({}, defaultOptions, options) as ResolvedOptions
76+
resolved.libraries = toArray(resolved.libraries).map(i => typeof i === 'string' ? { name: i } : i)
77+
resolved.customComponentResolvers = toArray(resolved.customComponentResolvers)
78+
resolved.customComponentResolvers.push(...resolved.libraries.map(lib => LibraryResolver(lib)))
79+
resolved.extensions = toArray(resolved.extensions)
7980

80-
return resolvedOptions
81+
const extsGlob = resolved.extensions.length === 1
82+
? resolved.extensions
83+
: `{${resolved.extensions.join(',')}}`
84+
85+
resolved.globs = toArray(resolved.dirs).map(i =>
86+
resolved.deep
87+
? join(i, `/**/*.${extsGlob}`)
88+
: join(i, `/*.${extsGlob}`),
89+
)
90+
91+
resolved.dirs = toArray(resolved.dirs).map(i => resolve(viteConfig.root, i))
92+
93+
if (!resolved.extensions.length)
94+
throw new Error('[vite-plugin-components] extensions are required to search for components')
95+
96+
return resolved
8197
}
8298

8399
export function getNameFromFilePath(filePath: string, options: ResolvedOptions): string {

0 commit comments

Comments
 (0)