使用node.js API进行Webpack进度

时间:2015-06-25 14:17:50

标签: webpack

目前是否有办法在使用node.js API时访问webpack的进度?我使用CLI熟悉--progress标志。

3 个答案:

答案 0 :(得分:29)

The Webpack CLI uses the ProgressPlugin to log the progress of a compilation.

var ProgressPlugin = require('webpack/lib/ProgressPlugin');

var compiler = webpack(config);

compiler.apply(new ProgressPlugin(function(percentage, msg) {
  console.log((percentage * 100) + '%', msg);
}));

compiler.run(function(err, stats) {
  // ...
});

Here is a link to the Compiler documentation and the ProgressPlugin documentation.

答案 1 :(得分:12)

输出类似于CLI --progress标志的内容:

    var webpack = require('webpack')
    var ProgressPlugin = require('webpack/lib/ProgressPlugin')
    var config = require('./webpack.config')
    var compiler = webpack(config)

    compiler.apply(new ProgressPlugin(function (percentage, msg, current, active, modulepath) {
      if (process.stdout.isTTY && percentage < 1) {
        process.stdout.cursorTo(0)
        modulepath = modulepath ? ' …' + modulepath.substr(modulepath.length - 30) : ''
        current = current ? ' ' + current : ''
        active = active ? ' ' + active : ''
        process.stdout.write((percentage * 100).toFixed(0) + '% ' + msg + current + active + modulepath + ' ')
        process.stdout.clearLine(1)
      } else if (percentage === 1) {
        process.stdout.write('\n')
        console.log('webpack: done.')
      }
    }))

    compiler.run(function (err, stats) {
      if (err) throw err
      process.stdout.write(stats.toString({
        colors: true,
        modules: false,
        children: false,
        chunks: false,
        chunkModules: false
      }) + '\n\n')
    })

答案 2 :(得分:3)

https://www.npmjs.com/package/progress-bar-webpack-plugin此插件利用节点进度。

  new ProgressBarPlugin({
    format: '  build [:bar] :percent (:elapsed seconds)',
    clear: false, 
    width: 60
  })