Skip to content

Commit cda2228

Browse files
antfuhannoeru
andauthored
fix: normalize slashes (#23)
* fix: normalize slashes * fix: resolve dirs to relative path Co-authored-by: hanlee <[email protected]>
1 parent e79de12 commit cda2228

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

src/context.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { join, relative, resolve } from 'path'
1+
import { relative, resolve } from 'path'
22
import Debug from 'debug'
33
import chokidar from 'chokidar'
44
import { ResolvedConfig, UpdatePayload, ViteDevServer } from 'vite'
55
import { Options, ComponentInfo, ResolvedOptions } from './types'
6-
import { pascalCase, toArray, getNameFromFilePath, resolveAlias, resolveOptions, matchGlobs } from './utils'
6+
import { pascalCase, toArray, getNameFromFilePath, resolveAlias, resolveOptions, matchGlobs, slash } from './utils'
77
import { searchComponents } from './fs/glob'
88

99
const debug = {
@@ -25,19 +25,19 @@ export class Context {
2525
public readonly viteConfig: ResolvedConfig,
2626
) {
2727
this.options = resolveOptions(options, viteConfig)
28-
const { watchGlobs, dirs } = this.options
28+
const { globs, dirs } = this.options
2929

3030
if (viteConfig.command === 'serve') {
3131
// TODO: use vite's watcher instead
32-
chokidar.watch(dirs, { ignoreInitial: true })
32+
chokidar.watch(dirs, { ignoreInitial: true, cwd: this.root })
3333
.on('unlink', (path) => {
34-
if (matchGlobs(path, watchGlobs)) {
34+
if (matchGlobs(path, globs)) {
3535
this.removeComponents(path)
3636
this.onUpdate(path)
3737
}
3838
})
3939
.on('add', (path) => {
40-
if (matchGlobs(path, watchGlobs)) {
40+
if (matchGlobs(path, globs)) {
4141
this.addComponents(path)
4242
this.onUpdate(path)
4343
}
@@ -180,8 +180,8 @@ export class Context {
180180

181181
relative(path: string) {
182182
if (path.startsWith('/') && !path.startsWith(this.root))
183-
return path.slice(1).replace(/\\/g, '/')
184-
return relative(this.root, path).replace(/\\/g, '/')
183+
return slash(path.slice(1))
184+
return slash(relative(this.root, path))
185185
}
186186

187187
_searched = false

src/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ Required<Options>,
7676
extensions: string[]
7777
dirs: string[]
7878
globs: string[]
79-
watchGlobs: string[]
8079
}
8180

8281
export interface ComponentInfo {

src/utils.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { join, parse, resolve } from 'path'
1+
import { join, parse } from 'path'
22
import minimatch from 'minimatch'
33
import { ResolvedConfig } from 'vite'
44
import { ComponentInfo, ResolvedOptions, Options } from './types'
@@ -10,6 +10,10 @@ export interface ResolveComponent {
1010
namespace?: string
1111
}
1212

13+
export function slash(str: string) {
14+
return str.replace(/\\/g, '/')
15+
}
16+
1317
export function pascalCase(str: string) {
1418
return capitalize(camelCase(str))
1519
}
@@ -58,7 +62,7 @@ export function isEmpty(value: any) {
5862

5963
export function matchGlobs(filepath: string, globs: string[]) {
6064
for (const glob of globs) {
61-
if (minimatch(filepath, glob))
65+
if (minimatch(slash(filepath), glob))
6266
return true
6367
}
6468
return false
@@ -82,18 +86,12 @@ export function resolveOptions(options: Options, viteConfig: ResolvedConfig): Re
8286
? resolved.extensions
8387
: `{${resolved.extensions.join(',')}}`
8488

85-
resolved.globs = toArray(resolved.dirs).map(i =>
86-
resolved.deep
87-
? `${i}/**/*.${extsGlob}`
88-
: `${i}/*.${extsGlob}`,
89-
)
90-
91-
resolved.dirs = toArray(resolved.dirs).map(i => resolve(viteConfig.root, i))
89+
resolved.dirs = toArray(resolved.dirs)
9290

93-
resolved.watchGlobs = toArray(resolved.dirs).map(i =>
91+
resolved.globs = resolved.dirs.map(i =>
9492
resolved.deep
95-
? join(i, `/**/*.${extsGlob}`)
96-
: join(i, `/*.${extsGlob}`),
93+
? slash(join(i, `**/*.${extsGlob}`))
94+
: slash(join(i, `*.${extsGlob}`)),
9795
)
9896

9997
if (!resolved.extensions.length)
@@ -110,7 +108,7 @@ export function getNameFromFilePath(filePath: string, options: ResolvedOptions):
110108
let strippedPath = ''
111109

112110
// remove include directories from filepath
113-
for (const dir of toArray(dirs)) {
111+
for (const dir of dirs) {
114112
if (parsedFilePath.dir.startsWith(dir)) {
115113
strippedPath = parsedFilePath.dir.slice(dir.length)
116114
break

0 commit comments

Comments
 (0)