An esbuild plugin to compress output using gzip, brotli, and deflate. Based on Node's zlib API.
npm install @liber-ufpe/esbuild-plugin-compress --save-dev
Important
metafile: true
option is required to generate the compressed files. See more about metafile in esbuild docs.
import esbuild from "esbuild";
import compressPlugin from "@liber-ufpe/esbuild-plugin-compress";
const compress = compressPlugin();
esbuild.build({
entryPoints: ["src/index.js"],
bundle: true,
metafile: true,
outfile: "dist/index.js",
plugins: [compress],
}).catch(() => process.exit(1));
Or when customizing the compression options:
import esbuild from "esbuild";
import compressPlugin from "@liber-ufpe/esbuild-plugin-compress";
const compress = compressPlugin({
gzip: true,
brotli: false,
gzipOptions: { level: constants.Z_BEST_SPEED },
deflateOptions: { level: constants.Z_DEFAULT_COMPRESSION },
excludes: ["**/*.{webp,avif,jpg,png}"]
});
esbuild.build({
entryPoints: ["src/index.js"],
bundle: true,
metafile: true,
outfile: "dist/index.js",
plugins: [compress],
}).catch(() => process.exit(1));
gzip
: Enable gzip compression.- type:
boolean
- default:
true
- type:
brotli
: Enable brotli compression.- type:
boolean
- default:
true
- type:
deflate
: Enable deflate compression.- type:
boolean
- default:
true
- type:
excludes
: glob patterns to exclude files from compression.- type:
string[]
- default:
[]
- type:
gzipOptions
:ZlibOptions
for gzip compression.- type:
ZlibOptions
- default:
{level: constants.Z_BEST_COMPRESSION}
- type:
deflateOptions
:ZlibOptions
for deflate compression.- type:
ZlibOptions
- default:
{level: constants.Z_BEST_COMPRESSION}
- type:
brotliOptions
:BrotliOptions
for brotli compression.- type:
BrotliOptions
- default:
{}
- type: