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-merge-images

v0.0.1

Published

[![NPM version](https://img.shields.io/npm/v/vite-plugin-merge-images.svg?style=flat)](https://npmjs.com/package/vite-plugin-merge-images) [![NPM downloads](http://img.shields.io/npm/dm/vite-plugin-merge-images.svg?style=flat)](https://npmjs.com/package/v

Readme

NPM version NPM downloads

vite-plugin-merge-images

将多个图片自动合并为一张雪碧图(Sprite),并生成对应的样式与坐标数据,便于在 Vite 项目中高效使用精灵图技术。

✨ 特性

  • 自动扫描指定目录下的 PNG 图片,合并为一张精灵图
  • 同时生成坐标 JSON 与样式对象,方便按需加载
  • 提供 getImageStyle 工具函数用于快速定位 sprite 中的图片
  • 生成虚拟模块 virtual:merge-images-runtime,可在项目中直接使用

📦 安装

pnpm add -D vite-plugin-merge-images
# 或者
npm install -D vite-plugin-merge-images

🛠️ 使用方式

1. 配置插件

vite.config.ts 中引入并使用插件:

import { defineConfig } from "vite";
import vitePluginSpritesmith from "vite-plugin-merge-images";

export default defineConfig({
  plugins: [
    vitePluginSpritesmith({
      src: "./src/icons", // 图片来源目录
      output: "./public/sprites", // 精灵图和坐标输出路径
      spriteName: "sprite.png", // 精灵图文件名
      coordsName: "sprite.json", // 坐标 JSON 文件名
      glob: "**/*.png", // 匹配图片的 glob 模式
    }),
  ],
});

2. 在项目中引入精灵图资源

你可以在任何地方直接引入虚拟模块:

import {
  sprite,
  style,
  coords,
  getImageStyle,
} from "virtual:merge-images-runtime";

console.log(sprite); // => "/public/sprites/sprite.png"
console.log(style["icon-name"]); // => 样式对象
console.log(coords["icon-name"]); // => 精确坐标信息

// 在组件中使用 getImageStyle
const myStyle = getImageStyle(coords, "icon-name", 0.5); // 缩放为 0.5 倍

3. 在组件中使用

export function Icon({ name }: { name: string }) {
  const style = getImageStyle(coords, name, 1); // 或传 scale 缩放比例
  return <img src={sprite} style={style} alt={name} />;
}

📁 输出结构

生成后的输出目录如下:

public/sprites/
  ├── sprite.png          // 合并后的精灵图
  └── sprite.json         // 坐标数据

🧩 虚拟模块导出内容

export const sprite: string; // 精灵图路径(vite 资源路径)
export const style: SpriteStyle; // 所有图片的样式对象
export const coords: SpriteCoords; // 所有图片的精确坐标
export const getImageStyle: (coords, name, scale) => React.CSSProperties;

📌 类型定义

export interface SpritePluginOptions {
  src?: string;
  output?: string;
  spriteName?: string;
  coordsName?: string;
  glob?: string;
}

export interface SpriteCoord {
  x: number;
  y: number;
  width: number;
  height: number;
}

export interface SpriteCoords {
  [key: string]: SpriteCoord;
}

export interface SpriteStyle {
  [key: string]: {
    objectFit: "none";
    objectPosition: string;
    width: string;
    height: string;
    display: string;
  };
}

🧪 TODO

  • [ ] 支持 JPG、SVG 等图片格式
  • [ ] 支持多套 sprite 输出

🤝 感谢

本插件基于 spritesmith 构建。

LICENSE

MIT