casterly
Version:
CLI for Casterly
140 lines (135 loc) • 6.45 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.measureFileSizesBeforeBuild = exports.printFileSizesAfterBuild = void 0;
/**
MIT License
Copyright (c) 2015-present, Facebook, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// originally taken from https://github.com/facebook/create-react-app/tree/master/packages/react-dev-utils
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const chalk_1 = __importDefault(require("chalk"));
const filesize_1 = __importDefault(require("filesize"));
const gzip_size_1 = require("gzip-size");
const recursive_readdir_1 = __importDefault(require("recursive-readdir"));
const strip_ansi_1 = __importDefault(require("strip-ansi"));
function canReadAsset(asset) {
return (/\.(js|css)$/.test(asset) &&
!/service-worker\.js/.test(asset) &&
!/precache-manifest\.[0-9a-f]+\.js/.test(asset));
}
// Prints a detailed summary of build files.
function printFileSizesAfterBuild(webpackStats, previousSizeMap, buildFolder, maxBundleGzipSize, maxChunkGzipSize) {
const root = previousSizeMap.root;
const sizes = previousSizeMap.sizes;
const assets = [webpackStats]
.map((stats) => stats.toJson({ all: false, assets: true }).assets
.filter((asset) => canReadAsset(asset.name))
.map((asset) => {
const fileContents = fs_1.default.readFileSync(path_1.default.join(root, asset.name));
const size = gzip_size_1.sync(fileContents);
const previousSize = sizes[removeFileNameHash(root, asset.name)];
const difference = getDifferenceLabel(size, previousSize);
return {
folder: path_1.default.join(path_1.default.basename(buildFolder), path_1.default.dirname(asset.name)),
name: path_1.default.basename(asset.name),
size,
sizeLabel: filesize_1.default(size) + (difference ? ' (' + difference + ')' : ''),
};
}))
.reduce((single, all) => all.concat(single), []);
assets.sort((a, b) => b.size - a.size);
const longestSizeLabelLength = Math.max.apply(null, assets.map((a) => strip_ansi_1.default(a.sizeLabel).length));
let suggestBundleSplitting = false;
assets.forEach((asset) => {
let sizeLabel = asset.sizeLabel;
const sizeLength = strip_ansi_1.default(sizeLabel).length;
if (sizeLength < longestSizeLabelLength) {
const rightPadding = ' '.repeat(longestSizeLabelLength - sizeLength);
sizeLabel += rightPadding;
}
const isMainBundle = asset.name.indexOf('main.') === 0;
const maxRecommendedSize = isMainBundle
? maxBundleGzipSize
: maxChunkGzipSize;
const isLarge = maxRecommendedSize && asset.size > maxRecommendedSize;
if (isLarge && path_1.default.extname(asset.name) === '.js') {
suggestBundleSplitting = true;
}
console.log(' ' +
(isLarge ? chalk_1.default.yellow(sizeLabel) : sizeLabel) +
' ' +
chalk_1.default.dim(asset.folder + path_1.default.sep) +
chalk_1.default.cyan(asset.name));
});
if (suggestBundleSplitting) {
console.log();
console.log(chalk_1.default.yellow('The bundle size is significantly larger than recommended.'));
console.log(chalk_1.default.yellow('Consider reducing it with code splitting: https://goo.gl/9VhYWB'));
console.log(chalk_1.default.yellow('You can also analyze the project dependencies: https://goo.gl/LeUzfb'));
}
}
exports.printFileSizesAfterBuild = printFileSizesAfterBuild;
function removeFileNameHash(buildFolder, fileName) {
return fileName
.replace(buildFolder, '')
.replace(/\\/g, '/')
.replace(/\/?(.*)(\.[0-9a-f]+)(\.chunk)?(\.js|\.css)/, (_, p1, __, ___, p4) => p1 + p4);
}
// Input: 1024, 2048
// Output: "(+1 KB)"
function getDifferenceLabel(currentSize, previousSize) {
const FIFTY_KILOBYTES = 1024 * 50;
const difference = currentSize - previousSize;
const fileSize = !Number.isNaN(difference) ? filesize_1.default(difference) : 0;
if (difference >= FIFTY_KILOBYTES) {
return chalk_1.default.red('+' + fileSize);
}
else if (difference < FIFTY_KILOBYTES && difference > 0) {
return chalk_1.default.yellow('+' + fileSize);
}
else if (difference < 0) {
return chalk_1.default.green(fileSize);
}
return '';
}
function measureFileSizesBeforeBuild(buildFolder) {
return new Promise((resolve) => {
recursive_readdir_1.default(buildFolder, (err, fileNames) => {
let sizes = null;
if (!err && fileNames) {
sizes = fileNames
.filter(canReadAsset)
.reduce((memo, fileName) => {
const contents = fs_1.default.readFileSync(fileName);
const key = removeFileNameHash(buildFolder, fileName);
memo[key] = gzip_size_1.sync(contents);
return memo;
}, {});
}
resolve({
root: buildFolder,
sizes: sizes !== null && sizes !== void 0 ? sizes : {},
});
});
});
}
exports.measureFileSizesBeforeBuild = measureFileSizesBeforeBuild;
;