Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Watch webpack.config.js and re-run webpack command in response to a file change

Tags:

I am using webpack.config.js and

webpack --watch --progress --debug 

to build modules.

I would like webpack --watch to restart after I change webpack.config.js file.

Is there a webpack flag I can use?

like image 337
Gajus Avatar asked Jul 17 '15 09:07

Gajus


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.

What does the webpack -- config webpack config js do?

Webpack configs allow you to configure and extend Webpack's basic functionality. A Webpack config is a JavaScript object that configures one of Webpack's options. Most projects define their Webpack config in a top-level webpack.

What is -- watch in webpack?

Turn on watch mode. This means that after the initial build, webpack will continue to watch for changes in any of the resolved files. webpack.config.js module. exports = { //...


2 Answers

I have ended up using nodemon. nodemon allows executing non-node scripts in response to changes in a file system.

You cannot use it with webpack --watch because --watch is not going to exit. However, you can simply use it with webpack, e.g.

nodemon \     --watch ./\     --delay 250ms\     --exec 'node ./node_modules/.bin/webpack' 

nodemon supports configuration files that can be used to reduce the boilerplate, ignore specific files and/or directories and produce re-usable configuration.

In addition, using nodemon instead of webpack --watch works around a known webpack issue, "Does not detect minor changes to the file (added/removed whitespace, semicolon)".

like image 131
Gajus Avatar answered Oct 04 '22 02:10

Gajus


I personally prefer my development setup to mimic my production setup as much as possible. That's why I prefer PM2 to nodemon, forever, etc.

Since you can roll this tool out in production and you have production ready toolsets built around it. Here's documentation on how to get it to watch and restart.

To get this working with webpack-dev-server you would have to wrap your config in a node server.

like image 32
Mo Binni Avatar answered Oct 04 '22 03:10

Mo Binni