Skip to content

Commit 533d4da

Browse files
authored
fix: dirs hot reload without wildcards (#588)
1 parent 2d375d4 commit 533d4da

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/core/ctx.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import type { BiomeLintrc, ESLintrc, ImportExtended, Options } from '../types'
33
import { existsSync, promises as fs } from 'node:fs'
44
import { dirname, isAbsolute, relative, resolve } from 'node:path'
55
import process from 'node:process'
6-
import { slash, throttle, toArray } from '@antfu/utils'
6+
import { isString, slash, throttle, toArray } from '@antfu/utils'
77
import { isPackageExists } from 'local-pkg'
88
import MagicString from 'magic-string'
9-
import { createUnimport, resolvePreset } from 'unimport'
9+
import { createUnimport, normalizeScanDirs, resolvePreset } from 'unimport'
1010
import { createFilter } from 'unplugin-utils'
1111
import { presets } from '../presets'
1212
import { generateBiomeLintConfigs } from './biomelintrc'
@@ -276,6 +276,22 @@ ${dts}`.trim()}\n`
276276
}
277277
}
278278

279+
const configFilePaths = [
280+
dts,
281+
eslintrc.filepath,
282+
biomelintrc.filepath,
283+
dumpUnimportItems,
284+
]
285+
.filter(isString)
286+
.map(path => resolve(root, path))
287+
288+
const normalizedDirPaths = dirs?.length
289+
? dirs.flatMap(dir => normalizeScanDirs([dir], {
290+
...dirsScanOptions,
291+
cwd: root,
292+
}))
293+
: []
294+
279295
return {
280296
root,
281297
dirs,
@@ -287,6 +303,8 @@ ${dts}`.trim()}\n`
287303
generateDTS,
288304
generateESLint,
289305
unimport,
306+
configFilePaths,
307+
normalizedDirPaths,
290308
}
291309
}
292310

src/core/unplugin.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { FilterPattern } from 'unplugin'
22
import type { Options } from '../types'
3-
import path from 'node:path'
43
import { slash } from '@antfu/utils'
54
import { isPackageExists } from 'local-pkg'
65
import pm from 'picomatch'
@@ -51,8 +50,19 @@ export default createUnplugin<Options>((options) => {
5150
}
5251
},
5352
async handleHotUpdate({ file }) {
54-
const relativeFile = path.relative(ctx.root, slash(file))
55-
if (ctx.dirs?.some(dir => pm.isMatch(slash(relativeFile), slash(typeof dir === 'string' ? dir : dir.glob))))
53+
if (!ctx.dirs?.length)
54+
return
55+
56+
if (ctx.configFilePaths.includes(file))
57+
return
58+
59+
const normalizedFilePath = slash(file)
60+
61+
const shouldRescan = ctx.normalizedDirPaths.some(dirPath =>
62+
pm.isMatch(normalizedFilePath, dirPath.glob),
63+
)
64+
65+
if (shouldRescan)
5666
await ctx.scanDirs()
5767
},
5868
async configResolved(config) {

0 commit comments

Comments
 (0)