Refactor(ES6): ProgressPlugin

This commit is contained in:
SendilKumar N 2017-02-22 15:57:56 +08:00
parent a2c5d79b8a
commit 3e22f61bf1
2 changed files with 164 additions and 164 deletions

View File

@ -2,7 +2,11 @@
MIT License http://www.opensource.org/licenses/mit-license.php MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra Author Tobias Koppers @sokra
*/ */
function ProgressPlugin(options) { "use strict";
class ProgressPlugin {
constructor(options) {
if(typeof options === "function") { if(typeof options === "function") {
options = { options = {
handler: options handler: options
@ -12,47 +16,42 @@ function ProgressPlugin(options) {
this.profile = options.profile; this.profile = options.profile;
this.handler = options.handler; this.handler = options.handler;
} }
module.exports = ProgressPlugin;
ProgressPlugin.prototype.apply = function(compiler) { apply(compiler) {
var handler = this.handler || defaultHandler; const handler = this.handler || defaultHandler;
var profile = this.profile; const profile = this.profile;
if(compiler.compilers) { if(compiler.compilers) {
var states = new Array(compiler.compilers.length); const states = new Array(compiler.compilers.length);
compiler.compilers.forEach(function(compiler, idx) { compiler.compilers.forEach(function(compiler, idx) {
compiler.apply(new ProgressPlugin(function(p, msg) { compiler.apply(new ProgressPlugin((p, msg) => {
states[idx] = Array.prototype.slice.apply(arguments); states[idx] = Array.prototype.slice.apply(arguments);
handler.apply(null, [ handler(...[
states.map(function(state) { states.map(state => state && state[0] || 0).reduce((a, b) => a + b) / states.length,
return state && state[0] || 0; `[${idx}] ${msg}`
}).reduce(function(a, b) {
return a + b;
}) / states.length,
"[" + idx + "] " + msg
].concat(Array.prototype.slice.call(arguments, 2))); ].concat(Array.prototype.slice.call(arguments, 2)));
})); }));
}); });
} else { } else {
var lastModulesCount = 0; let lastModulesCount = 0;
var moduleCount = 500; let moduleCount = 500;
var doneModules = 0; let doneModules = 0;
var activeModules = []; const activeModules = [];
var update = function update(module) { const update = function update(module) {
handler( handler(
0.1 + (doneModules / Math.max(lastModulesCount, moduleCount)) * 0.6, 0.1 + (doneModules / Math.max(lastModulesCount, moduleCount)) * 0.6,
"building modules", "building modules",
doneModules + "/" + moduleCount + " modules", `${doneModules}/${moduleCount} modules`,
activeModules.length + " active", `${activeModules.length} active`,
activeModules[activeModules.length - 1] activeModules[activeModules.length - 1]
); );
}; };
var moduleDone = function moduleDone(module) { const moduleDone = function moduleDone(module) {
doneModules++; doneModules++;
var ident = module.identifier(); const ident = module.identifier();
if(ident) { if(ident) {
var idx = activeModules.indexOf(ident); const idx = activeModules.indexOf(ident);
if(idx >= 0) activeModules.splice(idx, 1); if(idx >= 0) activeModules.splice(idx, 1);
} }
update(); update();
@ -65,7 +64,7 @@ ProgressPlugin.prototype.apply = function(compiler) {
handler(0, "compiling"); handler(0, "compiling");
compilation.plugin("build-module", function(module) { compilation.plugin("build-module", function(module) {
moduleCount++; moduleCount++;
var ident = module.identifier(); const ident = module.identifier();
if(ident) { if(ident) {
activeModules.push(ident); activeModules.push(ident);
} }
@ -73,7 +72,7 @@ ProgressPlugin.prototype.apply = function(compiler) {
}); });
compilation.plugin("failed-module", moduleDone); compilation.plugin("failed-module", moduleDone);
compilation.plugin("succeed-module", moduleDone); compilation.plugin("succeed-module", moduleDone);
var syncHooks = { const syncHooks = {
"seal": [0.71, "sealing"], "seal": [0.71, "sealing"],
"optimize": [0.72, "optimizing"], "optimize": [0.72, "optimizing"],
"optimize-modules-basic": [0.73, "basic module optimization"], "optimize-modules-basic": [0.73, "basic module optimization"],
@ -95,63 +94,63 @@ ProgressPlugin.prototype.apply = function(compiler) {
"additional-chunk-assets": [0.89, "additional chunk assets processing"], "additional-chunk-assets": [0.89, "additional chunk assets processing"],
"record": [0.90, "recording"] "record": [0.90, "recording"]
}; };
Object.keys(syncHooks).forEach(function(name) { Object.keys(syncHooks).forEach(name => {
var pass = 0; let pass = 0;
var settings = syncHooks[name]; const settings = syncHooks[name];
compilation.plugin(name, function() { compilation.plugin(name, () => {
if(pass++ > 0) if(pass++ > 0)
handler(settings[0], settings[1], "pass " + pass); handler(settings[0], settings[1], `pass ${pass}`);
else else
handler(settings[0], settings[1]); handler(settings[0], settings[1]);
}); });
}); });
compilation.plugin("optimize-tree", function(chunks, modules, callback) { compilation.plugin("optimize-tree", (chunks, modules, callback) => {
handler(0.79, "module and chunk tree optimization"); handler(0.79, "module and chunk tree optimization");
callback(); callback();
}); });
compilation.plugin("additional-assets", function(callback) { compilation.plugin("additional-assets", callback => {
handler(0.91, "additional asset processing"); handler(0.91, "additional asset processing");
callback(); callback();
}); });
compilation.plugin("optimize-chunk-assets", function(chunks, callback) { compilation.plugin("optimize-chunk-assets", (chunks, callback) => {
handler(0.92, "chunk asset optimization"); handler(0.92, "chunk asset optimization");
callback(); callback();
}); });
compilation.plugin("optimize-assets", function(assets, callback) { compilation.plugin("optimize-assets", (assets, callback) => {
handler(0.94, "asset optimization"); handler(0.94, "asset optimization");
callback(); callback();
}); });
}); });
compiler.plugin("emit", function(compilation, callback) { compiler.plugin("emit", (compilation, callback) => {
handler(0.95, "emitting"); handler(0.95, "emitting");
callback(); callback();
}); });
compiler.plugin("done", function() { compiler.plugin("done", () => {
handler(1, ""); handler(1, "");
}); });
} }
var lineCaretPosition = 0, let lineCaretPosition = 0,
lastState, lastStateTime; lastState, lastStateTime;
function defaultHandler(percentage, msg) { function defaultHandler(percentage, msg) {
var state = msg; let state = msg;
var details = Array.prototype.slice.call(arguments, 2); const details = Array.prototype.slice.call(arguments, 2);
if(percentage < 1) { if(percentage < 1) {
percentage = Math.floor(percentage * 100); percentage = Math.floor(percentage * 100);
msg = percentage + "% " + msg; msg = `${percentage}% ${msg}`;
if(percentage < 100) { if(percentage < 100) {
msg = " " + msg; msg = ` ${msg}`;
} }
if(percentage < 10) { if(percentage < 10) {
msg = " " + msg; msg = ` ${msg}`;
} }
details.forEach(function(detail) { details.forEach(detail => {
if(!detail) return; if(!detail) return;
if(detail.length > 40) { if(detail.length > 40) {
detail = "..." + detail.substr(detail.length - 37); detail = `...${detail.substr(detail.length - 37)}`;
} }
msg += " " + detail; msg += ` ${detail}`;
}); });
} }
if(profile) { if(profile) {
@ -160,9 +159,9 @@ ProgressPlugin.prototype.apply = function(compiler) {
lastState = null; lastState = null;
lastStateTime = +new Date(); lastStateTime = +new Date();
} else if(state !== lastState || percentage === 1) { } else if(state !== lastState || percentage === 1) {
var now = +new Date(); const now = +new Date();
if(lastState) { if(lastState) {
var stateMsg = (now - lastStateTime) + "ms " + lastState; const stateMsg = `${now - lastStateTime}ms ${lastState}`;
goToLineStart(stateMsg); goToLineStart(stateMsg);
process.stderr.write(stateMsg + "\n"); process.stderr.write(stateMsg + "\n");
lineCaretPosition = 0; lineCaretPosition = 0;
@ -176,7 +175,7 @@ ProgressPlugin.prototype.apply = function(compiler) {
} }
function goToLineStart(nextMessage) { function goToLineStart(nextMessage) {
var str = ""; let str = "";
for(; lineCaretPosition > nextMessage.length; lineCaretPosition--) { for(; lineCaretPosition > nextMessage.length; lineCaretPosition--) {
str += "\b \b"; str += "\b \b";
} }
@ -186,4 +185,6 @@ ProgressPlugin.prototype.apply = function(compiler) {
lineCaretPosition = nextMessage.length; lineCaretPosition = nextMessage.length;
if(str) process.stderr.write(str); if(str) process.stderr.write(str);
} }
}; }
}
module.exports = ProgressPlugin;

View File

@ -52,5 +52,4 @@ class ProvidePlugin {
}); });
} }
} }
module.exports = ProvidePlugin; module.exports = ProvidePlugin;