Skip to content

Commit

Permalink
fix(importMetaGlob): avoid unnecessary hmr of negative glob (#13646)
Browse files Browse the repository at this point in the history
Co-authored-by: fuhao.xu <fuhao.xu@yitu-inc.com>
  • Loading branch information
sun0day and fuhao.xu authored Jul 28, 2023
1 parent dd9d4c1 commit 844451c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
22 changes: 20 additions & 2 deletions packages/vite/src/node/plugins/importMetaGlob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ export interface ViteDevServer {
/**
* @internal
*/
_importGlobMap: Map<string, string[][]>
_importGlobMap: Map<string, { affirmed: string[]; negated: string[] }[]>
/**
* Deps that are externalized
* @internal
Expand Down
12 changes: 12 additions & 0 deletions playground/glob-import/__tests__/glob-import.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
4 changes: 4 additions & 0 deletions playground/glob-import/dir/index.js
Original file line number Diff line number Diff line change
@@ -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')

Expand Down

0 comments on commit 844451c

Please sign in to comment.