esbuild-plugin-compress
Version:
ESBuild plugin for output compression
81 lines (78 loc) • 3.31 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
// src/lib/esbuild-plugin-compress.ts
import fs from "fs-extra";
import path from "path";
import chalk from "chalk";
import micromatch from "micromatch";
import { gzipSync, brotliCompressSync } from "zlib";
var writeOriginFiles = /* @__PURE__ */ __name((path2, contents) => {
fs.writeFileSync(path2, contents);
}, "writeOriginFiles");
var writeGzipCompress = /* @__PURE__ */ __name((path2, contents, options = {}) => {
const gzipped = gzipSync(contents, options);
fs.writeFileSync(`${path2}.gz`, gzipped);
}, "writeGzipCompress");
var writeBrotliCompress = /* @__PURE__ */ __name((path2, contents, options = {}) => {
const gzipped = brotliCompressSync(contents, options);
fs.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(chalk.yellow("WARN"), " Set write option as false to use compress plugin.");
return;
}
if (noCompressSpecified) {
console.log(chalk.yellow("WARN"), " Set at least one compression as true to use compress plugin.");
}
if (outputDir && !outdir && !outfile) {
console.log(chalk.yellow("WARN"), " When using outputDir option, outdir or outfile must be specified.");
} else if (outputDir && outfile) {
outputDir = path.resolve(path.dirname(outfile), outputDir);
} else if (outputDir && outdir) {
outputDir = path.resolve(outdir, outputDir);
}
function handler(originPath, contents) {
const writePath = outputDir ? path.resolve(outputDir, path.basename(originPath)) : originPath;
if (!(contents == null ? void 0 : contents.length)) {
return;
}
fs.ensureDirSync(path.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 (!micromatch.isMatch(outputPath, excludePatterns)) {
handler(outputPath, contents);
}
emitOrigin && writeOriginFiles(outputPath, contents);
} else {
for (const { path: outputPath1, contents: contents1 } of outputFiles) {
if (!micromatch.isMatch(outputPath1, excludePatterns)) {
handler(outputPath1, contents1);
}
emitOrigin && writeOriginFiles(outputPath1, contents1);
}
}
});
}
};
}, "compress");
// src/index.ts
var src_default = compress;
export {
compress,
src_default as default
};