UNPKG

esbuild-plugin-compress

Version:
111 lines (107 loc) 4.94 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { compress: () => compress, default: () => src_default }); module.exports = __toCommonJS(src_exports); // src/lib/esbuild-plugin-compress.ts var import_fs_extra = __toESM(require("fs-extra")); var import_path = __toESM(require("path")); var import_chalk = __toESM(require("chalk")); var import_micromatch = __toESM(require("micromatch")); var import_zlib = require("zlib"); var writeOriginFiles = /* @__PURE__ */ __name((path2, contents) => { import_fs_extra.default.writeFileSync(path2, contents); }, "writeOriginFiles"); var writeGzipCompress = /* @__PURE__ */ __name((path2, contents, options = {}) => { const gzipped = (0, import_zlib.gzipSync)(contents, options); import_fs_extra.default.writeFileSync(`${path2}.gz`, gzipped); }, "writeGzipCompress"); var writeBrotliCompress = /* @__PURE__ */ __name((path2, contents, options = {}) => { const gzipped = (0, import_zlib.brotliCompressSync)(contents, options); import_fs_extra.default.writeFileSync(`${path2}.br`, gzipped); }, "writeBrotliCompress"); var compress = /* @__PURE__ */ __name((options = {}) => { const { gzip = true, gzipOptions = {}, brotli = true, brotliOptions = {}, emitOrigin = true, exclude = [] } = options; const excludePatterns = Array.isArray(exclude) ? exclude : [ exclude ]; const noCompressSpecified = !gzip && !brotli; let outputDir = options.outputDir ?? null; return { name: "plugin:compress", setup({ initialOptions: { outfile, outdir, write }, onEnd }) { if (write === true) { console.log(import_chalk.default.yellow("WARN"), " Set write option as false to use compress plugin."); return; } if (noCompressSpecified) { console.log(import_chalk.default.yellow("WARN"), " Set at least one compression as true to use compress plugin."); } if (outputDir && !outdir && !outfile) { console.log(import_chalk.default.yellow("WARN"), " When using outputDir option, outdir or outfile must be specified."); } else if (outputDir && outfile) { outputDir = import_path.default.resolve(import_path.default.dirname(outfile), outputDir); } else if (outputDir && outdir) { outputDir = import_path.default.resolve(outdir, outputDir); } function handler(originPath, contents) { const writePath = outputDir ? import_path.default.resolve(outputDir, import_path.default.basename(originPath)) : originPath; if (!(contents == null ? void 0 : contents.length)) { return; } import_fs_extra.default.ensureDirSync(import_path.default.dirname(writePath)); gzip ? writeGzipCompress(writePath, contents, gzipOptions) : void 0; brotli ? writeBrotliCompress(writePath, contents, brotliOptions) : void 0; } __name(handler, "handler"); onEnd(async ({ outputFiles }) => { if ((outputFiles == null ? void 0 : outputFiles.length) === 1) { const { path: outputPath, contents } = outputFiles[0]; if (!import_micromatch.default.isMatch(outputPath, excludePatterns)) { handler(outputPath, contents); } emitOrigin && writeOriginFiles(outputPath, contents); } else { for (const { path: outputPath1, contents: contents1 } of outputFiles) { if (!import_micromatch.default.isMatch(outputPath1, excludePatterns)) { handler(outputPath1, contents1); } emitOrigin && writeOriginFiles(outputPath1, contents1); } } }); } }; }, "compress"); // src/index.ts var src_default = compress; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { compress });