Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
5 changes: 5 additions & 0 deletions packages/vite/src/node/server/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions packages/vite/src/node/server/moduleGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ export class ModuleGraph {
Promise<ModuleNode> | ModuleNode
>()

/** @internal */
_hasResolveFailedErrorModules = new Set<ModuleNode>()

constructor(
private resolveId: (
url: string,
Expand Down Expand Up @@ -229,6 +232,8 @@ export class ModuleGraph {
)
}
})

this._hasResolveFailedErrorModules.delete(mod)
}

invalidateAll(): void {
Expand Down
19 changes: 19 additions & 0 deletions playground/hmr/__tests__/hmr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
})
}
2 changes: 2 additions & 0 deletions playground/hmr/missing-file/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<div>Page</div>
<script type="module" src="main.js"></script>
1 change: 1 addition & 0 deletions playground/hmr/missing-file/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './a.js'