diff --git a/README.md b/README.md index 007f559..c6b4096 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,7 @@ Once the build finishes, a child process is spawned firing both a python and nod * `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: [ ]** +* `onBuildError`: array of scripts to execute when there is an error during compilation. **Default: [ ]** * `dev`: switch for development environments. This causes scripts to execute once. Useful for running HMR on webpack-dev-server or webpack watch mode. **Default: true** * `safe`: switches script execution process from spawn to exec. If running into problems with spawn, turn this setting on. **Default: false** * `verbose`: **DEPRECATED** enable for verbose output. **Default: false** diff --git a/lib/index.js b/lib/index.js index d6c468e..e006fc1 100644 --- a/lib/index.js +++ b/lib/index.js @@ -149,6 +149,7 @@ var defaultOptions = { onBuildStart: [], onBuildEnd: [], onBuildExit: [], + onBuildError: [], dev: true, verbose: false, safe: false @@ -216,6 +217,9 @@ var WebpackShellPlugin = function () { if (typeof options.onBuildExit === 'string') { options.onBuildExit = options.onBuildExit.split('&&'); } + if (typeof options.onBuildError === 'string') { + options.onBuildError = options.onBuildError.split('&&'); + } return options; } }, { @@ -262,11 +266,16 @@ var WebpackShellPlugin = function () { callback(); }); - compiler.plugin('done', function () { + compiler.plugin('done', function (stats) { + if (stats.hasErrors() && _this.options.onBuildError.length) { + for (var i = 0; i < _this.options.onBuildError.length; i++) { + _this.handleScript(_this.options.onBuildError[i]); + } + } if (_this.options.onBuildExit.length) { console.log('Executing additional scripts before exit'); - for (var i = 0; i < _this.options.onBuildExit.length; i++) { - _this.handleScript(_this.options.onBuildExit[i]); + for (var _i = 0; _i < _this.options.onBuildExit.length; _i++) { + _this.handleScript(_this.options.onBuildExit[_i]); } } }); @@ -275,4 +284,4 @@ var WebpackShellPlugin = function () { return WebpackShellPlugin; }(); -module.exports = WebpackShellPlugin; \ No newline at end of file +module.exports = WebpackShellPlugin; diff --git a/src/webpack-shell-plugin.js b/src/webpack-shell-plugin.js index b287508..7f61b58 100644 --- a/src/webpack-shell-plugin.js +++ b/src/webpack-shell-plugin.js @@ -6,6 +6,7 @@ const defaultOptions = { onBuildStart: [], onBuildEnd: [], onBuildExit: [], + onBuildError: [], dev: true, verbose: false, safe: false @@ -56,6 +57,9 @@ export default class WebpackShellPlugin { if (typeof options.onBuildExit === 'string') { options.onBuildExit = options.onBuildExit.split('&&'); } + if (typeof options.onBuildError === 'string') { + options.onBuildError = options.onBuildError.split('&&'); + } return options; } @@ -99,7 +103,12 @@ export default class WebpackShellPlugin { callback(); }); - compiler.plugin('done', () => { + compiler.plugin('done', (stats) => { + if (stats.hasErrors() && this.options.onBuildError.length) { + for (let i = 0; i < this.options.onBuildError.length; i++) { + this.handleScript(this.options.onBuildError[i]); + } + } if (this.options.onBuildExit.length) { console.log('Executing additional scripts before exit'); for (let i = 0; i < this.options.onBuildExit.length; i++) { diff --git a/test/style.css b/test/style.css index 0c12baa..632a0e9 100644 --- a/test/style.css +++ b/test/style.css @@ -1,4 +1,6 @@ body { background: slategrey; color: blueviolet; -} \ No newline at end of file +} + +cause-webpack-error diff --git a/webpack.config.js b/webpack.config.js index 95ef485..602ff38 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -14,12 +14,19 @@ module.exports = { contentBase: path.resolve(__dirname, 'test') },*/ module: { - loaders: [ - { test: /\.css$/, loader: 'style!css' } - ] + loaders: [{ + test: /\.css$/, + loader: 'style!css' + }] }, plugins: [ - new WebpackShellPlugin({onBuildStart:['node test.js'], onBuildEnd:['echo "Webpack End"'], safe: true, verbose: true}), + new WebpackShellPlugin({ + onBuildStart: ['node test.js'], + onBuildEnd: ['echo "Webpack End"'], + onBuildError: ['echo "Webpack ERROR"'], + safe: true, + verbose: true + }), new webpack.HotModuleReplacementPlugin() ] };