Skip to content

Instantly share code, notes, and snippets.

@michaeljota
Last active March 27, 2019 16:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save michaeljota/0f9739917b4fafc1bf47420b84e3c09b to your computer and use it in GitHub Desktop.
Save michaeljota/0f9739917b4fafc1bf47420b84e3c09b 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