From b31604e08c4333bed5a3c4cc6876d1eb337a100b Mon Sep 17 00:00:00 2001 From: Romuald Brillout Date: Fri, 21 May 2021 14:42:56 +0200 Subject: [PATCH] fix: invalidate import globs upon new/removed files (fix #3499) (#3500) --- .../glob-import/__tests__/glob-import.spec.ts | 18 ++++++++++++++++-- packages/vite/src/node/server/hmr.ts | 3 +++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/packages/playground/glob-import/__tests__/glob-import.spec.ts b/packages/playground/glob-import/__tests__/glob-import.spec.ts index 15ffb4c7d97ede..4377f7961ef229 100644 --- a/packages/playground/glob-import/__tests__/glob-import.spec.ts +++ b/packages/playground/glob-import/__tests__/glob-import.spec.ts @@ -59,7 +59,13 @@ if (!isBuild) { JSON.stringify( { '/dir/a.js': {}, - ...allResult + ...allResult, + '/dir/index.js': { + modules: { + './a.js': {}, + ...allResult['/dir/index.js'].modules + } + } }, null, 2 @@ -75,7 +81,15 @@ if (!isBuild) { '/dir/a.js': { msg: 'a' }, - ...allResult + ...allResult, + '/dir/index.js': { + modules: { + './a.js': { + msg: 'a' + }, + ...allResult['/dir/index.js'].modules + } + } }, null, 2 diff --git a/packages/vite/src/node/server/hmr.ts b/packages/vite/src/node/server/hmr.ts index c7849d8c78c49d..da9222cf4b139e 100644 --- a/packages/vite/src/node/server/hmr.ts +++ b/packages/vite/src/node/server/hmr.ts @@ -181,6 +181,9 @@ export async function handleFileAddUnlink( 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!) } } }