@ch-ui/vite-plugin-icons

中文文档
1.2.0发布于 2026/04/10Ch Public LicenseTypeScriptESM + CJS近 30 天下载 6204

Vite plugin for automating icon sprite bundling.

原始使用文档

@ch-ui/vite-plugin-icons

A Vite plugin for spriting icons used in your project in a Tailwind-like way.

Install & configure

pnpm add -D @ch-ui/vite-plugin-icons
import IconsPlugin from '@ch-ui/vite-plugin-icons';
// ...
export default defineConfig((env) => ({
  // The plugin will put the sprite in the project’s `publicDir`
  publicDir: resolve(__dirname, '../dist/assets'),
  // ...
  plugins: [
    // ...
    IconsPlugin({
      // Define the regular expression for the symbols you intend to use for your project; the expression must capture any strings you need to determine the path to the SVG asset.
      symbolPattern:
        'ph-icon--([a-z]+[a-z-]*)--(bold|duotone|fill|light|regular|thin)',
      // The matches in your regular expression are spread onto `assetPath` which must return the path to the SVG file specified by the symbol.
      assetPath: (name, variant) =>
        `./packages/icons/node_modules/@phosphor-icons/core/assets/${variant}/${name}${
          variant === 'regular' ? '' : `-${variant}`
        }.svg`,
      // Tell the plugin how to name the sprite.
      spriteFile: 'sprite.svg',
      // Tell the plugin in which files to look for icon symbols.
      contentPaths: ['**/*.stories.tsx'],
    }),
    // ...
  ],
}));
// ...

Usage example

The following example is provided for a React component, but this plugin will work for any text content you might happen to use.

import React, { ComponentPropsWithRef, forwardRef } from 'react';

const SPRITE = './assets/sprite.svg';

type IconName = string;
type IconVariant = 'bold' | 'duotone' | 'fill' | 'light' | 'regular' | 'thin';

type IconProps = ComponentPropsWithRef<'svg'> & {
  symbol: `ph-icon--${IconName}--${IconVariant}`;
};

const Icon = forwardRef<SVGSVGElement, IconProps>(
  ({ symbol, ...props }, forwardedRef) => {
    return (
      <svg viewBox="0 0 256 256" {...props} ref={forwardedRef}>
        <use href={`${SPRITE}#${symbol}`} />
      </svg>
    );
  },
);

export const ExampleIcons = () => {
  return (
    <>
      <style>{`
        svg{ inline-size: 4rem; block-size: 4rem; display: inline-block; margin:.25rem; color: blue }
      `}</style>
      <Icon symbol="ph-icon--address-book--regular" />
      <Icon symbol="ph-icon--planet--thin" />
      <Icon symbol="ph-icon--anchor--bold" />
      <Icon symbol="ph-icon--cards-three--light" />
      <Icon symbol="ph-icon--map-pin-simple-area--duotone" />
    </>
  );
};

版本与发布记录

@ch-ui/vite-plugin-icons 共发布 19 个版本,首次发布于 2024/07/09;过去 12 个月发布了 4 个版本。

版本发布日期距今
1.2.0latest2026/04/106 个月前
1.1.12026/03/127 个月前
1.1.02026/03/127 个月前
1.0.82026/02/069 个月前
1.0.72025/03/081.7 年前
1.0.62025/03/081.7 年前

关于 @ch-ui/vite-plugin-icons 的常见问题

@ch-ui/vite-plugin-icons 是什么?

Vite plugin for automating icon sprite bundling.

如何安装 @ch-ui/vite-plugin-icons?

在项目目录运行 npm install @ch-ui/vite-plugin-icons,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:vite(5.x || 6.x)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@ch-ui/vite-plugin-icons 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ch-ui/vite-plugin-icons 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@ch-ui/vite-plugin-icons 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@ch-ui/vite-plugin-icons 可以商用吗?

@ch-ui/vite-plugin-icons 使用 Ch Public License 许可。声明的许可为 Ch Public License,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@ch-ui/vite-plugin-icons 的下载量有多少?

根据 npm 官方下载统计,@ch-ui/vite-plugin-icons 在 2026/10/10 – 2026/11/08 这 30 天内被下载 6,204 次,日均约 207 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ch-ui/vite-plugin-icons 还在维护吗?

最新版本 1.2.0 发布于 2026/04/10(6 个月前);至今共发布 19 个版本,其中过去 12 个月发布了 4 个;首次发布于 2024/07/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。