time-analytics-webpack-plugin
Version:
analytize the time of loaders and plugins
90 lines • 4.22 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Writer = void 0;
const fs_1 = require("fs");
const os_1 = require("os");
const ramda_1 = require("ramda");
const chalk_1 = __importDefault(require("chalk"));
const path_1 = __importDefault(require("path"));
const utils_1 = require("./utils");
const loaderHelper_1 = require("./loaderHelper");
const colorTime = (0, ramda_1.curry)((limit, color, time) => {
if (time >= limit) {
const formatedTime = color(time.toFixed(4) + ' ms');
return color(formatedTime);
}
return undefined;
});
function prettyTime(ms, option) {
const dangerTime = colorTime(option.dangerTimeLimit, chalk_1.default.red);
const warnTime = colorTime(option.warnTimeLimit, chalk_1.default.yellow);
const safeTime = colorTime(0, chalk_1.default.green);
for (const func of [dangerTime, warnTime, safeTime]) {
const res = func(ms);
if (res)
return res;
}
(0, utils_1.fail)('We did not give a pretty message about time, why?');
}
const headerText = '┌── time-analytics-webpack-plugin';
const sectionStartPrefix = '├── ';
const nextLinePrefix = '│ ';
const byTime = (0, ramda_1.descend)((0, ramda_1.prop)('time'));
class Writer {
static writeResult(a, b, c, option) {
const messages = ['', headerText];
// #region meta
messages.push(`${nextLinePrefix}Webpack compile takes ${prettyTime(a.totalTime, option)}`);
// #endregion meta
// #region plugin
messages.push(`${sectionStartPrefix}${chalk_1.default.blue(chalk_1.default.bold('Plugins'))}`);
let allPluginTime = 0;
(0, ramda_1.sort)(byTime, b.pluginsInfo).forEach(({ name: pluginName, time }) => {
messages.push(`${nextLinePrefix}Plugin ${chalk_1.default.bold(pluginName)} takes ${prettyTime(time, option)}`);
allPluginTime += time;
});
messages.push(`${nextLinePrefix}All plugins take ${prettyTime(allPluginTime, option)}`);
// #endregion plugin
// #region loaders
messages.push(`${sectionStartPrefix}${chalk_1.default.blue(chalk_1.default.bold('Loaders'))}`);
let allLoaderTime = 0;
let isDuplicatedLodaerIdOutputed = false;
const loaderIdSet = new Set();
(0, ramda_1.sort)(byTime, c.loadersInfo).forEach(({ path: loaderPath, time }) => {
if (time === 0) {
messages.push(`${nextLinePrefix}Loader ${chalk_1.default.bold(loaderPath)} is ignored by "loader.exclude" option`);
return;
}
allLoaderTime += time;
const loaderName = (0, loaderHelper_1.getLoaderName)(loaderPath);
const loaderId = option.groupLoaderByPath ? loaderPath : loaderName;
if (loaderIdSet.has(loaderId)) {
isDuplicatedLodaerIdOutputed = true;
}
loaderIdSet.add(loaderId);
messages.push(`${nextLinePrefix}Loader ${chalk_1.default.bold(loaderId)} takes ${prettyTime(time, option)}`);
});
if (isDuplicatedLodaerIdOutputed) {
messages.push(`${nextLinePrefix}There are many differnt loaders that have same assumed name. Consider use "loader.groupedByAbsolutePath" option to show the full path of loaders.`);
}
messages.push(`${nextLinePrefix}All loaders take ${prettyTime(allLoaderTime, option)}`);
// #endregion loaders
messages.push('');
const content = messages.join(os_1.EOL);
// #region meta
if (option.filePath) {
const outputFileAbsolutePath = path_1.default.resolve(option.filePath);
utils_1.ConsoleHelper.log(`try to write file to file "${outputFileAbsolutePath}"`);
(0, fs_1.writeFileSync)(option.filePath, content);
}
else {
console.log(content);
}
// #endregion meta
}
}
exports.Writer = Writer;
//# sourceMappingURL=writer.js.map