Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack watch() with multiple entry points - emitting bundles for non-changed files?

In my webpack.config.js I have 3 separate entry points, one for the JS bundle, one for the main SCSS bundle, and one for a separate SCSS bundle that has no relationship with the main SCSS bundle.

When I use the webpack.watch() API, for some reason editing, say, the JS source files, causes not only the JS bundle to be recompiled, but also the 2 SCSS bundles.
Why is this, and how can I stop this behaviour and ensure that only the entry point that is edited is recompiled?

The reason this is an issue is I'm using browsersync, and for CSS bundle recompiles I'm just injecting the CSS instead of reloading, but on HTML/JS edits its reloading. However if I edit the SCSS and it also recompiles the JS/HTML browsersync triggers a reload instead of a CSS inject.

like image 965
Matthew Avatar asked Mar 01 '17 03:03

Matthew


People also ask

Can webpack have multiple entry points?

Configuring the output configuration options tells webpack how to write the compiled files to disk. Note that, while there can be multiple entry points, only one output configuration is specified.

What does webpack watch do?

When using watch mode, webpack installs file watchers to all files, which were used in the compilation process. If any change is detected, it'll run the compilation again. When caching is enabled, webpack keeps each module in memory and will reuse it if it isn't changed.

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.


1 Answers

In that case you need a file that let webpack detect how many changes have be done on each different entry point.

For that, you can use the manifest file provided by commonsChunkPlugin:

For example, if you have the following entry points:

entry: {    app: 'main.js', // main entry point    vendor: ['jquery', 'react'] //Third libraries } 

You can use the plugin CommonsChunkPlugin:

new wepack.optimize.CommonsChunkPlugin({   name: ['vendor', 'manifest'] }) 

This configuration generates a manifest file as another output. In that case, if you make a change in your 'app' entry point, webpack are only going to recompile the main.js output bundle (according the 'filename' format in the 'output' configuration) because the vendor bundle is already the same.

You can try that with your specific entry points.

like image 170
Adrian García Avatar answered Sep 30 '22 10:09

Adrian García