Webpack多配置回调错误和统计

Webpack multi-config callback errors and stats

本文关键字:统计 错误 回调 配置 Webpack      更新时间:2023-09-26

我试图在监视模式下运行时使用多个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个stats对象),错误被正确地提示出来。

webpack(configs).run(done);
// args {
//   '0': null,
//   '1':  { 
//     stats: [ [Object], [Object] ],
//     hash: '6939dac42dcc6c751bc6a0de33bd8893f6a13f78'
//   }
// }
<<p> 观看模式/strong>
甚至可以多次输出。
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 mode
此处理程序只触发一次,并在摘要compilation对象下传递stats数组。只需循环compilation.stats.toString(opts)

watch mode
这个处理程序在初始配置数组中的每个config触发一次。每次调用都传递相应的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));
  });
};