fix(importMetaGlob): avoid unnecessary hmr of negative glob (#13646)

Co-authored-by: fuhao.xu <fuhao.xu@yitu-inc.com>
This commit is contained in:
sun0day 2023-07-28 21:03:36 +08:00 committed by GitHub
parent dd9d4c1320
commit 844451c015
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 3 deletions

View File

@ -51,7 +51,14 @@ export function getAffectedGlobModules(
): ModuleNode[] {
const modules: ModuleNode[] = []
for (const [id, allGlobs] of server._importGlobMap!) {
if (allGlobs.some((glob) => isMatch(file, glob)))
// (glob1 || glob2) && !glob3 && !glob4...
if (
allGlobs.some(
({ affirmed, negated }) =>
(!affirmed.length || affirmed.some((glob) => isMatch(file, glob))) &&
(!negated.length || negated.every((glob) => isMatch(file, glob))),
)
)
modules.push(...(server.moduleGraph.getModulesByFile(id) || []))
}
modules.forEach((i) => {
@ -83,7 +90,18 @@ export function importGlobPlugin(config: ResolvedConfig): Plugin {
if (result) {
if (server) {
const allGlobs = result.matches.map((i) => i.globsResolved)
server._importGlobMap.set(id, allGlobs)
server._importGlobMap.set(
id,
allGlobs.map((globs) => {
const affirmed: string[] = []
const negated: string[] = []
for (const glob of globs) {
;(glob[0] === '!' ? negated : affirmed).push(glob)
}
return { affirmed, negated }
}),
)
}
return transformStableResult(result.s, id, config)
}

View File

@ -285,7 +285,7 @@ export interface ViteDevServer {
/**
* @internal
*/
_importGlobMap: Map<string, string[][]>
_importGlobMap: Map<string, { affirmed: string[]; negated: string[] }[]>
/**
* Deps that are externalized
* @internal

View File

@ -178,6 +178,18 @@ if (!isBuild) {
expect(JSON.parse(actualRemove)).toStrictEqual(allResult)
})
})
test('no hmr for adding/removing files', async () => {
let request = page.waitForResponse(/dir\/index\.js$/, { timeout: 200 })
addFile('nohmr.js', '')
let response = await request.catch(() => ({ status: () => -1 }))
expect(response.status()).toBe(-1)
request = page.waitForResponse(/dir\/index\.js$/, { timeout: 200 })
removeFile('nohmr.js')
response = await request.catch(() => ({ status: () => -1 }))
expect(response.status()).toBe(-1)
})
}
test('tree-shake eager css', async () => {

View File

@ -1,6 +1,10 @@
const modules = import.meta.glob('./*.(js|ts)', { eager: true })
const globWithAlias = import.meta.glob('@dir/al*.js', { eager: true })
// test negative glob
import.meta.glob(['@dir/*.js', '!@dir/x.js'])
import.meta.glob(['!@dir/x.js', '@dir/*.js'])
// test for sourcemap
console.log('hello')