219 lines
11 KiB
JavaScript
219 lines
11 KiB
JavaScript
import Table from 'cli-table3';
|
|
import { minify } from 'terser';
|
|
import { filesize } from 'filesize';
|
|
import { gzipSize } from 'gzip-size';
|
|
import brotli from 'brotli-size';
|
|
|
|
/******************************************************************************
|
|
Copyright (c) Microsoft Corporation.
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
purpose with or without fee is hereby granted.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
PERFORMANCE OF THIS SOFTWARE.
|
|
***************************************************************************** */
|
|
|
|
function __awaiter(thisArg, _arguments, P, generator) {
|
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
});
|
|
}
|
|
|
|
function getFileSize(input) {
|
|
const length = typeof input === "string" ? Buffer.byteLength(input) : input;
|
|
return filesize(length);
|
|
}
|
|
function getMinifiedSize(text) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
const minified = yield minify(text);
|
|
return Buffer.byteLength(minified.code || "");
|
|
});
|
|
}
|
|
function getGzippedSize(text) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
return yield gzipSize(text);
|
|
});
|
|
}
|
|
function getBrotliSize(text) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
const brotliSize = brotli.default || brotli;
|
|
return brotliSize(text);
|
|
});
|
|
}
|
|
const color = {
|
|
black: str => `\x1b[30m${str}\x1b[0m`,
|
|
red: str => `\x1b[31m${str}\x1b[0m`,
|
|
green: str => `\x1b[32m${str}\x1b[0m`,
|
|
yellow: str => `\x1b[33m${str}\x1b[0m`,
|
|
blue: str => `\x1b[34m${str}\x1b[0m`,
|
|
magenta: str => `\x1b[35m${str}\x1b[0m`,
|
|
cyan: str => `\x1b[36m${str}\x1b[0m`,
|
|
white: str => `\x1b[37m${str}\x1b[0m`,
|
|
gray: str => `\x1b[90m${str}\x1b[0m`,
|
|
};
|
|
function reportWarning(descriptor, warnLow, warnHigh) {
|
|
switch (true) {
|
|
case descriptor.value > warnHigh:
|
|
return color.red(descriptor.displayValue);
|
|
case descriptor.value > warnLow:
|
|
return color.yellow(descriptor.displayValue);
|
|
default:
|
|
return color.green(descriptor.displayValue);
|
|
}
|
|
}
|
|
function generateSeparator(list) {
|
|
const numOfColumns = list[0].length;
|
|
const output = Array(numOfColumns).fill("-");
|
|
list.forEach(row => {
|
|
row.forEach((str, i) => {
|
|
if (str.length > output[i].length) {
|
|
output[i] = "-".repeat(str.length);
|
|
}
|
|
});
|
|
});
|
|
return output.map(i => color.gray(i));
|
|
}
|
|
function uuid() {
|
|
return Date.now().toString(36) + Math.random().toString(36).substring(2);
|
|
}
|
|
|
|
/** Prints out a summary of the rollup build */
|
|
function summary({ warnLow = 5e3, warnHigh = 1e4, totalLow = 2e5, totalHigh = 3e5, showBrotliSize = false, showGzippedSize = false, showMinifiedSize = false, } = {}) {
|
|
const info = new Map();
|
|
return {
|
|
name: "rollup-plugin-summary",
|
|
generateBundle: function (options, bundle) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
const identifierList = [];
|
|
options.dir && identifierList.push(options.dir);
|
|
options.file && identifierList.push(options.file);
|
|
options.format && identifierList.push(options.format);
|
|
const identifier = identifierList.length ? identifierList.join(" - ") : uuid();
|
|
if (!info.has(identifier)) {
|
|
info.set(identifier, []);
|
|
}
|
|
const defaultDescriptor = () => ({
|
|
value: 0,
|
|
displayValue: "0 B",
|
|
coloredValue: "0 B",
|
|
});
|
|
const totals = { fileName: "Totals", size: defaultDescriptor() };
|
|
for (const fileName in bundle) {
|
|
if (bundle[fileName].type === "chunk") {
|
|
const chunk = bundle[fileName];
|
|
// TODO: Investigate this
|
|
const code = chunk.code;
|
|
const warn = [warnLow || 0, warnHigh || 0];
|
|
const totalWarn = [totalLow || 0, totalHigh || 0];
|
|
/** @type {SummaryChunkInfo} */
|
|
const chunkInfo = { fileName, size: defaultDescriptor() };
|
|
chunkInfo.size.value = Buffer.byteLength(code);
|
|
chunkInfo.size.displayValue = getFileSize(chunkInfo.size.value);
|
|
chunkInfo.size.coloredValue = reportWarning(chunkInfo.size, ...warn);
|
|
totals.size.value += chunkInfo.size.value;
|
|
totals.size.displayValue = getFileSize(totals.size.value);
|
|
totals.size.coloredValue = reportWarning(totals.size, ...totalWarn);
|
|
if (showMinifiedSize) {
|
|
if (!chunkInfo.minified) {
|
|
chunkInfo.minified = defaultDescriptor();
|
|
}
|
|
if (!totals.minified) {
|
|
totals.minified = defaultDescriptor();
|
|
}
|
|
chunkInfo.minified.value = yield getMinifiedSize(code);
|
|
chunkInfo.minified.displayValue = getFileSize(chunkInfo.minified.value);
|
|
chunkInfo.minified.coloredValue = reportWarning(chunkInfo.minified, ...warn);
|
|
totals.minified.value += chunkInfo.minified.value;
|
|
totals.minified.displayValue = getFileSize(totals.minified.value);
|
|
totals.minified.coloredValue = reportWarning(totals.minified, ...totalWarn);
|
|
}
|
|
if (showGzippedSize) {
|
|
if (!chunkInfo.gzipped) {
|
|
chunkInfo.gzipped = defaultDescriptor();
|
|
}
|
|
if (!totals.gzipped) {
|
|
totals.gzipped = defaultDescriptor();
|
|
}
|
|
chunkInfo.gzipped.value = yield getGzippedSize(code);
|
|
chunkInfo.gzipped.displayValue = getFileSize(chunkInfo.gzipped.value);
|
|
chunkInfo.gzipped.coloredValue = reportWarning(chunkInfo.gzipped, ...warn);
|
|
totals.gzipped.value += chunkInfo.gzipped.value;
|
|
totals.gzipped.displayValue = getFileSize(totals.gzipped.value);
|
|
totals.gzipped.coloredValue = reportWarning(totals.gzipped, ...totalWarn);
|
|
}
|
|
if (showBrotliSize) {
|
|
if (!chunkInfo.brotli) {
|
|
chunkInfo.brotli = defaultDescriptor();
|
|
}
|
|
if (!totals.brotli) {
|
|
totals.brotli = defaultDescriptor();
|
|
}
|
|
chunkInfo.brotli.value = yield getBrotliSize(code);
|
|
chunkInfo.brotli.displayValue = getFileSize(chunkInfo.brotli.value);
|
|
chunkInfo.brotli.coloredValue = reportWarning(chunkInfo.brotli, ...warn);
|
|
totals.brotli.value += chunkInfo.brotli.value;
|
|
totals.brotli.displayValue = getFileSize(totals.brotli.value);
|
|
totals.brotli.coloredValue = reportWarning(totals.brotli, ...totalWarn);
|
|
}
|
|
info.get(identifier).push(chunkInfo);
|
|
}
|
|
}
|
|
info.get(identifier).push(totals);
|
|
});
|
|
},
|
|
closeBundle: function () {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
info.forEach((output, dir) => {
|
|
/** @type {string[]} */
|
|
const headers = ["File name", "Size"];
|
|
showMinifiedSize && headers.push("Minified");
|
|
showGzippedSize && headers.push("Gzipped");
|
|
showBrotliSize && headers.push("Brotli");
|
|
const table = new Table({
|
|
head: headers,
|
|
chars: { mid: "", "left-mid": "", "mid-mid": "", "right-mid": "" },
|
|
style: { head: ["blue"] },
|
|
});
|
|
const printable = output.map(file => {
|
|
const output = [file.fileName, file.size.coloredValue];
|
|
showMinifiedSize && output.push(file.minified.coloredValue);
|
|
showGzippedSize && output.push(file.gzipped.coloredValue);
|
|
showBrotliSize && output.push(file.brotli.coloredValue);
|
|
return output;
|
|
});
|
|
const totalsRow = printable.pop();
|
|
const separator = generateSeparator([
|
|
headers,
|
|
...output.map(file => {
|
|
const output = [file.fileName, file.size.displayValue];
|
|
showMinifiedSize && output.push(file.minified.displayValue);
|
|
showGzippedSize && output.push(file.gzipped.displayValue);
|
|
showBrotliSize && output.push(file.brotli.displayValue);
|
|
return output;
|
|
}),
|
|
]);
|
|
table.push(separator);
|
|
table.push(...printable);
|
|
table.push(separator);
|
|
table.push(totalsRow);
|
|
console.log("Build summary for", color.cyan(dir));
|
|
console.log(table.toString());
|
|
});
|
|
});
|
|
},
|
|
};
|
|
}
|
|
|
|
export { summary as default, summary };
|
|
//# sourceMappingURL=index.js.map
|