Skip to content

Commit

Permalink
fix: handle HMR for files with more than one glob import (#3497)
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout authored May 21, 2021
1 parent b31604e commit 05bd96e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
11 changes: 8 additions & 3 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,16 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
str().prepend(importsString)
str().overwrite(expStart, endIndex, exp)
imports.forEach((url) => importedUrls.add(url.replace(base, '/')))
server._globImporters[importerModule.file!] = {
module: importerModule,
if (!(importerModule.file! in server._globImporters)) {
server._globImporters[importerModule.file!] = {
module: importerModule,
importGlobs: []
}
}
server._globImporters[importerModule.file!].importGlobs.push({
base,
pattern
}
})
}
continue
}
Expand Down
17 changes: 10 additions & 7 deletions packages/vite/src/node/server/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,16 @@ export async function handleFileAddUnlink(
delete server._globImporters[file]
} else {
for (const i in server._globImporters) {
const { module, base, pattern } = server._globImporters[i]
const relative = path.relative(base, file)
if (match(relative, pattern)) {
modules.push(module)
// We use `onFileChange` to invalidate `module.file` so that subsequent `ssrLoadModule()`
// calls get fresh glob import results with(out) the newly added(/removed) `file`.
server.moduleGraph.onFileChange(module.file!)
const { module, importGlobs } = server._globImporters[i]
for (const { base, pattern } of importGlobs) {
const relative = path.relative(base, file)
if (match(relative, pattern)) {
modules.push(module)
// We use `onFileChange` to invalidate `module.file` so that subsequent `ssrLoadModule()`
// calls get fresh glob import results with(out) the newly added(/removed) `file`.
server.moduleGraph.onFileChange(module.file!)
break
}
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,11 @@ export interface ViteDevServer {
_globImporters: Record<
string,
{
base: string
pattern: string
module: ModuleNode
importGlobs: {
base: string
pattern: string
}[]
}
>
/**
Expand Down

0 comments on commit 05bd96e

Please sign in to comment.