Skip to content

A Universal Compress Plugin for Webpack / Vite / Rollup.

Notifications You must be signed in to change notification settings

rogerleung0411/unplugin-compress

Repository files navigation

unplugin-compress

NPM version

A universal compress plugin for Webpack / Vite / Rollup, powered by Unplugin, generating compressed file using gzip / brotli / etc...

Basic Usage

See more usage example in folder /examples. Feel free to modify and play it on your own!

Webpack

// webpack.config.js

const compress = require('unplugin-compress/webpack');

module.exports = ({
  mode: 'production',
  entry: './src/index.js',
  output: {
    path: distPath,
  },
  plugins: [
    compress({
      filter: /\.js$/,
    }),
    compress({
      filter: /main\.js$/,
      algorithm: 'brotliCompress',
      extname: '.br',
    }),
    // etc...
  ]
});

Vite

// in vite.config.ts

import { defineConfig } from 'vite';
import compress from 'unplugin-compress/vite';

export default defineConfig({
  plugins: [
    compress({
      filter: /\.js$/,
    }),
    compress({
      filter: /\.(js|css)$/,
      algorithm: 'brotliCompress',
      extname: '.br',
    }),
  ]
});

Rollup

// in rollup.config.js
import compress from 'unplugin-compress/rollup';

export default {
  // ...
  plugins: [
    compress({
      filter: /\.js$/,
    })
  ],
  // ...
};

Options

Key Type Default Description
verbose boolean true Whether to output the compressed result in the console
filter RegExp | (filename: string) => boolean () => false Distinguish whether a chunk/asset should be compressed or not. If filter is undefined, nothing will be compressed.
algorithm Algorithm gzip Possible values are gzip, brotliCompress, deflate and deflateRaw
extname string .gz The extension name of output file
threshold number 0 Only assets bigger than this size are processed (in bytes)

Inspiration

Todo

  • Vite support.
  • Webpack(both 4 and 5) support.
  • Rollup support.

About

A Universal Compress Plugin for Webpack / Vite / Rollup.

Resources

Stars

Watchers

Forks

Packages

No packages published