iconfont-preview-cli 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +255 -0
- package/app/assets/index-BN1JFPmQ.js +22 -0
- package/app/assets/index-fas_yV9m.css +1 -0
- package/app/index.html +14 -0
- package/app/vite.svg +1 -0
- package/components/index.cjs +1 -0
- package/components/index.css +1 -0
- package/components/index.mjs +1233 -0
- package/components/types/index.d.ts +2 -0
- package/components/types/render-icon-list/index.d.ts +3 -0
- package/components/types/render-icon-list/src/components/render-icon-class.d.ts +23 -0
- package/components/types/render-icon-list/src/components/render-icon.d.ts +26 -0
- package/components/types/render-icon-list/src/render-icon-list.d.ts +33 -0
- package/components/types/render-icon-list/src/render-icon-list.vue.d.ts +61 -0
- package/package.json +66 -0
- package/server/bin.cjs +163 -0
- package/server/bin.mjs +150 -0
- package/server/index.cjs +141 -0
- package/server/index.d.cts +6 -0
- package/server/index.d.mts +6 -0
- package/server/index.d.ts +6 -0
- package/server/index.mjs +133 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 王志磊
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
# iconfont-preview-cli
|
|
2
|
+
|
|
3
|
+
一个用于预览本地字体图标的命令行工具。它可以扫描指定目录下的 CSS 文件,解析出图标类名,并提供一个可视化的 Web 界面来预览、搜索和复制图标代码。
|
|
4
|
+
|
|
5
|
+
## ✨ 功能特性
|
|
6
|
+
|
|
7
|
+
- 🔍 **自动扫描**: 递归扫描指定目录下的所有 CSS 文件。
|
|
8
|
+
- 🎨 **可视化预览**: 解析 CSS 中的 `content` 属性,展示图标及其类名。
|
|
9
|
+
- 📋 **一键复制**: 点击图标即可复制类名到剪贴板。
|
|
10
|
+
- ⚡ **即时搜索**: 支持通过类名关键词快速筛选图标。
|
|
11
|
+
- 🛠 **Vite 插件集成**: 提供 Vite 插件,方便在开发环境中直接集成图标预览服务。
|
|
12
|
+
|
|
13
|
+
## 🚀 快速开始
|
|
14
|
+
|
|
15
|
+
### 安装
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
# 全局安装
|
|
19
|
+
npm install -g iconfont-preview-cli
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
# 或者在项目中安装
|
|
23
|
+
npm install -D iconfont-preview-cli
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### 命令行使用 (CLI)
|
|
27
|
+
|
|
28
|
+
在项目根目录下,你可以通过以下命令启动预览服务:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
# 预览指定目录下的字体图标
|
|
32
|
+
iconfont-preview-cli --dir ./path/to/your/fonts
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
# 指定端口 (默认 3000)
|
|
36
|
+
iconfont-preview-cli --dir ./path/to/your/fonts --port 8080
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### 参数说明
|
|
40
|
+
|
|
41
|
+
| 参数 | 简写 | 描述 | 默认值 |
|
|
42
|
+
| -------- | ---- | -------------------------------------------- | ------ |
|
|
43
|
+
| `--dir` | `-d` | **(必填)** 包含字体图标 CSS 文件的文件夹路径 | - |
|
|
44
|
+
| `--port` | `-p` | 服务启动端口 | `3000` |
|
|
45
|
+
|
|
46
|
+
## 📦 安装与集成
|
|
47
|
+
|
|
48
|
+
### 作为 Vite 插件使用
|
|
49
|
+
|
|
50
|
+
如果你正在开发一个 Vite 项目,可以将此工具作为插件集成,在开发服务器中同时提供图标预览功能。
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
// vite.config.ts
|
|
54
|
+
import { iconfontServer } from "iconfont-preview-cli/server";
|
|
55
|
+
|
|
56
|
+
export default {
|
|
57
|
+
plugins: [
|
|
58
|
+
iconfontServer({
|
|
59
|
+
iconDir: "./src/assets/fonts", // 字体图标存放目录
|
|
60
|
+
urlPrefix: "/iconfont-proxy" // 代理路径前缀 (可选)
|
|
61
|
+
})
|
|
62
|
+
]
|
|
63
|
+
};
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### 组件
|
|
67
|
+
|
|
68
|
+
除了 CLI 工具,本项目还导出了核心组件 `RenderIconList`,方便你在自己的 Vue 项目中定制图标预览页面。
|
|
69
|
+
|
|
70
|
+
### 引入
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
import { RenderIconList } from "iconfont-preview-cli/components";
|
|
74
|
+
// 引入样式
|
|
75
|
+
import "iconfont-preview-cli/components/index.css";
|
|
76
|
+
import axios from "axios";
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### 基本用法
|
|
80
|
+
|
|
81
|
+
```vue
|
|
82
|
+
<template>
|
|
83
|
+
<div class="local-icon-demo">
|
|
84
|
+
<div class="local-icon-demo__header">
|
|
85
|
+
<el-input
|
|
86
|
+
v-model="keyword"
|
|
87
|
+
placeholder="输入关键字,回车搜索"
|
|
88
|
+
clearable
|
|
89
|
+
@change="onSearch"
|
|
90
|
+
/>
|
|
91
|
+
</div>
|
|
92
|
+
<RenderIconList
|
|
93
|
+
ref="renderIconListRef"
|
|
94
|
+
:get-icons-info="getIconsInfo"
|
|
95
|
+
:css-link-format="cssLinkFormat"
|
|
96
|
+
>
|
|
97
|
+
<template #default="{ iconsInfo, renderIcon }">
|
|
98
|
+
<div v-for="info in iconsInfo" :key="info.filePath">
|
|
99
|
+
<h3>{{ info.filePath }}</h3>
|
|
100
|
+
<ul class="local-icon-demo__icon-list">
|
|
101
|
+
<li
|
|
102
|
+
v-for="className in info.classNames"
|
|
103
|
+
:key="className"
|
|
104
|
+
class="local-icon-demo__icon-li"
|
|
105
|
+
>
|
|
106
|
+
<!-- 使用 renderIcon 组件渲染图标 -->
|
|
107
|
+
<component
|
|
108
|
+
:is="renderIcon"
|
|
109
|
+
:icon-class="className"
|
|
110
|
+
:iconInfo="info"
|
|
111
|
+
/>
|
|
112
|
+
</li>
|
|
113
|
+
</ul>
|
|
114
|
+
</div>
|
|
115
|
+
</template>
|
|
116
|
+
</RenderIconList>
|
|
117
|
+
</div>
|
|
118
|
+
</template>
|
|
119
|
+
|
|
120
|
+
<script setup lang="ts">
|
|
121
|
+
import { ref } from "vue";
|
|
122
|
+
import { RenderIconList } from "iconfont-preview-cli/components";
|
|
123
|
+
// 引入样式
|
|
124
|
+
import "iconfont-preview-cli/components/index.css";
|
|
125
|
+
import axios from "axios";
|
|
126
|
+
import type { RenderIconListInstance, IconInfo } from "iconfont-preview-cli/components";
|
|
127
|
+
|
|
128
|
+
/** 搜索关键字 */
|
|
129
|
+
const keyword = ref("");
|
|
130
|
+
const renderIconListRef = ref<RenderIconListInstance>();
|
|
131
|
+
|
|
132
|
+
// 获取图标数据的函数
|
|
133
|
+
const getIconsInfo = async (): Promise<IconInfo[]> => {
|
|
134
|
+
// 这里请求接口前缀替换为vite插件iconfontServer中urlPrefix的值
|
|
135
|
+
const res = await axios.get("/iconfont-proxy/api/iconsInfo");
|
|
136
|
+
const iconsInfo = res.data.data;
|
|
137
|
+
return iconsInfo;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
// 动态加载字体样式
|
|
141
|
+
const cssLinkFormat = (href: string) => {
|
|
142
|
+
// 这里根据需求替换为字体样式的路径
|
|
143
|
+
return href && `/@/assets/font-icon${href}`;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
// 调用搜索
|
|
147
|
+
const onSearch = (query: string) => {
|
|
148
|
+
renderIconListRef.value?.onSearch(query);
|
|
149
|
+
};
|
|
150
|
+
</script>
|
|
151
|
+
|
|
152
|
+
<style>
|
|
153
|
+
.local-icon-demo__header {
|
|
154
|
+
margin-bottom: 12px;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
.local-icon-demo__icon-list {
|
|
158
|
+
display: grid;
|
|
159
|
+
grid-template-columns: repeat(7, minmax(120px, 1fr));
|
|
160
|
+
list-style: none;
|
|
161
|
+
}
|
|
162
|
+
.local-icon-demo__icon-li {
|
|
163
|
+
height: 120px;
|
|
164
|
+
border-right: 1px solid getCssVar(border-color);
|
|
165
|
+
border-bottom: 1px solid getCssVar(border-color);
|
|
166
|
+
}
|
|
167
|
+
</style>
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### API
|
|
171
|
+
|
|
172
|
+
#### Props
|
|
173
|
+
|
|
174
|
+
| 属性名 | 类型 | 必填 | 描述 |
|
|
175
|
+
| --------------- | ----------------------------------------- | ---- | ------------------------------------------------- |
|
|
176
|
+
| `getIconsInfo` | `() => Promise<IconInfo[]> \| IconInfo[]` | 是 | 获取图标信息的函数,返回图标数据数组。 |
|
|
177
|
+
| `cssLinkFormat` | `(href: string) => string` | 否 | 格式化 CSS 文件链接的函数,用于动态加载字体样式。 |
|
|
178
|
+
|
|
179
|
+
#### Methods (Exposed)
|
|
180
|
+
|
|
181
|
+
| 方法名 | 参数 | 描述 |
|
|
182
|
+
| ---------- | ----------------- | ------------------------------------- |
|
|
183
|
+
| `onSearch` | `(query: string)` | 执行模糊搜索,过滤 `iconsInfo` 数据。 |
|
|
184
|
+
|
|
185
|
+
#### Slots
|
|
186
|
+
|
|
187
|
+
| 插槽名 | 参数 | 描述 |
|
|
188
|
+
| --------- | ------------------------------------------------------- | --------------------------------------------------------------- |
|
|
189
|
+
| `default` | `{ iconsInfo: InnerIconInfo[], renderIcon: Component }` | 自定义渲染列表内容。`renderIcon` 是一个用于渲染单个图标的组件。 |
|
|
190
|
+
|
|
191
|
+
### renderIcon API
|
|
192
|
+
|
|
193
|
+
#### Props
|
|
194
|
+
|
|
195
|
+
| 属性名 | 类型 | 必填 | 描述 |
|
|
196
|
+
| ------------- | --------------------------------------------- | ---- | ------------------ |
|
|
197
|
+
| `copyHandler` | `(iconName: string) => void \| Promise<void>` | 否 | 自定义图标点击复制 |
|
|
198
|
+
|
|
199
|
+
#### Type Definitions
|
|
200
|
+
|
|
201
|
+
```typescript
|
|
202
|
+
interface IconInfo {
|
|
203
|
+
/** CSS 文件相对路径 */
|
|
204
|
+
filePath: string;
|
|
205
|
+
/** 基础类名 (如 iconfont) */
|
|
206
|
+
baseClassName: string;
|
|
207
|
+
/** 图标类名列表 */
|
|
208
|
+
classNames: string[];
|
|
209
|
+
/** 自定义渲染图标的函数,返回一个组件 */
|
|
210
|
+
renderIcon?: (iconName: string) => Component;
|
|
211
|
+
}
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
## 🛠 本地开发
|
|
215
|
+
|
|
216
|
+
本项目采用 Monorepo 结构,使用 pnpm workspace 管理。
|
|
217
|
+
|
|
218
|
+
### 目录结构
|
|
219
|
+
|
|
220
|
+
- **`app/`**: 前端预览应用 (Vue 3 + Element Plus)
|
|
221
|
+
- **`server/`**: 服务端核心逻辑、CLI 实现及 Vite 插件 (Koa + Commander)
|
|
222
|
+
- **`font-icon/`**: 示例字体图标文件
|
|
223
|
+
|
|
224
|
+
### 开发命令
|
|
225
|
+
|
|
226
|
+
1. **安装依赖**
|
|
227
|
+
|
|
228
|
+
```bash
|
|
229
|
+
pnpm install
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
2. **启动开发环境**
|
|
233
|
+
|
|
234
|
+
同时启动服务端和前端应用的开发模式:
|
|
235
|
+
|
|
236
|
+
```bash
|
|
237
|
+
pnpm dev
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
- Server 运行在 `http://localhost:3000` (默认)
|
|
241
|
+
- App 运行在 Vite 开发服务器端口
|
|
242
|
+
|
|
243
|
+
3. **构建项目**
|
|
244
|
+
|
|
245
|
+
```bash
|
|
246
|
+
pnpm build
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
### FAQ
|
|
250
|
+
|
|
251
|
+
1. 报错:Error: Cannot find module '@rolldown/binding-darwin-universal'
|
|
252
|
+
|
|
253
|
+
```shell
|
|
254
|
+
pnpm i -f
|
|
255
|
+
```
|