[英]How to mark a compilation as failed and make webpack to exit with non-zero return value from a plugin?
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. 我想创建一个webpack插件来检查代码库,并可选地将编译标记为失败,并导致webpack(如果未在监视模式下运行)以非零返回码退出。
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. 看来,加载程序(基于babel-loader
检查)可以仅通过抛出异常来实现。
How to make webpack to exit with non-zero return code from its plugin? 如何使Webpack从其插件以非零返回码退出? Assuming that bail
flag is set and one doesn't want to resort to hacks like: 假设设置了bail
标志,并且您不想诉诸黑客,例如:
if (bail) {
process.on('exit', () => process.exitCode = 1)
}
Plugin can report a failure by passing an true-ish object to callback of run
hook: 插件可以通过将真实对象传递给run
挂钩的回调来报告失败:
compiler.plugin('run', (compiler, callback) => {
callback(new Error('problem description'))
})
Code above causes webpack to exit with return code 1
. 上面的代码导致webpack退出,返回代码为1
。
This holds for all methods marked as async in documentation : 这适用于文档中标记为异步的所有方法:
async: Last parameter is a callback. 异步:最后一个参数是回调。 Signature: function(err, result) 签名:函数(错误,结果)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.