Skip to content

onBuildError option takes script to execute when there is an error during compilation #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down
17 changes: 13 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ var defaultOptions = {
onBuildStart: [],
onBuildEnd: [],
onBuildExit: [],
onBuildError: [],
dev: true,
verbose: false,
safe: false
Expand Down Expand Up @@ -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;
}
}, {
Expand Down Expand Up @@ -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]);
}
}
});
Expand All @@ -275,4 +284,4 @@ var WebpackShellPlugin = function () {
return WebpackShellPlugin;
}();

module.exports = WebpackShellPlugin;
module.exports = WebpackShellPlugin;
11 changes: 10 additions & 1 deletion src/webpack-shell-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const defaultOptions = {
onBuildStart: [],
onBuildEnd: [],
onBuildExit: [],
onBuildError: [],
dev: true,
verbose: false,
safe: false
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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++) {
Expand Down
4 changes: 3 additions & 1 deletion test/style.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
body {
background: slategrey;
color: blueviolet;
}
}

cause-webpack-error
15 changes: 11 additions & 4 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
]
};