简体   繁体   English

如何将编译标记为失败并使Webpack从插件返回非零返回值?

[英]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.

相关问题 如何将值从插件返回到 action/js? - How to return a value from plugin to action/js? Munin-如何从插件返回未知值 - Munin - how to return unknown value from plugin 如何使我的编译插件覆盖Maven之一? - How to make my compilation plugin override the maven one? 尝试运行 React 应用程序时获取“Html Webpack 插件:错误:子编译失败:Module.createRequire 不是函数” - Getting "Html Webpack Plugin: Error: Child compilation failed: Module.createRequire is not a function" when trying to run React app 如何从 jquery 插件元素上的单击事件获取返回值? - How to get a return value from click event on a jquery plugin element? 如何突出显示/标记为重要/列出一些WordPress帖子? 是否存在任何插件? - How to highlight / mark as important / make a list of some wordpress posts? Exist any plugin? 如何从jquery插件返回iframe内容? - How to return Iframe content from a jquery plugin? jQuery:如何从插件返回数据 - jQuery: how to return data from a plugin Xcode - clang因退出代码1而失败。插件原因? - Xcode - clang failed with exit code 1. Plugin cause? 如何让插件执行另一个插件的命令? - How can i make a plugin execute a command from another plugin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM