Skip to content

Commit

Permalink
fix(hmr): cannot reload after missing import on server startup (#9534) (
Browse files Browse the repository at this point in the history
  • Loading branch information
tuchg committed Oct 25, 2022
1 parent df86990 commit ee7c28a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/vite/src/node/plugins/importAnalysis.ts
Expand Up @@ -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(),
Expand Down
24 changes: 24 additions & 0 deletions playground/hmr/__tests__/hmr.spec.ts
Expand Up @@ -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
})
}
3 changes: 3 additions & 0 deletions playground/hmr/missing-import/a.js
@@ -0,0 +1,3 @@
import 'missing-modules'

console.log('missing test')
2 changes: 2 additions & 0 deletions playground/hmr/missing-import/index.html
@@ -0,0 +1,2 @@
<div>Page</div>
<script type="module" src="main.js"></script>
1 change: 1 addition & 0 deletions playground/hmr/missing-import/main.js
@@ -0,0 +1 @@
import './a.js'

0 comments on commit ee7c28a

Please sign in to comment.