UNPKG

esbuild-plugin-filesize

Version:
84 lines (83 loc) 4.17 kB
var __awaiter = (this && this.__awaiter) || function (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()); }); }; import { normalizeOption } from './normalize-option'; import fs from 'fs-extra'; import path from 'path'; import fileSize from 'filesize'; import gzipSize from 'gzip-size'; import terser from 'terser'; import { sync as brotliSync } from 'brotli-size'; import { boxenSingleOutputReporter, boxenMultiOutputReporter, } from './reporter'; export function esbuildPluginFileSize(options = {}) { const normalizedOptions = normalizeOption(options); const formatFileSize = (size) => fileSize(size, normalizedOptions.format); function handleFileSizeDisplay(filePath) { return __awaiter(this, void 0, void 0, function* () { const fileSizeBytes = fs.statSync(filePath).size; const fileContent = fs.readFileSync(filePath, 'utf8'); const minifiedContent = (yield terser.minify(fileContent)).code; const fileSize = formatFileSize(fileSizeBytes); const gzippedSize = formatFileSize(gzipSize.sync(fileContent)); const minifiedSize = formatFileSize(minifiedContent.length); const brotliSize = formatFileSize(brotliSync(fileContent)); return { fileSize, gzippedSize, minifiedSize, brotliSize, }; }); } return { name: 'fileSize', // waiting for buildEnd hook, too... setup({ initialOptions, onEnd }) { return __awaiter(this, void 0, void 0, function* () { const { outdir, outfile } = initialOptions; onEnd(() => __awaiter(this, void 0, void 0, function* () { if (outfile) { const originFilePath = path.resolve(outfile); if (!fs.existsSync(originFilePath)) { return; } const { fileSize, minifiedSize, gzippedSize, brotliSize } = yield handleFileSizeDisplay(originFilePath); boxenSingleOutputReporter(normalizedOptions, { fileSize, fileName: originFilePath, minifiedSize, gzippedSize, outputPath: originFilePath, brotliSize, }); } else if (outdir) { const originDirPath = path.resolve(outdir); if (!fs.existsSync(originDirPath)) { return; } const files = fs .readdirSync(originDirPath) // .filter((str) => // normalizedOptions.exclude.every((ex) => !str.match(ex)) // ) .filter((filePath) => !filePath.endsWith('.map')) .map((filePath) => path.resolve(outdir, filePath)); const infos = []; for (const file of files) { const tmp = yield handleFileSizeDisplay(file); infos.push(Object.assign(Object.assign({}, tmp), { fileName: file, outputPath: file })); } boxenMultiOutputReporter(normalizedOptions, infos); } })); }); }, }; }