UNPKG

webpack-bundle-analyzer

Version:

Webpack plugin and CLI utility that represents bundle content as convenient interactive zoomable treemap

33 lines (30 loc) 1.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getCompressedSize = getCompressedSize; exports.isZstdSupported = void 0; var _nodeZlib = _interopRequireDefault(require("node:zlib")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } const isZstdSupported = exports.isZstdSupported = "createZstdCompress" in _nodeZlib.default; /** @typedef {"gzip" | "brotli" | "zstd"} Algorithm */ /** * @param {Algorithm} algorithm compression algorithm * @param {string} input input * @returns {number} compressed size */ function getCompressedSize(algorithm, input) { if (algorithm === "gzip") { return _nodeZlib.default.gzipSync(input, { level: 9 }).length; } if (algorithm === "brotli") { return _nodeZlib.default.brotliCompressSync(input).length; } if (algorithm === "zstd" && isZstdSupported) { // eslint-disable-next-line n/no-unsupported-features/node-builtins return _nodeZlib.default.zstdCompressSync(input).length; } throw new Error(`Unsupported compression algorithm: ${algorithm}.`); }