diff --git a/README.md b/README.md index 007f559..409cfff 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,7 @@ module.exports = config; Once the build finishes, a child process is spawned firing both a python and node script. ### API +* `onBeforeBuild`: array of scripts to execute before every build. **Default: [ ]** * `onBuildStart`: array of scripts to execute on the initial build. **Default: [ ]** * `onBuildEnd`: array of scripts to execute after files are emitted at the end of the compilation. **Default: [ ]** * `onBuildExit`: array of scripts to execute after webpack's process is complete. *Note: this event also fires in `webpack --watch` when webpack has finished updating the bundle.* **Default: [ ]** diff --git a/lib/index.js b/lib/index.js index d6c468e..fd27fb9 100644 --- a/lib/index.js +++ b/lib/index.js @@ -146,6 +146,7 @@ var exec = require('child_process').exec; var os = require('os'); var defaultOptions = { + onBeforeBuild: [], onBuildStart: [], onBuildEnd: [], onBuildExit: [], @@ -207,6 +208,9 @@ var WebpackShellPlugin = function () { }, { key: 'validateInput', value: function validateInput(options) { + if (typeof options.onBeforeBuild === 'string') { + options.onBeforeBuild = options.onBeforeBuild.split('&&'); + } if (typeof options.onBuildStart === 'string') { options.onBuildStart = options.onBuildStart.split('&&'); } @@ -233,6 +237,15 @@ var WebpackShellPlugin = function () { value: function apply(compiler) { var _this = this; + compiler.plugin('invalid', function () { + if (this.options.onBeforeBuild.length) { + console.log('Executing before build scripts'); + for (var i = 0; i < this.options.onBeforeBuild.length; i++) { + this.handleScript(this.options.onBeforeBuild[i]); + } + } + }); + compiler.plugin('compilation', function (compilation) { if (_this.options.verbose) { console.log('Report compilation: ' + compilation); diff --git a/src/webpack-shell-plugin.js b/src/webpack-shell-plugin.js index b287508..89f22e0 100644 --- a/src/webpack-shell-plugin.js +++ b/src/webpack-shell-plugin.js @@ -3,6 +3,7 @@ const exec = require('child_process').exec; const os = require('os'); const defaultOptions = { + onBeforeBuild: [], onBuildStart: [], onBuildEnd: [], onBuildExit: [], @@ -47,6 +48,9 @@ export default class WebpackShellPlugin { } validateInput(options) { + if (typeof options.onBeforeBuild === 'string') { + options.onBeforeBuild = options.onBeforeBuild.split('&&'); + } if (typeof options.onBuildStart === 'string') { options.onBuildStart = options.onBuildStart.split('&&'); } @@ -69,6 +73,14 @@ export default class WebpackShellPlugin { } apply(compiler) { + compiler.plugin('invalid', function () { + if (this.options.onBeforeBuild.length) { + console.log('Executing before build scripts'); + for (var i = 0; i < this.options.onBeforeBuild.length; i++) { + this.handleScript(this.options.onBeforeBuild[i]); + } + } + }); compiler.plugin('compilation', (compilation) => { if (this.options.verbose) {