"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildReporterPlugin = void 0;
const path_1 = __importDefault(require("path"));
const chalk_1 = __importDefault(require("chalk"));
const brotli_size_1 = require("brotli-size");
const writeColors = {
    [0 /* JS */]: chalk_1.default.cyan,
    [1 /* CSS */]: chalk_1.default.magenta,
    [2 /* ASSET */]: chalk_1.default.green,
    [3 /* HTML */]: chalk_1.default.blue,
    [4 /* SOURCE_MAP */]: chalk_1.default.gray
};
function buildReporterPlugin(config) {
    function printFileInfo(filePath, content, type) {
        const needCompression = type === 0 /* JS */ || type === 1 /* CSS */ || type === 3 /* HTML */;
        const compressed = needCompression
            ? `, brotli: ${(brotli_size_1.sync(typeof content === 'string' ? content : Buffer.from(content)) / 1024).toFixed(2)}kb`
            : ``;
        config.logger.info(`${chalk_1.default.gray(`[write]`)} ${writeColors[type](path_1.default.relative(process.cwd(), filePath))} ${(content.length / 1024).toFixed(2)}kb${compressed}`);
    }
    return {
        name: 'vite:size',
        writeBundle(_, output) {
            for (const file in output) {
                const chunk = output[file];
                if (chunk.type === 'chunk') {
                    printFileInfo(chunk.fileName, chunk.code, 0 /* JS */);
                    if (chunk.map) {
                        printFileInfo(chunk.fileName + '.map', chunk.map.toString(), 4 /* SOURCE_MAP */);
                    }
                }
                else if (chunk.source) {
                    printFileInfo(chunk.fileName, chunk.source, chunk.fileName.endsWith('.css') ? 1 /* CSS */ : 2 /* ASSET */);
                }
            }
        }
    };
}
exports.buildReporterPlugin = buildReporterPlugin;
//# sourceMappingURL=reporter.js.map