npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vite-plugin-vsharp

v1.8.1

Published

A Vite plugin that compresses static images after each builds by using sharp.js

Downloads

36,104

Readme

VSharp: Compress Images for the Web with Vite and Sharp

VSharp is a powerful Vite plugin that utilizes the formidable Sharp library to compress and optimize static images during your build process. This helps to significantly reduce image sizes, enhancing your website or application's load time and overall performance. With VSharp, maintaining high-quality images at smaller file sizes becomes an effortless part of your development workflow.

Features

  • Supported image formats: .jpg/.jpeg, .png, .gif, .webp.
  • Integrates image compression features from Sharp's API, including sharp().jpeg(), sharp().png(), sharp().gif(), and sharp().webp() (Sharp documentation).

Getting Started

Installation

To install the plugin, run the following command:

npm install vite-plugin-vsharp --save-dev

Configuration

Add VSharp to your Vite configuration file:

// vite.config.js
import vsharp from 'vite-plugin-vsharp';

export default {
  plugins: [
    vsharp({
      // Plugin options go here
    }),
  ],
};

Plugin Options

Customize the behavior of VSharp with the following options:

  1. exclude: Specify which image files to skip during image bundling. Simple names without path prefixes are required.

    // vite.config.js
    {
      // ...
      plugins: [
        vsharp({
          exclude: [
            "bg.jpg", // Includes "bg.jpg"
            // Do not add path prefixes or hashes
          ],
        }),
      ]
    }
  2. excludePublic: Exclude images from the public directory using glob patterns. Prefixes relative to your project's root are necessary.

    // vite.config.js
    {
      // ...
      plugins: [
        vsharp({
          excludePublic: [
            "public/test_img/*", // Exclude all images in public/test_img
          ],
        }),
      ]
    }
  3. includePublic: Specifically include images from an excluded directory, overriding the excludePublic option.

    // vite.config.js
    {
      // ...
      plugins: [
        vsharp({
          excludePublic: [
            "public/test_img/*"
          ],
          includePublic: [
            "public/test_img/001.jpg", // Include this particular image
          ],
        }),
      ]
    }
  4. Resize options: Configure dimensions or scaling to resize images.

    // vite.config.js
    {
      // ...
      plugins: [
        vsharp({
          width: 800, // Max width, images with a smaller width than this will not be resized
          height: 800, // Max height, images with a smaller height than this will not be resized
          scale: 0.8, // Overrides width and height
        }),
      ]
    }
  5. Preserve Metadata: Maintain image metadata such as orientation.

    // vite.config.js
    {
      // ...
      plugins: [
        vsharp({
          preserveMetadata: {
            orientation: true, // Preserves image orientation
          },
        }),
      ]
    }

Default Settings

The plugin provides sensible defaults, which can be overridden by specifying your own settings in the plugin options:

{
  "includePublic": [],
  "excludePublic": [],
  "exclude": [],
  ".jpg": {
    "quality": 80
  },
  ".jpeg": {
    "quality": 80
  },
  ".png": {
    "quality": 80,
    "palette": true
  },
  ".webp": {
    "lossless": true
  },
  ".gif": {
    
  },
  "preserveMetadata": {
    "orientation": false
  }
}

For additional Sharp function parameters, refer to the official Sharp documentation.