Socket
Book a DemoInstallSign in
Socket

vite-plugin-compression2

Package Overview
Dependencies
Maintainers
1
Versions
47
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vite-plugin-compression2

a fast vite compression plugin

2.0.1
Source
npmnpm
Version published
Maintainers
1
Created
Source

codecov

Install

$ yarn add vite-plugin-compression2 -D

# or

$ npm install vite-plugin-compression2 -D

Usage

Basic Usage

import { defineConfig } from 'vite'
import { compression } from 'vite-plugin-compression2'

export default defineConfig({
  plugins: [
    // ...your plugins
    compression()
  ]
})

Multiple Algorithms

import { compression, defineAlgorithm } from 'vite-plugin-compression2'

export default defineConfig({
  plugins: [
    compression({
      algorithms: [
        'gzip',
        'brotliCompress',
        defineAlgorithm('deflate', { level: 9 })
      ]
    })
  ]
})

Custom Algorithm Function

import { compression, defineAlgorithm } from 'vite-plugin-compression2'

export default defineConfig({
  plugins: [
    compression({
      algorithms: [
        defineAlgorithm(
          async (buffer, options) => {
            // Your custom compression logic
            return compressedBuffer
          },
          { customOption: true }
        )
      ]
    })
  ]
})

With Tarball

import { compression, tarball } from 'vite-plugin-compression2'

export default defineConfig({
  plugins: [
    compression(),
    // If you want to create a tarball archive, use tarball plugin after compression
    tarball({ dest: './dist/archive' })
  ]
})

Options

Compression Plugin Options

paramstypedefaultdescription
includestring | RegExp | Array<string | RegExp>/\.(html|xml|css|json|js|mjs|svg|yaml|yml|toml)$/Include all assets matching any of these conditions.
excludestring | RegExp | Array<string | RegExp>-Exclude all assets matching any of these conditions.
thresholdnumber0Only assets bigger than this size are processed (in bytes)
algorithmsAlgorithms['gzip', 'brotliCompress']Array of compression algorithms or defineAlgorithm results
filenamestring | function[path][base].gz or [path][base].brThe target asset filename pattern
deleteOriginalAssetsbooleanfalseWhether to delete the original assets or not
skipIfLargerOrEqualbooleantrueWhether to skip the compression if the result is larger than or equal to the original file

Tarball Plugin Options

paramstypedefaultdescription
deststring-Destination directory for tarball

API

defineAlgorithm(algorithm, options?)

Define a compression algorithm with options.

Parameters:

  • algorithm: Algorithm name ('gzip' | 'brotliCompress' | 'deflate' | 'deflateRaw') or custom function
  • options: Compression options for the algorithm

Returns: [algorithm, options] tuple

Examples:

// Built-in algorithm with default options
defineAlgorithm('gzip')

// Built-in algorithm with custom options
defineAlgorithm('gzip', { level: 9 })

// Brotli with custom quality
defineAlgorithm('brotliCompress', {
  params: {
    [require('zlib').constants.BROTLI_PARAM_QUALITY]: 11
  }
})

// Custom algorithm function
defineAlgorithm(
  async (buffer, options) => {
    // Your compression implementation
    return compressedBuffer
  },
  { customOption: 'value' }
)

Supported Algorithms

  • gzip: Standard gzip compression
  • brotliCompress: Brotli compression (better compression ratio)
  • deflate: Deflate compression
  • deflateRaw: Raw deflate compression
  • Custom Function: Your own compression algorithm

Algorithm Types

The algorithms option accepts:

type Algorithms =
  | Algorithm[] // ['gzip', 'brotliCompress']
  | DefineAlgorithmResult[] // [defineAlgorithm('gzip'), ...]
  | (Algorithm | DefineAlgorithmResult)[] // Mixed array

Migration

If you're upgrading from v1.x, please check the Migration Guide.

Q & A

FAQ

Examples

Basic Gzip Only

compression({
  algorithms: ['gzip']
})

Multiple Algorithms with Custom Options

compression({
  algorithms: [
    defineAlgorithm('gzip', { level: 9 }),
    defineAlgorithm('brotliCompress', {
      params: {
        [require('zlib').constants.BROTLI_PARAM_QUALITY]: 11
      }
    })
  ]
})

Custom Filename Pattern

compression({
  algorithms: ['gzip'],
  filename: '[path][base].[hash].gz'
})

Delete Original Files

compression({
  algorithms: ['gzip'],
  deleteOriginalAssets: true
})

Size Threshold

compression({
  algorithms: ['gzip'],
  threshold: 1000 // Only compress files larger than 1KB
})

Others

  • If you want to analyze your bundle assets, try vite-bundle-analyzer
  • tarball option dest means to generate a tarball somewhere
  • tarball is based on the ustar format. It should be compatible with all popular tar distributions (gnutar, bsdtar etc)

Sponsors

LICENSE

MIT

Acknowledgements

NWYLZW

Author

Kanno

Keywords

vite

FAQs

Package last updated on 05 Jun 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.