Is it possible to make webpack-dev-server reload whenever a specified set of arbitrary files changes?
Something along the lines of:
devServer: {
watchTheseFiles: [ 'path/to/files', 'path/to/more/files' ]
}
And even more granularly, can I specify a regex of which files to watch?
I'm doing this as a bit of a hack to reload when I trigger certain changes in arbitrary files (they could be .txt
, .png
, whatever...)
At the moment, the specified paths in contentBase
don't seem to trigger reload when files change.
Using Watch Mode You can instruct webpack to "watch" all files within your dependency graph for changes. If one of these files is updated, the code will be recompiled so you don't have to run the full build manually. Now run npm run watch from the command line and see how webpack compiles your code.
Below are the scripts to use it: "scripts": { "watch:webpack-build-dev": "webpack --watch --mode development", "clean-db": "rm -rf ./db && mkdir -p ./db", "local-dev": "npm run clean-db && npm run watch:webpack-build-dev" ... }
devServer.client Allows to set log level in the browser, e.g. before reloading, before an error or when Hot Module Replacement is enabled. webpack.config.js module.
With watchmode, Webpack will watch your files and when one of them changes, it will immediately rerun the build and recreate your output file. After watch is set to true, when you run the webpack command, webpack will rebuild your bundle when any of your files change. Webpack has a web server called webpack-dev-server.
I'm not sure if there's anything readily available that's better than contentBase
. webpack
, webpack-dev-server
and webpack-dev-middleware
all expose an invalidate(callback)
helper, but it would require you to create your custom script to programmatically manage the compiler, server or middleware to invoke the handler.
Something similar to this that should work across all compilers configured for watching is to add additional files as compilation dependencies and let webpack handle the invalidation. You can also do this when using webpack programmatically, but it's also really easy to write your own plugin and perform this from the configuration. Here's an example:
class CustomContextPlugin {
apply (compiler) {
compiler.hooks.beforeCompile.tap('CustomContextPlugin', params => {
params.compilationDependencies.add(
path.resolve(__dirname, 'public', 'robots.txt')
)
})
}
}
module.exports = {
plugins: [
new CustomContextPlugin()
]
}
It uses the beforeCompile
hook to add a robots.txt
file as a compilation dependency. In development where we don't perform any caching, this should cause the development server to reload the page because new chunks were emitted.
There's probably also other options. One thing that comes to mind is maybe having a development entry or some other module that uses require.context
, but I think these options would involve more effort.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With