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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _webpack = require('webpack');
var _webpack2 = _interopRequireDefault(_webpack);
var _chalk = require('chalk');
var _chalk2 = _interopRequireDefault(_chalk);
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
var _logUpdate = require('log-update');
var _logUpdate2 = _interopRequireDefault(_logUpdate);
var _stdEnv = require('std-env');
var _stdEnv2 = _interopRequireDefault(_stdEnv);
var _consola = require('consola');
var _consola2 = _interopRequireDefault(_consola);
var _prettyTime = require('pretty-time');
var _prettyTime2 = _interopRequireDefault(_prettyTime);
var _profile = require('./profile');
var _profile2 = _interopRequireDefault(_profile);
var _utils = require('./utils');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const sharedState = {};
const defaults = {
name: 'webpack',
color: 'green',
profile: false,
compiledIn: true,
done: null,
minimal: _stdEnv2.default.minimalCLI,
stream: null
};
const hasRunning = () => Object.values(sharedState).find(s => s.isRunning);
const $logUpdate = _logUpdate2.default.create(process.stderr, {
showCursor: false
});
class WebpackBarPlugin extends _webpack2.default.ProgressPlugin {
constructor(options) {
super();
this.options = Object.assign({}, defaults, options);
// this.handler will be called by webpack.ProgressPlugin
this.handler = (percent, msg, ...details) => this.updateProgress(percent, msg, details);
this._render = _lodash2.default.throttle(this.render, 100);
this.logUpdate = this.options.logUpdate || $logUpdate;
if (!this.state) {
sharedState[this.options.name] = {
isRunning: false,
color: this.options.color,
profile: this.options.profile ? new _profile2.default(this.options.name) : null
};
}
}
get state() {
return sharedState[this.options.name];
}
get stream() {
return this.options.stream || process.stderr;
}
apply(compiler) {
super.apply(compiler);
const hook = stats => {
this.state.stats = stats;
if (!hasRunning()) {
this.logUpdate.clear();
this.done();
}
};
if (compiler.hooks) {
compiler.hooks.done.tap('WebpackBar', hook);
} else {
compiler.plugin('done', hook);
}
}
done() {
if (this.options.profile) {
const stats = this.state.profile.getStats();
const statsStr = (0, _utils.formatStats)(stats);
this.stream.write(`\n${statsStr}\n`);
}
if (typeof this.options.done === 'function') {
this.options.done(sharedState, this);
}
}
updateProgress(percent, msg, details) {
const progress = Math.floor(percent * 100);
const isRunning = progress < 100;
const wasRunning = this.state.isRunning;
Object.assign(this.state, {
progress,
msg: isRunning && msg ? msg : '',
details: details || [],
request: (0, _utils.parseRequst)(details[2]),
isRunning
});
if (!wasRunning && isRunning) {
// Started
delete this.state.stats;
this.state.start = process.hrtime();
if (this.options.minimal) {
_consola2.default.info(`Compiling ${this.options.name}`);
}
} else if (wasRunning && !isRunning) {
// Finished
const time = process.hrtime(this.state.start);
if (this.options.minimal) {
_consola2.default.success(`Compiled ${this.options.name} in ${(0, _prettyTime2.default)(time)}`);
} else {
this.logUpdate.clear();
if (this.options.compiledIn) {
_consola2.default.success(`${this.options.name} compiled in ${(0, _prettyTime2.default)(time, 'ms')}`);
}
}
delete this.state.start;
}
if (this.options.profile) {
this.state.profile.onRequest(this.state.request);
}
this._render();
}
render() {
if (this.options.minimal) {
return;
}
const columns = this.stream.columns || 80;
const stateLines = _lodash2.default.sortBy(Object.keys(sharedState), n => n).map(name => {
const state = sharedState[name];
const color = (0, _utils.colorize)(state.color);
if (!state.isRunning) {
const color2 = state.progress === 100 ? color : _chalk2.default.grey;
return color2(`${_utils.BULLET} ${name}\n`);
}
return `${[color(_utils.BULLET), color(name), (0, _utils.renderBar)(state.progress, state.color), state.msg, `(${state.progress || 0}%)`, _chalk2.default.grey(state.details && state.details[0] || ''), _chalk2.default.grey(state.details && state.details[1] || '')].join(' ')}\n ${state.request ? _chalk2.default.grey((0, _utils.elipsesLeft)((0, _utils.formatRequest)(state.request), columns - 2)) : ''}\n`;
});
if (hasRunning()) {
const title = _chalk2.default.underline.blue('Compiling');
const log = `\n${title}\n\n${stateLines.join('\n')}`;
this.logUpdate(log);
}
}
}
exports.default = WebpackBarPlugin;
|