Thanks to an excellent answer by @McMath I now have webpack compiling both my client and my server. I'm now on to trying to make webpack --watch
be useful. Ideally I'd like to have it spawn something like nodemon for my server process when that bundle changes, and some flavor of browsersync for when my client changes.
I realize it's a bundler/loader and not really a task runner, but is there some way to accomplish this? A lack of google results seems to indicate I'm trying something new, but this must have been done already..
I can always have webpack package to another directory and use gulp to watch it/copy it/browsersync-ify it, but that seems like a hack.. Is there a better way?
Faced the same problem and found the next solution - "webpack-shell-plugin". It
So, thats my scripts in package.json:
If I run 'start' script it launches next script sequence: clean -> start -> poststart. And there is part of 'webpack.server.config.js':
"onBuildEnd" event fires only once after first build, rebuilds are not trigger "onBuildEnd", so nodemon works as intended
In addition to @Ling's good answer:
If you want to build your project once, before you watch it with
nodemon
, you can use a webpack compiler hook. The plugin's code triggersnodemon
in thedone
hook once after webpack has finished its compilation (see also this helpful post).webpack.config.js:
package.json:
Hope, it helps.
There's no need to use plugins here. You could try running multiple
nodemon
instances like below. Try modifying the following script for your use case, and see if it works for you:@Ling has an answer very close to being correct. But it errors the first time somebody runs watch. You'll need to modify the solution as so to prevent errors.
Run
npm install npm-run-all webpack nodemon
Create a file called
watch-shim.js
in your root. Add the following contents, which will create a dummy file and directory if they're missing.Setup your scripts as so in
package.json
. This will only run watch if thewatch-shim.js
file runs successfully. Thereby preventing Nodemon from crashing due to missing files on the first run.Assuming
nodemon server.js
touch theserver.js
fileafterEmit
:You don't need any plugins to use webpack and nodemon, just use this scripts on your package.json