如何在gulp中显示webpack错误

3

我有一个使用webpack进行转译的TypeScript项目,构建任务由gulp驱动。这里以一个简单的任务来展示我的问题:

var webpack = require('webpack');
var webpackStream = require("webpack-stream");

gulp.task("check", function() {
        return gulp.src("*")
            .pipe(webpackStream(require("./webpack.config.js"), webpack))
            .pipe(gulp.dest(OUTPUT_DIR));
    }
);

现在当一切正常时,gulp check可以正常工作。现在假设我犯了一些编码错误 - 这个 gulp 任务将会发出:

[10:20:02] Starting 'check'...
[hardsource:chrome] Tracking node dependencies with: yarn.lock.
[hardsource:chrome] Reading from cache chrome...
(node:20232) UnhandledPromiseRejectionWarning
(node:20232) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:20232) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
[10:20:08] The following tasks did not complete: check
[10:20:08] Did you forget to signal async completion?

这个信息很不明确。如果我从检查任务中移除.pipe(gulp.dest(OUTPUT_DIR));,那么我就能看到原始的错误信息了。比如:
[10:35:42] Starting 'check'...
[hardsource:chrome] Tracking node dependencies with: yarn.lock.
[hardsource:chrome] Reading from cache chrome...
[10:35:48] 'check' errored after 5.96 s
[10:35:48] Error in plugin "webpack-stream"
Message:
    ./myfile.ts
Module build failed: Error: Typescript emitted no output for C:\Projects\myfile.ts.
    at successLoader (C:\Projects\***\node_modules\ts-loader\dist\index.js:41:15)
    at Object.loader (C:\Projects\***\node_modules\ts-loader\dist\index.js:21:12)
 @ ./src/background/background.ts 16:25-54
 @ multi ./src/background/backgroundC:\Projects\***\src\myfile.ts
./src/myfile.ts
[tsl] ERROR in C:\Projects\***\src\myfile.ts(305,28)
      TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
  Type 'undefined' is not assignable to type 'number'.
Details:
    domainEmitter: [object Object]
    domain: [object Object]
    domainThrown: false

在webpackStream之前或之后向gulp-load-plugins.logger添加管道无法解决问题。我能将gulp流导入到gulp.dest中,仍然能获得详细的webpack错误信息吗?如何做到?

2个回答

4
我甚至没有使用hard-source-webpack-plugin,这种情况也发生了。似乎这是webpack-stream本身的问题:https://github.com/shama/webpack-stream/issues/34 改为:
.pipe(webpackStream(require("./webpack.config.js"), webpack))
.pipe(gulp.dest(OUTPUT_DIR));

在webpackStream管道中添加错误处理程序:

.pipe(webpackStream(require("./webpack.config.js"), webpack))
    .on('error',function (err) {
      console.error('WEBPACK ERROR', err);
      this.emit('end'); // Don't stop the rest of the task
    })
.pipe(gulp.dest(OUTPUT_DIR));

3
我会把解决方案发布出来,而不是删除问题,以防未来有人需要它。
在我们的情况下,问题是使用了一个名为hard-source-webpack-plugin的webpack构建加速器。 显然这是一个已知问题 - github链接包括一个潜在的解决方法。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接