小编Lem*_*mmy的帖子

Webpack插件错误管理

这篇文章的主要目标是在编写Webpack插件时获得有关错误/警告管理的额外信息.

我在Webpack插件文档中看到,可以将err参数传递给基于时序的插件接口(在回调中),但没有进一步解释它如何影响Webpack生命周期,它的构建过程以及如何使用它.它没有解释是否有办法管理其他类型的插件接口的错误.

无论如何,作为第一次尝试,在'emit'生命周期步骤中,我试图传递给err参数a new Error('An error has occurred')或只是一个'An error has occured'值,但在任何一种情况下它确实在控制台中显示给定的err参数(IE很遗憾没有任何参数)特定于错误的颜色),并且webpack-sev-server卡住了:

  function WpAggregationPlugin() {
    this.startTime = Date.now();
    this.prevTimestamps = {};
  }

  WpAggregationPlugin.prototype.apply = function(compiler) {
    compiler.plugin( 'emit', (compilation, callback) => {

      var changedFiles = Object.keys(compilation.fileTimestamps).filter( watchfile => 
        this.prevTimestamps[watchfile] &&
        (this.prevTimestamps[watchfile] < (compilation.fileTimestamps[watchfile] || Infinity)) )

      // compilation.errors.push(new Error('...'))

      this.prevTimestamps = compilation.fileTimestamps;

      if( changedFiles.length <= 0 ) {
        callback()
      } else {
        process.stdout.write( `File modification detected :\n${JSON.stringify(changedFiles, null, 4)}\n` ) …
Run Code Online (Sandbox Code Playgroud)

webpack webpack-dev-server webpack-plugin

5
推荐指数
2
解决办法
2707
查看次数