[Bug] Creating virtual files on the fly causes infinite watched rebuilds
See original GitHub issue- I’d be willing to submit the fix
Describe the bug
I don’t necessarily know all the virtual files I want to create up front, so I’ve been creating them on a beforeResolve hook if they match a pattern. This worked fine up to and including 0.3.1. Since 0.3.2 this causes infinite rebuilds (which I guess makes sense given I’m creating files mid way through a build).
I’m not sure if this was ever a supported scenario so this may well be a feature request, but it was working until very recently
Is there a way to keep this working? Or alternatively a better approach?
To Reproduce
Add the following plugin to your webpack config and import testfile.virtual.js
from a file in the build.
Start a watched build.
class ReproPlugin {
apply(compiler) {
const virtualModules = new VirtualModulesPlugin();
virtualModules.apply(compiler);
compiler.hooks.compilation.tap(this.constructor.name, (compilation, { normalModuleFactory }) => {
normalModuleFactory.hooks.beforeResolve.tap(this.constructor.name, (result) => {
if (/\.virtual\.js$/i.test(result.request)) {
virtualModules.writeModule(
path.resolve(result.context, result.request),
'// test content');
}
// uncomment if webpack < 5
// return result;
});
});
}
}
Prior to 0.3.2 this would build happily and wait for new changes. In 0.3.2 it builds repeatedly.
Screenshots
If applicable, add screenshots to help explain your problem.
Environment if relevant (please complete the following information):
- OS: Windows 10
- Node version: 12.16.3
- Webpack version: 5.3.2
- webpack-virtual-modules: 0.3.2
Additional context
The virtual files I’m creating don’t exist in any form on disk so I wasn’t able get a custom loader to work instead.
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (3 by maintainers)
Top GitHub Comments
@larixer You’re 100% right. I had assumed we didn’t need to resolve the same path more than once in a compilation.
This works absolutely fine with latest webpack and webpack-virtual-modules
So no feature request needed and apologies for the spurious bug. Thank you, I appreciate the help.
@StephanGerbeth can do.
It’s worth trying to populate the files early (in the virtual modules constructor or compilation hook) like in the readme examples and make sure the issue is not to do with how that virtual modules interacts with that plugin in general.
If not I’m happy to look through a sandbox, though I don’t think I’m using the code above in conjunction with other loaders anywhere, so no guarantees I’ll be able to make it work either 😃