Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack --watch and launching nodemon?

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?

like image 356
XeroxDucati Avatar asked Feb 22 '16 02:02

XeroxDucati


People also ask

How do I watch Webpacks?

Other Ways to Enable Watch Mode You can also enable watch mode from your Webpack config file: module. exports = { mode: 'development', watch: true, // Enable watch mode entry: { app: `${__dirname}/app. js` }, target: 'web', output: { path: `${__dirname}/bin`, filename: '[name].

How we can 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

  1. Install the following dependencies:

npm install npm-run-all webpack nodemon

  1. Configure your package.json file to something as seen below:

package.json

{   ...    "scripts": {     "start"        : "npm-run-all --parallel watch:server watch:build",     "watch:build"  : "webpack --watch",     "watch:server" : "nodemon \"./dist/index.js\" --watch \"./dist\""   },    ...  } 

After doing so, you can easily run your project by using npm start.

Don't forget config WatchIgnorePlugin for webpack to ignore ./dist folder.

Dependencies

  1. npm-run-all - A CLI tool to run multiple npm-scripts in parallel or sequential.
  2. webpack - webpack is a module bundler. Its main purpose is to bundle JavaScript files for usage in a browser, yet it is also capable of transforming, bundling, or packaging just about any resource or asset.
  3. nodemon - Simple monitor script for use during development of a node.js app.
like image 170
Ling Avatar answered Oct 05 '22 01:10

Ling