fix(hmr): trigger hmr for missing file import errored module after file creation (#16303)

This commit is contained in:
翠 / green 2024-03-29 15:04:00 +09:00 committed by GitHub
parent dfffea1f43
commit ffedc06cab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 33 additions and 0 deletions

View File

@ -311,6 +311,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
}
// fix#9534, prevent the importerModuleNode being stopped from propagating updates
importerModule.isSelfAccepting = false
moduleGraph._hasResolveFailedErrorModules.add(importerModule)
return this.error(
`Failed to resolve import "${url}" from "${normalizePath(
path.relative(process.cwd(), importerFile),

View File

@ -163,6 +163,11 @@ export async function handleHMRUpdate(
}
const mods = new Set(moduleGraph.getModulesByFile(file))
if (type === 'create') {
for (const mod of moduleGraph._hasResolveFailedErrorModules) {
mods.add(mod)
}
}
if (type === 'create' || type === 'delete') {
for (const mod of getAffectedGlobModules(file, server)) {
mods.add(mod)

View File

@ -108,6 +108,9 @@ export class ModuleGraph {
Promise<ModuleNode> | ModuleNode
>()
/** @internal */
_hasResolveFailedErrorModules = new Set<ModuleNode>()
constructor(
private resolveId: (
url: string,
@ -229,6 +232,8 @@ export class ModuleGraph {
)
}
})
this._hasResolveFailedErrorModules.delete(mod)
}
invalidateAll(): void {

View File

@ -962,4 +962,23 @@ if (!isBuild) {
editFile('css-deps/dep.js', (code) => code.replace(`red`, `green`))
await untilUpdated(() => getColor('.css-deps'), 'green')
})
test('hmr should happen after missing file is created', async () => {
const file = 'missing-file/a.js'
const code = 'console.log("a.js")'
await untilBrowserLogAfter(
() =>
page.goto(viteTestUrl + '/missing-file/index.html', {
waitUntil: 'load',
}),
/connected/, // wait for HMR connection
)
await untilBrowserLogAfter(async () => {
const loadPromise = page.waitForEvent('load')
addFile(file, code)
await loadPromise
}, [/connected/, 'a.js'])
})
}

View File

@ -0,0 +1,2 @@
<div>Page</div>
<script type="module" src="main.js"></script>

View File

@ -0,0 +1 @@
import './a.js'