Manually invalidate virtual module #15504
Replies: 2 comments 2 replies
-
@benjaminpreiss I've been trying to hack together some something similar and here is what I came up with:
I'm sure there's lot's of dragons there, as it invalidates all modules. The main issue with the pattern I think you and I are both using is that we are introducing a virtual module which has hidden (in vite's eyes) dependencies on other files. Both the modules and server.moduleGraph from the HmrContext in the To solve this issue I'm thinking about injecting |
Beta Was this translation helpful? Give feedback.
-
Thanks @connormckelvey, I've used your exemple and end up with this solution, if it can help someone : export function browserConfig(): Plugin {
const virtualModuleId = 'virtual:browser-config';
const resolvedVirtualModuleId = '\0' + virtualModuleId;
return {
name: 'rizom:browser-config',
configureServer(server) {
const configDir = path.resolve(process.cwd(), '.rizom');
server.watcher.add(path.join(configDir, '**'));
},
async handleHotUpdate({ server, file }) {
if(file.includes('config.browser.txt')){
const module = server.moduleGraph.getModuleById(resolvedVirtualModuleId);
if (module) {
server.moduleGraph.invalidateModule(module);
return [module]
}
}
},
resolveId(id) {
if (id === virtualModuleId) {
return resolvedVirtualModuleId;
}
},
load(id){
// virtual module stuff
}
}
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I am building a library which adds virtual: module imports to specific files depending on a changing configuration file.
The problem is, that vite does detect changes in the configuration file / files imported from the configuration file but does not retrigger transforms in the virtual modules.
How can I make my vite plugin retrigger
transform
/load
when the configuration file changes?I was thinking there must be a cache invalidation option for specific files which I can call?
Very thankful for help.
Beta Was this translation helpful? Give feedback.
All reactions