You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thanks! This plugin is assuming that there probably isn't any physical file to watch - so I'm not sure we should try to add the functionality here. It should probably be an additional plugin that adds additional files to the watcher. Take a look at Webpack's bundled WatchIgnorePlugin. I think you could pretty easily copy that and reverse the functionality so the paths passed to the plugin are merged in. If you get that working, please update this issue with a link to your repository or a gist so others that might need it can find it from here. I could update the README here with a link to it and example as well.
Hi, we're having the same issue.
We use VirtualModule to generate a config file from three other files.
We can easily trigger a build whenever one of the three sub-config files change, but as they're imported and used from the webpack.config, VirtualModule does not update the generated file anyway.
Can you explain better the approach with WatchIgnorePlugin you were thinking of?
@FrancescoCioria Thanks for giving feedback. I was suggesting to create a new plugin - maybe called WatchAddPlugin - that could take a list of file paths and insert it into the watch filesystem. You could duplicate the source of the WatchIgnorePlugin, name it WatchAddPlugin and change it from removing files from the list to adding them instead. I found this conversation with the creator of Webpack who explains the watch filesystem a bit - webpack/webpack-dev-middleware#3
But I see what you're saying. That would get webpack to rebuild on change of your additional files, but it wouldn't update the virtual module contents as those are currently only fetched when the webpack config is created.
We'd need to create some type of watch/dev mode that would have it re-fetch the contents every time within the resolvePlugin. That dev option would also pass through to the populateFilesystem function so it would allow overwriting the existing contents - currently prevented here. It would probably be required to pass in the contents as a function in that case.
Unfortunately, I probably won't have bandwidth any time soon to work on that. But I would take a pull request - especially one that adds additional test coverage for the new functionality.
Essentially, you'll want to clone it - but replace its watch() method. Said method is tapping into compiler.watchFileSystem.watch() and that's the part you'll want to replace with another mechanism for invalidating your virtual modules.
Probably by expanding the function-type signature for content with a callback that can invalidate a virtual file. E.g.
constVirtualModulePlugin=require('virtual-module-webpack-plugin');module.exports={entry: './index',plugins: [newVirtualModulePlugin({moduleName: './time-of-day.txt',contents(invalidate){setTimeout(invalidate,10e3);return`export default "The time is: ${Date()};"`;}})]};
Another thing you could try is to -- rather than inject files into the existing compiler.inputFileSystem which, though a nice idea, still is kind of a hack -- use the compiler.hooks.afterEnvironment hook and wrap the then set-up compiler.inputFileSystem with one that merges a virtual file system (such as memory-fs) with the regular one.
Then implement watches on your virtual file system and a means to propagate them from both the original and virtual one on the merging file system.
Activity
rmarscher commentedon Dec 21, 2016
Thanks! This plugin is assuming that there probably isn't any physical file to watch - so I'm not sure we should try to add the functionality here. It should probably be an additional plugin that adds additional files to the watcher. Take a look at Webpack's bundled WatchIgnorePlugin. I think you could pretty easily copy that and reverse the functionality so the paths passed to the plugin are merged in. If you get that working, please update this issue with a link to your repository or a gist so others that might need it can find it from here. I could update the README here with a link to it and example as well.
FrancescoCioria commentedon Aug 22, 2017
Hi, we're having the same issue.
We use
VirtualModuleto generate aconfigfile from three other files.We can easily trigger a build whenever one of the three sub-config files change, but as they're imported and used from the
webpack.config,VirtualModuledoes not update the generated file anyway.Can you explain better the approach with
WatchIgnorePluginyou were thinking of?rmarscher commentedon Aug 22, 2017
@FrancescoCioria Thanks for giving feedback. I was suggesting to create a new plugin - maybe called
WatchAddPlugin- that could take a list of file paths and insert it into the watch filesystem. You could duplicate the source of the WatchIgnorePlugin, name it WatchAddPlugin and change it from removing files from the list to adding them instead. I found this conversation with the creator of Webpack who explains the watch filesystem a bit - webpack/webpack-dev-middleware#3But I see what you're saying. That would get webpack to rebuild on change of your additional files, but it wouldn't update the virtual module contents as those are currently only fetched when the webpack config is created.
We'd need to create some type of watch/dev mode that would have it re-fetch the contents every time within the resolvePlugin. That dev option would also pass through to the populateFilesystem function so it would allow overwriting the existing contents - currently prevented here. It would probably be required to pass in the contents as a function in that case.
Unfortunately, I probably won't have bandwidth any time soon to work on that. But I would take a pull request - especially one that adds additional test coverage for the new functionality.
Thanks.
rjgotten commentedon Jul 4, 2019
You don't need to lean on file system watches as surfaced through the
NodeWatchFileSystemclass.Look at how the
Watchingclass is implemented.Essentially, you'll want to clone it - but replace its
watch()method. Said method is tapping intocompiler.watchFileSystem.watch()and that's the part you'll want to replace with another mechanism for invalidating your virtual modules.Probably by expanding the function-type signature for
contentwith a callback that can invalidate a virtual file. E.g.Another thing you could try is to -- rather than inject files into the existing
compiler.inputFileSystemwhich, though a nice idea, still is kind of a hack -- use thecompiler.hooks.afterEnvironmenthook and wrap the then set-upcompiler.inputFileSystemwith one that merges a virtual file system (such asmemory-fs) with the regular one.Then implement watches on your virtual file system and a means to propagate them from both the original and virtual one on the merging file system.