Last active
March 27, 2019 16:28
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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