forked from jedwards1211/meteor-webpack-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddProgressPlugin.js
51 lines (49 loc) · 1.4 KB
/
addProgressPlugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var webpack = require('webpack');
// this is copped straight from webpack source
module.exports = function addProgressPlugin(options) {
if (!options.plugins) options.plugins = [];
var chars = 0, lastState, lastStateTime;
options.plugins.push(new webpack.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);
}
}