Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

vite-plugin-image-presets

ElMassimo5.4kMIT0.3.4TypeScript support: included

Image Presets for Vite.js apps. Optimize, resize, and process images consistently and with ease.

plugin, image, sharp, image-processing, build, vite, vite-plugin, vitejs

readme


<samp>vite-plugin-image-presets</samp>

Image Presets for Vite.js apps


This Vite plugin allows you to define presets for image processing using Sharp, allowing you to optimize, resize, and process images consistently and with ease.

Demo 🖼

Installation 💿

npm install -D vite-plugin-image-presets # pnpm, yarn

Configuration ⚙️

Add it to your plugins in vite.config.ts

import { defineConfig } from 'vite'
import imagePresets, { widthPreset } from 'vite-plugin-image-presets'

export default defineConfig({
  plugins: [
    imagePresets({
      thumbnail: widthPreset({
        class: 'img thumb',
        loading: 'lazy',
        widths: [48, 96],
        formats: {
          webp: { quality: 50 },
          jpg: { quality: 70 },
        },
      }),
    }),
  ],
})

Usage 🚀

Use the preset query parameter to obtain an array of source and img attrs:

import thumbnails from '~/images/logo.jpg?preset=thumbnail'

expect(thumbnails).toEqual([
  {
    type: 'image/webp',
    srcset: '/assets/logo.ffc730c4.webp 48w, /assets/logo.1f874174.webp 96w',
  },
  {
    type: 'image/jpeg',
    srcset: '/assets/logo.063759b1.jpeg 48w, /assets/logo.81d93491.jpeg 96w',
    src: '/assets/logo.81d93491.jpeg',
    class: 'img thumb',
    loading: 'lazy',
  },
])

You can also use the src and srcset query parameters for direct usage:

import srcset from '~/images/logo.jpg?preset=thumbnail&srcset'
import src from '~/images/logo.jpg?preset=thumbnail&src'

expect(srcset).toEqual('/assets/logo.063759b1.jpeg 48w, /assets/logo.81d93491.jpeg 96w')
expect(src).toEqual('/assets/logo.81d93491.jpeg')

Check the example for additional usage information and different preset examples, or see it live.

Documentation 📖

Additional usage documentation coming soon.

In the meantime, check the @islands/images module for îles.

Acknowledgements

  • sharp: High performance Node.js image processing
  • vite-imagetools: The middleware used in development is based on this nice library.

The hdPreset is based on the following article by Jake Archibald:

License

This library is available as open source under the terms of the MIT License.

changelog

0.3.4 (2023-01-23)

Features

0.3.3 (2022-10-27)

Bug Fixes

0.3.2 (2022-07-21)

Bug Fixes

Features

  • loosen deps to allow more recent versions of dependencies (3e3e94a)

0.3.1 (2022-01-16)

Bug Fixes

  • ensure src is deterministic, always take the last (6579611)
  • pass additional parameters as args to the generate fn (053da5d)

0.3.0 (2022-01-14)

Bug Fixes

  • handle unexpected errors without crashing the vite server (e174936)
  • remove undefined and null attrs (e3cf8e7)

Features

  • add src query param support and allow srcset to specify an index (53396c6)
  • add srcset param for direct usage (close #2) (7660af4)

0.2.0 (2022-01-12)

Bug Fixes

  • use the .avif file extension (5c35a2e)

Features

  • add hdPreset based on the Jake Archibald article (a02129a)
  • add densityPreset that uses density condition descriptors (9aa98a1)
  • add loading="lazy" by default to all presets (24bf381)
  • allow async generate functions to support reading metadata (ad48252)
  • api: add writeImages and purgeCache (1c831d3)
  • clean cached images that are no longer used (d0879b3)
  • strongly typed options for format options (7db1e03)
  • support 'original' format and widths in presets (822ffb1)

0.1.1 (2022-01-11)

0.1.0

Initial Version, with widthPreset.