-
-
Notifications
You must be signed in to change notification settings - Fork 8k
Fails to hmr accept virtual modules #12912
Copy link
Copy link
Closed
Labels
feat: hmrp3-minor-bugAn edge case that only affects very specific usage (priority)An edge case that only affects very specific usage (priority)
Description
Describe the bug
I'm trying to hot reload a virtual module, but it doesn't seem to be able to accept it:
// server.mjs
import { createServer } from "vite";
async function main() {
const devServer = await createServer({
clearScreen: false,
plugins: [
{
name: "my-plugin",
async resolveId(id) {
if (id === "virtual:my-plugin:foo") {
return "\0virtual:my-plugin:foo";
}
return null;
},
async load(id) {
if (id === "\0virtual:my-plugin:foo") {
return `export default Math.random();`;
}
return null;
},
},
],
});
await devServer.listen(3000);
devServer.printUrls();
setInterval(() => {
const mod = devServer.moduleGraph.getModuleById("\0virtual:my-plugin:foo");
if (mod) {
console.log("reloading virtual module");
devServer.reloadModule(mod);
}
}, 2000);
}
main();// bar.js
export default Math.random();// main.js
import foo from "virtual:my-plugin:foo";
import bar from "./bar";
console.log("loading foo ", foo);
console.log("loading bar ", bar);
if (import.meta.hot) {
import.meta.hot.accept("virtual:my-plugin:foo", (newFoo) => {
console.log(`hmr accepting ${newFoo.default} from "virtual:my-plugin:foo"`);
});
import.meta.hot.accept("./bar", (newBar) => {
console.log(`hmr accepting ${newBar.default} from "./bar"`);
});
}Reproduction
Steps to reproduce
- run
yarn && yarn dev - open browser to
http://localhost:3000 - open console
- observe the page reloading when the interval fires
- try the same but for the real module:
const mod = devServer.moduleGraph.getModuleById(path.resolve("./src/bar.js")); - observe the module being hot accepted
I tried using /@id/__x00__virtual:my-plugin:foo, but that doesn't work neither
System Info
System:
OS: macOS 13.3.1
CPU: (8) arm64 Apple M1
Memory: 101.66 MB / 16.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 16.20.0 - ~/.nvm/versions/node/v16.20.0/bin/node
Yarn: 1.22.19 - ~/.nvm/versions/node/v16.20.0/bin/yarn
npm: 8.19.4 - ~/.nvm/versions/node/v16.20.0/bin/npm
Browsers:
Chrome: 112.0.5615.121
Firefox: 111.0.1
Safari: 16.4
npmPackages:
vite: ^4.3.0-beta.2 => 4.3.0-beta.8Used Package Manager
yarn
Logs
No response
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
feat: hmrp3-minor-bugAn edge case that only affects very specific usage (priority)An edge case that only affects very specific usage (priority)