From a3e74e3f0c758896294407c9207be3e2553bb31f Mon Sep 17 00:00:00 2001 From: tuchg Date: Sun, 23 Oct 2022 16:51:02 +0800 Subject: [PATCH] fix(hmr): cannot reload after missing import on server startup (#9534) --- packages/vite/src/node/server/hmr.ts | 14 +++++++------- playground/hmr/__tests__/hmr.spec.ts | 24 ++++++++++++++++++++++++ playground/hmr/missing-import/a.js | 3 +++ playground/hmr/missing-import/index.html | 2 ++ playground/hmr/missing-import/main.js | 1 + 5 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 playground/hmr/missing-import/a.js create mode 100644 playground/hmr/missing-import/index.html create mode 100644 playground/hmr/missing-import/main.js diff --git a/packages/vite/src/node/server/hmr.ts b/packages/vite/src/node/server/hmr.ts index e5d93fad8629de..913300ff1e61b8 100644 --- a/packages/vite/src/node/server/hmr.ts +++ b/packages/vite/src/node/server/hmr.ts @@ -177,15 +177,15 @@ export function updateModules( if (updates.length === 0) { debugHmr(colors.yellow(`no update happened `) + colors.dim(file)) - return + } else { + config.logger.info( + updates + .map(({ path }) => colors.green(`hmr update `) + colors.dim(path)) + .join('\n'), + { clear: true, timestamp: true } + ) } - config.logger.info( - updates - .map(({ path }) => colors.green(`hmr update `) + colors.dim(path)) - .join('\n'), - { clear: true, timestamp: true } - ) ws.send({ type: 'update', updates 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'