Code Monkey home page Code Monkey logo

vite-plugin-squoosh's Introduction

vite-plugin-squoosh Build npm

a build plugin for Vite that uses the squoosh library to compress images. It offers persistent caching and parallel processing, and supports a range of image formats. This can help improve performance and reduce image size in web applications.

Install

  • Node: version 16.x
  • Vite: version >=2.x
  • vite-plugin-squoosh: see on NPM

Usage

Using NPM:

npm install --save-dev vite-plugin-squoosh

Using yarn:

yarn add -D vite-plugin-squoosh

Vite configuration in vite.config.ts

import squooshPlugin from 'vite-plugin-squoosh';

export default () => ({
    plugins: [squooshPlugin({
        // Specify codec options.
        codecs: {
            mozjpeg: { quality: 30, smoothing: 1 },
            webp: { quality: 25 },
            avif: { cqLevel: 20, sharpness: 1 },
            jxl: { quality: 30 },
            wp2: { quality: 40 },
            oxipng: { level: 3 }
        },
        // Do not encode .wp2 and .webp files.
        exclude: /.(wp2|webp)$/,
        // Encode png to webp.
        encodeTo: [{ from: /.png$/, to: "webp" }]
    })]
})

Plugin options

The squooshPlugin function takes an options object as its only argument. The possible properties of this options object are:

Option Description
cacheLevel A string that determines how aggressively to use caching to speed up processing. This can be "None", "PerSession", or "Persistent".
cachePath A string that specifies the path to the cache file on the filesystem.
codecs An object that specifies the codecs to use for encoding the images.
coreCount An optional integer that specifies the number of CPU cores to use for processing the images.
exclude An array of glob patterns that specify which files to exclude from processing.
includeDirs An array of strings or objects that specify additional directories to search for images to process.
silent A boolean that specifies whether to suppress logging output from the plugin.

Build locally

Pnpm (version 7.x) is a requirement.

npm install -g pnpm

Next, run 'install' with pnpm. This will install all the right dependencies for the workspaces.

pnpm install

(Optional)

To see debug logs, set the DEBUG environment variable to vite-plugin-squoosh.

set DEBUG=vite-plugin-squoosh

Run examples

This will run all the examples in the packages/examples/ directory.

pnpm run build:ex

vite-plugin-squoosh's People

Contributors

bituq avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

vite-plugin-squoosh's Issues

Also support imagequant codec

Thanks for the nice plugin! ๐Ÿ‘๐Ÿป

We use a lot of png's in our projects and I need to reduce the number of colors to compress them even further. Squoosh supports imagequant to support this (see https://github.com/GoogleChromeLabs/squoosh/tree/dev/codecs/imagequant).

Your code is setup so nicely generic, that I could not find a good starting point for supporting two optimisations:

  1. Reducing the number of colors in the png using imagequant (numColors: number, dither: number)
  2. Compress the smaller png using oxipng

Is this something that you're willing to support?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.