Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webpack dev server: watch arbitrary directory files and reload

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.

like image 570
clo_jur Avatar asked Jun 25 '17 06:06

clo_jur


People also ask

How can we make the webpack to watch for changes?

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.

Which command will run webpack in watch mode?

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" ... }

What is devServer?

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.

How does webpack Watch work?

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.


1 Answers

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.

like image 131
Filip Dupanović Avatar answered Nov 16 '22 19:11

Filip Dupanović