UNPKG

bundle-stats

Version:

In-depth bundle analyzer for webpack(bundle size, assets, modules, packages)

127 lines 5.41 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = run; const path_1 = __importDefault(require("path")); const fs_extra_1 = require("fs-extra"); const listr2_1 = require("listr2"); const lodash_1 = require("lodash"); const boxen_1 = __importDefault(require("boxen")); require("@bundle-stats/utils/lib/polyfills"); const utils_1 = require("@bundle-stats/utils"); const plugin_webpack_filter_1 = __importDefault(require("@bundle-stats/plugin-webpack-filter")); const plugin_webpack_validate_1 = __importDefault(require("@bundle-stats/plugin-webpack-validate")); const cli_utils_1 = require("@bundle-stats/cli-utils"); const locales_json_1 = __importDefault(require("./locales.json")); const REPORT_INFO_COLORS = { CHANGE: 'blue', HIGH_NEGATIVE: 'red', NEGATIVE: 'red', LOW_NEGATIVE: 'yellow', NO_CHANGE: 'gray', LOW_POSITIVE: 'green', POSITIVE: 'green', HIGH_POSITIVE: 'green', }; async function run(options) { const { baseline, baselineFilepath, compare, html, json, outDir, artifactFilepaths } = options; const baselineAbsolutePath = (0, cli_utils_1.getBaselinePath)(process.cwd(), outDir, baselineFilepath); const baselinePath = (0, cli_utils_1.getBaselineRelativePath)(process.cwd(), '', baselineAbsolutePath); const tasks = new listr2_1.Listr([ { title: 'Read Webpack stats files', task: async (ctx) => { const sources = await Promise.all(artifactFilepaths.map((filepath) => (0, cli_utils_1.readJSONStream)(filepath))); sources.forEach((source, index) => { const invalid = (0, plugin_webpack_validate_1.default)(source); if (invalid) { throw new Error(`${invalid}\n${locales_json_1.default.WEBPACK_CONFIGURATION_URL}\nFilepath: ${artifactFilepaths[index]}`); } }); ctx.sources = sources.map((source) => ({ webpack: (0, plugin_webpack_filter_1.default)(source) })); }, }, { title: 'Read baseline data', task: async (ctx, task) => { task.title = `${task.title} (${baselinePath})`; ctx.sources = ctx.sources.concat([{ webpack: (0, plugin_webpack_filter_1.default)(ctx.baselineStats) }]); }, skip: async (ctx) => { if (!compare) { return cli_utils_1.TEXT.CLI_NO_COMPARE_MODE; } if (artifactFilepaths.length !== 1) { return cli_utils_1.TEXT.CLI_MULTIPLE_STATS; } let baselineStats = {}; try { baselineStats = await (0, cli_utils_1.readBaseline)(baselineAbsolutePath); ctx.baselineStats = baselineStats; } catch (err) { return cli_utils_1.TEXT.BASELINE_MISSING; } return false; }, }, { title: 'Write baseline data', task: (ctx, task) => { const stats = (0, lodash_1.get)(ctx, 'sources[0]webpack'); const filteredWebpackStats = (0, plugin_webpack_filter_1.default)(stats); return (0, cli_utils_1.writeBaseline)(filteredWebpackStats, baselineAbsolutePath).then(() => { task.title = `${task.title} (${baselinePath})`; }); }, skip: () => !baseline && cli_utils_1.TEXT.CLI_NO_BASELINE, }, { title: 'Process data', task: (ctx) => { ctx.jobs = (0, utils_1.createJobs)(ctx.sources); ctx.report = (0, utils_1.createReport)(ctx.jobs); }, }, { title: 'Generate reports', task: async (ctx) => { ctx.artifacts = await (0, cli_utils_1.createArtifacts)(ctx.jobs, ctx.report, { html, json }); }, }, { title: 'Save reports', task: (ctx) => new listr2_1.Listr(Object.values(ctx.artifacts).map(({ filename, output }) => ({ title: filename, task: async () => { const filepath = path_1.default.join(outDir, filename); await (0, fs_extra_1.outputFile)(filepath, output); ctx.output = [...(ctx.output ? ctx.output : []), filepath]; }, })), { concurrent: true }), }, ]); let result = {}; try { result = await tasks.run(); } catch (err) { console.error(err.message); } const { output, report } = result; const reportInfo = (0, cli_utils_1.getReportInfo)(report); if (reportInfo?.text) { const infoBox = (0, boxen_1.default)(reportInfo.text, { padding: 1, borderColor: reportInfo.info?.deltaType ? REPORT_INFO_COLORS[reportInfo.info.deltaType] : REPORT_INFO_COLORS.NO_CHANGE, }); console.log(`\n${infoBox}`); } console.log('\nArtifacts:'); output?.map((reportPath) => console.log(`- ${reportPath}`)); } //# sourceMappingURL=run.js.map