Webpack 多配置回调错误和统计信息

Webpack multi-config callback errors and stats

当 运行处于监视模式时,我正在尝试使用多个 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"

我能够使用 done 处理程序中的以下代码段使报告再次工作。以下是在任一模式下编译时的发现摘要:

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));
  });
};