Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MichelML/8681d4845883489ae09796c1f7b8b5ed to your computer and use it in GitHub Desktop.
Save MichelML/8681d4845883489ae09796c1f7b8b5ed to your computer and use it in GitHub Desktop.
A webpack shell plugin to hock command after and before the build. All credits to original author: Yair Tavor. http://stackoverflow.com/a/35337516. I just classed it.
'use strict';
const exec = require('child_process').exec;
function puts(error, stdout, stderr) {
console.log(stdout);
}
class WebpackShellPlugin {
constructor(options) {
let defaultOptions = {
onBuildStart: [],
onBuildEnd: []
};
this.options = Object.assign(defaultOptions, options);
}
apply (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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment