Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/vite/src/node/plugins/importMetaGlob.ts
Expand Up @@ -51,7 +51,15 @@ 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((glob) =>
glob.reduce((match, g) => {
const m = isMatch(file, g)
return g[0] === '!' ? match && m : match || m
}, glob[0] === '!'),
)
)
sun0day marked this conversation as resolved.
Show resolved Hide resolved
modules.push(...(server.moduleGraph.getModulesByFile(id) || []))
}
modules.forEach((i) => {
Expand Down
12 changes: 12 additions & 0 deletions playground/glob-import/__tests__/glob-import.spec.ts
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
@@ -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