Skip to content

Commit 01e7a7c

Browse files
committed
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#1000, SRP
1 parent 1b9e880 commit 01e7a7c

2 files changed

Lines changed: 51 additions & 47 deletions

File tree

bin/convert-argv.js

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -382,52 +382,7 @@ module.exports = function(optimist, argv, convertOptions) {
382382
ifBooleanArg("progress", function() {
383383
var ProgressPlugin = require("../lib/ProgressPlugin");
384384
ensureArray(options, "plugins");
385-
var chars = 0,
386-
lastState, lastStateTime;
387-
options.plugins.push(new ProgressPlugin(function(percentage, msg) {
388-
var state = msg;
389-
if(percentage < 1) {
390-
percentage = Math.floor(percentage * 100);
391-
msg = percentage + "% " + msg;
392-
if(percentage < 100) {
393-
msg = " " + msg;
394-
}
395-
if(percentage < 10) {
396-
msg = " " + msg;
397-
}
398-
}
399-
if(options.profile) {
400-
state = state.replace(/^\d+\/\d+\s+/, "");
401-
if(percentage === 0) {
402-
lastState = null;
403-
lastStateTime = +new Date();
404-
} else if(state !== lastState || percentage === 1) {
405-
var now = +new Date();
406-
if(lastState) {
407-
var stateMsg = (now - lastStateTime) + "ms " + lastState;
408-
goToLineStart(stateMsg);
409-
process.stderr.write(stateMsg + "\n");
410-
chars = 0;
411-
}
412-
lastState = state;
413-
lastStateTime = now;
414-
}
415-
}
416-
goToLineStart(msg);
417-
process.stderr.write(msg);
418-
}));
419-
420-
function goToLineStart(nextMessage) {
421-
var str = "";
422-
for(; chars > nextMessage.length; chars--) {
423-
str += "\b \b";
424-
}
425-
chars = nextMessage.length;
426-
for(var i = 0; i < chars; i++) {
427-
str += "\b";
428-
}
429-
if(str) process.stderr.write(str);
430-
}
385+
options.plugins.push(new ProgressPlugin());
431386
});
432387

433388
ifArg("devtool", function(value) {

lib/ProgressPlugin.js

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function ProgressPlugin(handler) {
88
module.exports = ProgressPlugin;
99

1010
ProgressPlugin.prototype.apply = function(compiler) {
11-
var handler = this.handler;
11+
var handler = this.handler || defaultHandler;
1212
if(compiler.compilers) {
1313
var states = new Array(compiler.compilers.length);
1414
compiler.compilers.forEach(function(compiler, idx) {
@@ -77,4 +77,53 @@ ProgressPlugin.prototype.apply = function(compiler) {
7777
handler(1, "");
7878
});
7979
}
80+
81+
var chars = 0,
82+
lastState, lastStateTime;
83+
84+
function defaultHandler(percentage, msg) {
85+
var state = msg;
86+
87+
function goToLineStart(nextMessage) {
88+
var str = "";
89+
for(; chars > nextMessage.length; chars--) {
90+
str += "\b \b";
91+
}
92+
chars = nextMessage.length;
93+
for(var i = 0; i < chars; i++) {
94+
str += "\b";
95+
}
96+
if(str) process.stderr.write(str);
97+
}
98+
99+
if(percentage < 1) {
100+
percentage = Math.floor(percentage * 100);
101+
msg = percentage + "% " + msg;
102+
if(percentage < 100) {
103+
msg = " " + msg;
104+
}
105+
if(percentage < 10) {
106+
msg = " " + msg;
107+
}
108+
}
109+
if(compiler.options.profile) {
110+
state = state.replace(/^\d+\/\d+\s+/, "");
111+
if(percentage === 0) {
112+
lastState = null;
113+
lastStateTime = +new Date();
114+
} else if(state !== lastState || percentage === 1) {
115+
var now = +new Date();
116+
if(lastState) {
117+
var stateMsg = (now - lastStateTime) + "ms " + lastState;
118+
goToLineStart(stateMsg);
119+
process.stderr.write(stateMsg + "\n");
120+
chars = 0;
121+
}
122+
lastState = state;
123+
lastStateTime = now;
124+
}
125+
}
126+
goToLineStart(msg);
127+
process.stderr.write(msg);
128+
}
80129
};

0 commit comments

Comments
 (0)