lunx.docs
DocsPluginsplugin-compress

@lunx/plugin-compress

Ultra-fast Brotli and Gzip compression using parallelized native Rust threads. Reduces transfer size by up to 85% with zero runtime overhead on your application.

Installation

bash
npm install --save-dev @lunx/plugin-compress
typescriptlunx.config.ts
import { defineConfig } from 'lunx'
import compress from '@lunx/plugin-compress'

export default defineConfig({
  framework: 'react',
  plugins: [
    compress({
      algorithm:  'brotli',   // 'brotli' | 'gzip' | 'both'
      threshold:  1024,       // min file size to compress (bytes)
      quality:    11,         // brotli quality 0-11 (11 = max compression)
      include:    [/.(js|css|html|svg|json|wasm)$/],
    }),
  ],
})

How it works

After Rollup writes all output files to dist/, the plugin spawns a Rust thread pool matching your CPU core count and compresses all eligible files in parallel. The compressed files are placed alongside originals with .br and .gz extensions, ready for your server to serve with Content-Encoding negotiation.

Build output example

bash
lunx build

📦 Bundling complete
  dist/assets/main-4f2c1a.js        94.2 kB
  dist/assets/style-8d3f20.css      12.4 kB

🗜  Compressing (brotli, quality=11)...
  dist/assets/main-4f2c1a.js.br     16.8 kB  (−82%)
  dist/assets/style-8d3f20.css.br    2.1 kB  (−83%)
  dist/index.html.br                 0.3 kB  (−73%)

 Compression complete (324ms, 8 cores)

Serving compressed files

Configure your web server to serve .br files when the client sends Accept-Encoding: br. Here are examples for common servers:

Nginx

nginxnginx.conf
# Enable Brotli (requires ngx_brotli module)
brotli on;
brotli_static on;   # serve pre-compressed .br files directly
brotli_types text/html text/css application/javascript application/json;

# Enable Gzip fallback
gzip on;
gzip_static on;
gzip_types text/html text/css application/javascript;

Caddy

textCaddyfile
example.com {
  root * /var/www/dist
  encode br gzip   # Caddy serves pre-compressed automatically
  file_server
}

Vercel / Netlify

bash
# Both platforms automatically detect and serve .br and .gz files
# No additional configuration needed — just deploy dist/

Compression algorithm comparison

AlgorithmRatio (JS)Browser supportSpeed (compress)
Brotli~83%All modern browsers (97%+)Slower (run at build time)
Gzip~70%Universal (100%)Fast
Deflate~65%Universal (100%)Very fast
Zstd~85%Chrome 123+, Firefox 126+Very fast

Plugin options

FieldTypeDefaultDescription
algorithmstring'brotli'Compression algorithm: 'brotli' | 'gzip' | 'both'. Use 'both' for maximum compatibility.
thresholdnumber1024Minimum file size in bytes to compress. Files smaller than this are skipped.
qualitynumber11Brotli quality level (0–11). Higher = smaller files but slower compression.
levelnumber9Gzip compression level (1–9). Higher = smaller files but slower.
includeRegExp[]File patterns to compress. Default: JS, CSS, HTML, SVG, JSON, WASM.
excludeRegExp[]File patterns to exclude from compression.
deleteOriginalAssetsbooleanfalseDelete uncompressed originals after compression. Only use if your server exclusively serves pre-compressed files.
Use 'both' for maximum compatibility
Set algorithm: 'both' to generate both .br and .gz variants. Your server will negotiate the best encoding per client — Brotli for modern browsers, Gzip for legacy. The disk overhead is minimal compared to the bandwidth savings.