mirror of
https://github.com/vitejs/vite.git
synced 2024-11-21 22:59:10 +00:00
fix(importMetaGlob): avoid unnecessary hmr of negative glob (#13646)
Co-authored-by: fuhao.xu <fuhao.xu@yitu-inc.com>
This commit is contained in:
parent
dd9d4c1320
commit
844451c015
@ -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)
|
||||
}
|
||||
|
@ -285,7 +285,7 @@ export interface ViteDevServer {
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
_importGlobMap: Map<string, string[][]>
|
||||
_importGlobMap: Map<string, { affirmed: string[]; negated: string[] }[]>
|
||||
/**
|
||||
* Deps that are externalized
|
||||
* @internal
|
||||
|
@ -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 () => {
|
||||
|
@ -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')
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user