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 🙏

© 2026 – Pkg Stats / Ryan Hefner

vite-plugin-image-compress-l

v1.0.0

Published

简单的 Vite 图片压缩插件

Downloads

13

Readme

vite-plugin-image-compress-l

使用 sharp 库实现的 Vite 图片压缩插件,支持在构建时自动压缩项目中的图片资源。

特性

  • 支持多种图片格式:JPEG, PNG, WebP, AVIF, GIF, TIFF
  • 可配置压缩质量
  • 支持图片尺寸限制
  • 构建时自动压缩
  • 输出压缩统计信息

安装

npm install vite-plugin-image-compress-l sharp --save-dev
# 或
pnpm add vite-plugin-image-compress-l sharp -D
# 或
yarn add vite-plugin-image-compress-l sharp -D

使用方法

基础用法

// vite.config.ts
import { defineConfig } from 'vite';
import imageCompress from 'vite-plugin-image-compress';

export default defineConfig({
  plugins: [
    imageCompress()
  ]
});

配置选项

import imageCompress from 'vite-plugin-image-compress';

imageCompress({
  // JPEG 压缩质量 (1-100),默认 80
  jpegQuality: 80,

  // PNG 压缩级别 (0-9),默认 6
  pngCompressionLevel: 6,

  // WebP 压缩质量 (1-100),默认 80
  webpQuality: 80,

  // AVIF 压缩质量 (1-100),默认 50
  avifQuality: 50,

  // GIF 压缩质量,默认 100
  gifQuality: 100,

  // 最大宽度,超过则等比缩放
  maxWidth: 1920,

  // 最大高度,超过则等比缩放
  maxHeight: 1080,

  // 是否禁用压缩,默认 false
  disable: false,

  // 要排除的图片路径(支持 glob 模式)
  exclude: ['**/icon*.png', '**/logo.*'],

  // 是否在控制台输出压缩信息,默认 true
  verbose: true
})

在 Vue 项目中使用

// vite.config.ts
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import imageCompress from 'vite-plugin-image-compress';

export default defineConfig({
  plugins: [
    vue(),
    imageCompress({
      jpegQuality: 75,
      pngCompressionLevel: 9,
      webpQuality: 75,
      verbose: true
    })
  ]
});

在 React 项目中使用

// vite.config.ts
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import imageCompress from 'vite-plugin-image-compress';

export default defineConfig({
  plugins: [
    react(),
    imageCompress({
      jpegQuality: 80,
      maxWidth: 2048,
      exclude: ['**/static/**']
    })
  ]
});

构建输出示例

[vite-plugin-image-compress] 压缩: assets/logo.png
  256.32 KB --> 128.15 KB  50.00%

========== 图片压缩统计 ==========
共压缩 15 张图片
原始总大小: 2048.56 KB
压缩后总大小: 1024.28 KB
总体压缩率: 50.00%
===================================

注意事项

  1. sharp 依赖: 插件依赖 sharp 库,需要在项目中安装
  2. 构建时运行: 插件只在 vite build 时生效,开发模式下不会压缩
  3. 只压缩更小的文件: 插件只会替换比原始文件更小的压缩结果
  4. 图片格式: 不会改变图片格式,只进行压缩处理

与其他插件配合

// vite.config.ts
import { defineConfig } from 'vite';
import imageCompress from 'vite-plugin-image-compress';
import visualizer from 'rollup-plugin-visualizer';

export default defineConfig({
  plugins: [
    imageCompress(),
    // 其他插件...
  ]
});

许可证

MIT