Skip to content

Commit 844451c

Browse files
sun0dayfuhao.xu
and
fuhao.xu
authored
fix(importMetaGlob): avoid unnecessary hmr of negative glob (#13646)
Co-authored-by: fuhao.xu <fuhao.xu@yitu-inc.com>
1 parent dd9d4c1 commit 844451c

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

packages/vite/src/node/plugins/importMetaGlob.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,14 @@ export function getAffectedGlobModules(
5151
): ModuleNode[] {
5252
const modules: ModuleNode[] = []
5353
for (const [id, allGlobs] of server._importGlobMap!) {
54-
if (allGlobs.some((glob) => isMatch(file, glob)))
54+
// (glob1 || glob2) && !glob3 && !glob4...
55+
if (
56+
allGlobs.some(
57+
({ affirmed, negated }) =>
58+
(!affirmed.length || affirmed.some((glob) => isMatch(file, glob))) &&
59+
(!negated.length || negated.every((glob) => isMatch(file, glob))),
60+
)
61+
)
5562
modules.push(...(server.moduleGraph.getModulesByFile(id) || []))
5663
}
5764
modules.forEach((i) => {
@@ -83,7 +90,18 @@ export function importGlobPlugin(config: ResolvedConfig): Plugin {
8390
if (result) {
8491
if (server) {
8592
const allGlobs = result.matches.map((i) => i.globsResolved)
86-
server._importGlobMap.set(id, allGlobs)
93+
server._importGlobMap.set(
94+
id,
95+
allGlobs.map((globs) => {
96+
const affirmed: string[] = []
97+
const negated: string[] = []
98+
99+
for (const glob of globs) {
100+
;(glob[0] === '!' ? negated : affirmed).push(glob)
101+
}
102+
return { affirmed, negated }
103+
}),
104+
)
87105
}
88106
return transformStableResult(result.s, id, config)
89107
}

packages/vite/src/node/server/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ export interface ViteDevServer {
285285
/**
286286
* @internal
287287
*/
288-
_importGlobMap: Map<string, string[][]>
288+
_importGlobMap: Map<string, { affirmed: string[]; negated: string[] }[]>
289289
/**
290290
* Deps that are externalized
291291
* @internal

playground/glob-import/__tests__/glob-import.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,18 @@ if (!isBuild) {
178178
expect(JSON.parse(actualRemove)).toStrictEqual(allResult)
179179
})
180180
})
181+
182+
test('no hmr for adding/removing files', async () => {
183+
let request = page.waitForResponse(/dir\/index\.js$/, { timeout: 200 })
184+
addFile('nohmr.js', '')
185+
let response = await request.catch(() => ({ status: () => -1 }))
186+
expect(response.status()).toBe(-1)
187+
188+
request = page.waitForResponse(/dir\/index\.js$/, { timeout: 200 })
189+
removeFile('nohmr.js')
190+
response = await request.catch(() => ({ status: () => -1 }))
191+
expect(response.status()).toBe(-1)
192+
})
181193
}
182194

183195
test('tree-shake eager css', async () => {

playground/glob-import/dir/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
const modules = import.meta.glob('./*.(js|ts)', { eager: true })
22
const globWithAlias = import.meta.glob('@dir/al*.js', { eager: true })
33

4+
// test negative glob
5+
import.meta.glob(['@dir/*.js', '!@dir/x.js'])
6+
import.meta.glob(['!@dir/x.js', '@dir/*.js'])
7+
48
// test for sourcemap
59
console.log('hello')
610

0 commit comments

Comments
 (0)