How to mark a compilation as failed and make webpack to exit with non-zero return value from a plugin?

1.3k views Asked by At

I'd like to create a webpack plugin that checks the codebase and optionally marks the compilation as failed and caused webpack (if not running in watch mode) to exit with non-zero return code.

I noticed that plugins can report errors like this:

compilation.errors.push(new Error('foo error'))

However such errors doesn't cause non-zero return code. It seems that loaders (based on examination of babel-loader) can achieve that by mere throwing an exception.

How to make webpack to exit with non-zero return code from its plugin? Assuming that bail flag is set and one doesn't want to resort to hacks like:

if (bail) {
    process.on('exit', () => process.exitCode = 1)
}
1

There are 1 answers

1
czerny On BEST ANSWER

Plugin can report a failure by passing an true-ish object to callback of run hook:

compiler.plugin('run', (compiler, callback) => {
    callback(new Error('problem description'))
})

Code above causes webpack to exit with return code 1.

This holds for all methods marked as async in documentation:

async: Last parameter is a callback. Signature: function(err, result)