67

I'd like to run webpack in --watch mode, and run a shell command after each build that synchronizes a folder to another one.

I found this plugin that triggers an event after each build. That works, but the last piece of the puzzle is to trigger a shell command (for syncing) from Javascript. Any pointers on how to achieve this are greatly appreciated.

Monokai
  • 933
  • 2
  • 9
  • 14

7 Answers7

146

Webpack 4

As of today (April 11, 2018), most of the plugins I've tried use the deprecated API resulting in this warning:

DeprecationWarning: Tapable.plugin is deprecated. Use new API on `.hooks` instead

It pleased me to find that you can easily write an ad-hoc webpack plugin (docs).

In your webpack.config.js file:

const exec = require('child_process').exec;

module.exports = {

  // ... other config here ...

  plugins: [

    // ... other plugins here ...

    {
      apply: (compiler) => {
        compiler.hooks.afterEmit.tap('AfterEmitPlugin', (compilation) => {
          exec('<path to your post-build script here>', (err, stdout, stderr) => {
            if (stdout) process.stdout.write(stdout);
            if (stderr) process.stderr.write(stderr);
          });
        });
      }
    }
  ]
};

If you'd rather use spawn to get real-time or "live" data from your script, this illustrates the basic usage:

const spawn = require('child_process').spawn;

const child = spawn('<your script here>');
child.stdout.on('data', function (data) {
    process.stdout.write(data);
});
child.stderr.on('data', function (data) {
    process.stderr.write(data);
});
jchook
  • 5,317
  • 2
  • 31
  • 37
  • 2
    Great, also works on watch builds, what didn't seem to be the case with Yair Tavor's solution. – robbash Nov 14 '18 at 20:40
  • 9
    Just a heads up, if you're looking for the **before** equivalent of the hook, you can try `beforeCompile`. Documentation on which hooks are available can be found [here](https://webpack.js.org/api/compiler-hooks/) – Gus Nov 16 '18 at 18:06
  • Are you sure about ""? Docs says that it should be "the command to run": https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback . When I have a path there it complaints about permission: "/bin/sh: tasks/postbuild.sh: Permission denied" – Andrey Oct 06 '19 at 09:46
  • 1
    @Andrey you can supply a command to run, or a path to a script file with the execute permission set. To fix your problem try `chmod +x /path/to/script` – jchook Oct 06 '19 at 20:02
  • Yes, but if webpack has child compilations, it will not work – Limbo Nov 04 '19 at 07:59
80

I also needed such a thing, so I compiled a super simple plugin to execute shell commands before and after each build.

'use strict';

var exec = require('child_process').exec;

function puts(error, stdout, stderr) {
    console.log(stdout);
}

function WebpackShellPlugin(options) {
  var defaultOptions = {
    onBuildStart: [],
    onBuildEnd: []
  };

  this.options = Object.assign(defaultOptions, options);
}

WebpackShellPlugin.prototype.apply = function(compiler) {
  const options = this.options;

  compiler.plugin("compilation", compilation => {
    if(options.onBuildStart.length){
        console.log("Executing pre-build scripts");
        options.onBuildStart.forEach(script => exec(script, puts));
    }
  });

  compiler.plugin("emit", (compilation, callback) => {
    if(options.onBuildEnd.length){
        console.log("Executing post-build scripts");
        options.onBuildEnd.forEach(script => exec(script, puts));
    }
    callback();
  });
};

module.exports = WebpackShellPlugin;

then in your webpack config:

plugins: [
    new WebpackShellPlugin({ 
         onBuildStart: ['echo "hello world"'], 
         onBuildEnd: ['echo "goodbye world"'] 
    })
]

This is super basic, and do not support async scripts properly. but it works. feel free to modify however you see fit.

Consider this code under MIT licence.

Needs node 4.x and up to run, as I use some es6 features here.

Community
  • 1
  • 1
Yair Tavor
  • 2,508
  • 1
  • 18
  • 16
6

Use webpack-shell-plugin

How to Use:

const WebpackShellPlugin = require('webpack-shell-plugin');


    module.exports = {
      ...
      ...
      plugins: [
        new WebpackShellPlugin({onBuildStart:['echo "Webpack Start"'], onBuildEnd:['echo "Webpack End"']})
      ],
      ...
    }
Krishan Kumar
  • 61
  • 1
  • 4
  • Note: this is actually a plugin suggested by @Yair Tavor in his anwser. – Olga Sep 28 '18 at 10:52
  • 1
    @Olga reducing the solution to what is strictly necessary is an actual contribution and deserves credit. Krishna you can enhance your answer by explaining the differences with Yair Tavor's answer. As far as I understand, Yair Tavor's solution has been packaged in the webpack-shell-plugin and what you propose is how to use it. It is worthwhile mentionning to help others understand the situation. – Titou Oct 17 '18 at 10:33
5

Basically, you can hook into the compiler at various stages of the whole compilation to emitting resources stage etc and run your own script or code as you please.

I like to do it this way -

class CustomPlugin {
  constructor(name, command, stage = 'afterEmit') {
    this.name = name;
    this.command = command;
    this.stage = stage;
  }

  static execHandler(err, stdout, stderr) {
    if (stdout) process.stdout.write(stdout);
    if (stderr) process.stderr.write(stderr);
  }

  apply(compiler) {
    compiler.hooks[this.stage].tap(this.name, () => {
      exec(this.command, CustomPlugin.execHandler);
    });
  }
}

and then use it like so

new CustomPlugin('RunTest', 'jest', 'beforeRun'),
Sohail
  • 3,968
  • 1
  • 33
  • 36
4

You can easily run any shell command with built-in child_process module. Also you can try some shell libraries for node.js, like Shell.js. It wraps most of default shell for more convenient usage

just-boris
  • 8,201
  • 5
  • 41
  • 77
1

If you guya want to do it when a specific file gets changed you can use this little plugin I built: https://www.npmjs.com/package/webpack-noodle-plugin

Hope it can help

Pistolpete .
  • 808
  • 1
  • 7
  • 15
1

webpack-shell-plugin-next plugin

There is the webpack-shell-plugin-next plugin:

Using the plugin

The onAfterDone plugin API:

onAfterDone: configuration object for scripts that execute after done.

may be used to achieve the desired watch-related behaviour (in addition, please, see the important note below):

I'd like to run webpack in --watch mode, and run a shell command after each build that synchronizes a folder to another one.

Important note: the onAfterDone plugin API will work for (affect) the normal build mode too (i.e. the webpack command without the --watch option).

Here is an additional reference to the related GitHub issue: onDoneWatch scripts executing before bundle written · Issue #16 · s00d/webpack-shell-plugin-next.

Example

Have just tried to use the plugin: it has worked fine.

devDependencies (from package.json)

"devDependencies": {
  "webpack": "5.3.2",
  "webpack-cli": "4.1.0",
  "webpack-shell-plugin-next": "2.0.4"
}

watch npm run script (from package.json)

"scripts": {
  "watch": "webpack --config webpack.config.js --watch"
}

Webpack configuration file (webpack.config.js)

const WebpackShellPluginNext = require('webpack-shell-plugin-next');

module.exports = {
    plugins: [
        new WebpackShellPluginNext({
            onAfterDone: {
                scripts: ['echo "It works!"'],
                blocking: true,
                parallel: false
            }
        })
    ]
};

Command line to run Webpack in the watch mode

npm run watch
Sergey Brunov
  • 11,755
  • 7
  • 39
  • 71