JS API: ProgressPlugin default handler

Default handler moved from CLI args parser to plugin itself.
Now you can use ProgressPlugin in JS API without specifying handler.

Resolves webpack/webpack#1000, SRP
This commit is contained in:
Lex Vjatkin 2015-12-09 16:10:38 +01:00
parent 1b9e880f38
commit 01e7a7cb95
2 changed files with 51 additions and 47 deletions

View File

@ -382,52 +382,7 @@ module.exports = function(optimist, argv, convertOptions) {
ifBooleanArg("progress", function() { ifBooleanArg("progress", function() {
var ProgressPlugin = require("../lib/ProgressPlugin"); var ProgressPlugin = require("../lib/ProgressPlugin");
ensureArray(options, "plugins"); ensureArray(options, "plugins");
var chars = 0, options.plugins.push(new ProgressPlugin());
lastState, lastStateTime;
options.plugins.push(new ProgressPlugin(function(percentage, msg) {
var state = msg;
if(percentage < 1) {
percentage = Math.floor(percentage * 100);
msg = percentage + "% " + msg;
if(percentage < 100) {
msg = " " + msg;
}
if(percentage < 10) {
msg = " " + msg;
}
}
if(options.profile) {
state = state.replace(/^\d+\/\d+\s+/, "");
if(percentage === 0) {
lastState = null;
lastStateTime = +new Date();
} else if(state !== lastState || percentage === 1) {
var now = +new Date();
if(lastState) {
var stateMsg = (now - lastStateTime) + "ms " + lastState;
goToLineStart(stateMsg);
process.stderr.write(stateMsg + "\n");
chars = 0;
}
lastState = state;
lastStateTime = now;
}
}
goToLineStart(msg);
process.stderr.write(msg);
}));
function goToLineStart(nextMessage) {
var str = "";
for(; chars > nextMessage.length; chars--) {
str += "\b \b";
}
chars = nextMessage.length;
for(var i = 0; i < chars; i++) {
str += "\b";
}
if(str) process.stderr.write(str);
}
}); });
ifArg("devtool", function(value) { ifArg("devtool", function(value) {

View File

@ -8,7 +8,7 @@ function ProgressPlugin(handler) {
module.exports = ProgressPlugin; module.exports = ProgressPlugin;
ProgressPlugin.prototype.apply = function(compiler) { ProgressPlugin.prototype.apply = function(compiler) {
var handler = this.handler; var handler = this.handler || defaultHandler;
if(compiler.compilers) { if(compiler.compilers) {
var states = new Array(compiler.compilers.length); var states = new Array(compiler.compilers.length);
compiler.compilers.forEach(function(compiler, idx) { compiler.compilers.forEach(function(compiler, idx) {
@ -77,4 +77,53 @@ ProgressPlugin.prototype.apply = function(compiler) {
handler(1, ""); handler(1, "");
}); });
} }
var chars = 0,
lastState, lastStateTime;
function defaultHandler(percentage, msg) {
var state = msg;
function goToLineStart(nextMessage) {
var str = "";
for(; chars > nextMessage.length; chars--) {
str += "\b \b";
}
chars = nextMessage.length;
for(var i = 0; i < chars; i++) {
str += "\b";
}
if(str) process.stderr.write(str);
}
if(percentage < 1) {
percentage = Math.floor(percentage * 100);
msg = percentage + "% " + msg;
if(percentage < 100) {
msg = " " + msg;
}
if(percentage < 10) {
msg = " " + msg;
}
}
if(compiler.options.profile) {
state = state.replace(/^\d+\/\d+\s+/, "");
if(percentage === 0) {
lastState = null;
lastStateTime = +new Date();
} else if(state !== lastState || percentage === 1) {
var now = +new Date();
if(lastState) {
var stateMsg = (now - lastStateTime) + "ms " + lastState;
goToLineStart(stateMsg);
process.stderr.write(stateMsg + "\n");
chars = 0;
}
lastState = state;
lastStateTime = now;
}
}
goToLineStart(msg);
process.stderr.write(msg);
}
}; };