Webpack progress using node.js API

后端 未结 3 1864
[愿得一人]
[愿得一人] 2021-02-05 04:21

Is there currently a way to access webpack\'s progress while using the node.js API? I\'m familiar with the --progress flag using the CLI.

3条回答
  •  深忆病人
    2021-02-05 04:43

    To output something similar as the CLI --progress flag:

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

提交回复
热议问题