2015-06-25 12 views
23

Gibt es derzeit eine Möglichkeit, während der Verwendung der API node.js auf den Fortschritt des Webpacks zuzugreifen? Ich kenne den --progress -Flag mit dem CLI.Webpack-Fortschritt mit node.js-API

Antwort

26

Die Webpack CLI verwendet den ProgressPlugin, um den Fortschritt einer Kompilierung zu protokollieren.

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) { 
    // ... 
}); 

Hier ist ein Link auf die Compiler documentation und der ProgressPlugin documentation.

+4

Pair dieser mit [node-progress] (https://github.com/tj/node-progress) für formatieren Konsolenausgabe. –

+0

Liste aller internen Webpack-Plugins: https://github.com/webpack/docs/wiki/internal-webpack-plugins –

6

Zur Ausgabe etwas ähnliches wie die 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') 
    }) 
+1

das ist großartig, danke. – heisian