Skip to main content

Built and signed on GitHub Actions

vite 插件,用于压缩 png 格式的图片

This package works with Node.js, DenoIt is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
JSR Score
94%
Published
a month ago (1.0.3)
/** * @module * * 本模块是 vite 插件,使用 pngquant 工具对 png * 格式的图片进行压缩 * * @example * ``` * ``` */ import { exec } from "node:child_process"; import which from "npm:which@^4.0.0"; import type { Plugin } from "npm:vite@^5.3.3"; import { filter } from "./filter.ts"; export type CompressPngType = { pattern: string | string[]; }; const defaultOptions: CompressPngType = { pattern: "**/*.png", }; /** * 使用 pngquant 压缩 png 图片 */ export default function compressPng( options: CompressPngType = defaultOptions, ): Plugin { return { name: "vite-plugin-compress-png", async writeBundle(output) { const resolvedOrNull = await which("pngquant", { nothrow: true }); if (resolvedOrNull === null) { console.log("未找到图片压缩工具pngquant"); return; } const dir = output.dir; if (!dir) { return; } const compressed = await filter(dir, options.pattern); for (const imageFile of compressed) { exec(`pngquant --force --ext .png --quality 80 ${imageFile}`); } }, }; }