'Webpack progress using node.js API

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.



Solution 1:[1]

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

Solution 2:[2]

https://www.npmjs.com/package/progress-bar-webpack-plugin this plugin leverages node-progress.

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

Solution 3:[3]

This question is six years old!!! but also, if you're using bash or any term supporting ANSI/VT100 escape codes ...

handler(percentage, message, ...args) { 
  console.info(`\u001b[A\u001b[K\u001b[33m${(percentage * 100).toFixed(2)}%`+
    `\t\u001b[0m\u001b[1m${message}\t`+
    `\u001b[0m\u001b[90m${args && args.length>0?args[0]:""}\u001b[0m`);
}

(handler function of ProgressPlugin from Webpack 5.x)

Prints it in same line, with percentage in yellow, message in default console color, and first arg if any in dark grey.

print out sample

More about escape ANSI chars for bash or ANSI/VT100 compatible term: https://misc.flogisoft.com/bash/tip_colors_and_formatting

Now, let's go to code important stuff!!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 François Romain
Solution 2 Evi Song
Solution 3