diff --git a/packages/vite/src/node/plugins/importAnalysis.ts b/packages/vite/src/node/plugins/importAnalysis.ts index ae4a7b556d00c2..d26b0e219085d9 100644 --- a/packages/vite/src/node/plugins/importAnalysis.ts +++ b/packages/vite/src/node/plugins/importAnalysis.ts @@ -294,6 +294,8 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { if (ssr) { return [url, url] } + // fix#9534, prevent the importerModuleNode being stopped from propagating updates + importerModule.isSelfAccepting = false return this.error( `Failed to resolve import "${url}" from "${path.relative( process.cwd(), diff --git a/playground/hmr/__tests__/hmr.spec.ts b/playground/hmr/__tests__/hmr.spec.ts index 143adea310f0d1..c5d1950408ff04 100644 --- a/playground/hmr/__tests__/hmr.spec.ts +++ b/playground/hmr/__tests__/hmr.spec.ts @@ -670,4 +670,28 @@ if (!isBuild) { return await el.textContent() }, '[wow]1') }) + + test('keep hmr reload after missing import on server startup', async () => { + const file = 'missing-import/a.js' + const importCode = "import 'missing-modules'" + const unImportCode = `// ${importCode}` + const timeout = 2000 + + await page.goto(viteTestUrl + '/missing-import/index.html') + + browserLogs.length = 0 + expect(browserLogs).toMatchObject([]) + + editFile(file, (code) => code.replace(importCode, unImportCode)) + + await page.waitForNavigation({ timeout }) + expect(browserLogs.some((msg) => msg.match('missing test'))).toBe(true) + browserLogs.length = 0 + + editFile(file, (code) => code.replace(unImportCode, importCode)) + + await page.waitForNavigation({ timeout }) + expect(browserLogs.some((msg) => msg.includes('500'))).toBe(true) + browserLogs.length = 0 + }) } diff --git a/playground/hmr/missing-import/a.js b/playground/hmr/missing-import/a.js new file mode 100644 index 00000000000000..886fc4ff2ef541 --- /dev/null +++ b/playground/hmr/missing-import/a.js @@ -0,0 +1,3 @@ +import 'missing-modules' + +console.log('missing test') diff --git a/playground/hmr/missing-import/index.html b/playground/hmr/missing-import/index.html new file mode 100644 index 00000000000000..cfbd07a1e44286 --- /dev/null +++ b/playground/hmr/missing-import/index.html @@ -0,0 +1,2 @@ +
Page
+ diff --git a/playground/hmr/missing-import/main.js b/playground/hmr/missing-import/main.js new file mode 100644 index 00000000000000..999801e4dd1061 --- /dev/null +++ b/playground/hmr/missing-import/main.js @@ -0,0 +1 @@ +import './a.js'