【问题标题】:Webpack multi-config callback errors and statsWebpack 多配置回调错误和统计信息
【发布时间】:2015-08-19 07:42:43
【问题描述】:

我正在尝试在监视模式下运行时使用多个 Webpack 配置。

这个要点是,根据我是使用build 还是watch,使用不同的参数调用编译回调。我一直无法挖掘出任何这样的用法,并且想知道是否有其他人看到过这种行为。

var webpack = require('webpack');

var configs = [config, config]  // let me know if you need this
var done = function () {        // err, stats?
  console.log('args', arguments);
};

单次运行模式
这有点道理(2 个编译,2 个统计对象),并且错误会正确冒泡。

webpack(configs).run(done);
// args {
//   '0': null,
//   '1':  { 
//     stats: [ [Object], [Object] ],
//     hash: '6939dac42dcc6c751bc6a0de33bd8893f6a13f78'
//   }
// }

观看模式
这甚至可以输出多次。

webpack(configs).watch(done);
// {
//   '0': null,
//   '1': {
//     compilation: {
//       _plugins: [Object],
//       compiler: [Object],
//       resolvers: [Object],
//       ...
//       hash: 'a0de33bd8893f6a13f78',
//       fileDependencies: [Object],
//       contextDependencies: [],
//       missingDependencies: [] },
//     hash: 'a0de33bd8893f6a13f78',
//     startTime: 1439969525156,
//     endTime: 1439969525645
//   }
// }

如果您需要更多详细信息,请告诉我。

"webpack": "^1.10.5"

【问题讨论】:

    标签: javascript node.js build webpack


    【解决方案1】:

    我能够使用done 处理程序中的以下 sn-p 使报告再次工作。以下是在任一模式下编译时的结果摘要:

    run模式
    此处理程序仅触发一次,并在摘要compilation 对象下传递stats 数组。只需遍历 compilation.stats.toString(opts) 它们即可。

    watch 模式
    此处理程序在初始配置数组中每个配置触发一次。每次调用都会传递相应的 stats 对象本身,您可以直接使用 .toString(opts) 对其进行字符串化。

    function done(err, compilation) {
      if (err) {
        console.log('[webpack] error:', err);
        return;
      }
    
      // Stats are populated differently in build vs. watch mode.
      var stats = compilation.stats || [compilation];
      console.log('[webpack] the following asset bundles were built:');
      stats.forEach(function (c) {
        console.log(c.toString(statsOpts));
      });
    };
    

    【讨论】:

      猜你喜欢
      • 2021-01-19
      • 1970-01-01
      • 1970-01-01
      • 2016-12-27
      • 2018-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-21
      相关资源
      最近更新 更多