vectify 2.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.
Files changed (50) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +679 -0
  3. package/README.zh-CN.md +683 -0
  4. package/dist/chunk-4BWKFV7W.mjs +1311 -0
  5. package/dist/chunk-CIKTK6HI.mjs +96 -0
  6. package/dist/cli.d.mts +1 -0
  7. package/dist/cli.d.ts +1 -0
  8. package/dist/cli.js +1483 -0
  9. package/dist/cli.mjs +56 -0
  10. package/dist/helpers-UPZEBRGK.mjs +26 -0
  11. package/dist/index.d.mts +281 -0
  12. package/dist/index.d.ts +281 -0
  13. package/dist/index.js +1463 -0
  14. package/dist/index.mjs +27 -0
  15. package/dist/templates/angular/component.ts.hbs +121 -0
  16. package/dist/templates/angular/createIcon.ts.hbs +116 -0
  17. package/dist/templates/astro/component.astro.hbs +110 -0
  18. package/dist/templates/astro/createIcon.astro.hbs +111 -0
  19. package/dist/templates/lit/component.js.hbs +12 -0
  20. package/dist/templates/lit/component.ts.hbs +19 -0
  21. package/dist/templates/lit/createIcon.js.hbs +98 -0
  22. package/dist/templates/lit/createIcon.ts.hbs +99 -0
  23. package/dist/templates/preact/component.jsx.hbs +8 -0
  24. package/dist/templates/preact/component.tsx.hbs +11 -0
  25. package/dist/templates/preact/createIcon.jsx.hbs +101 -0
  26. package/dist/templates/preact/createIcon.tsx.hbs +121 -0
  27. package/dist/templates/qwik/component.jsx.hbs +7 -0
  28. package/dist/templates/qwik/component.tsx.hbs +8 -0
  29. package/dist/templates/qwik/createIcon.jsx.hbs +100 -0
  30. package/dist/templates/qwik/createIcon.tsx.hbs +111 -0
  31. package/dist/templates/react/component.jsx.hbs +8 -0
  32. package/dist/templates/react/component.tsx.hbs +11 -0
  33. package/dist/templates/react/createIcon.jsx.hbs +100 -0
  34. package/dist/templates/react/createIcon.tsx.hbs +117 -0
  35. package/dist/templates/solid/component.tsx.hbs +10 -0
  36. package/dist/templates/solid/createIcon.jsx.hbs +127 -0
  37. package/dist/templates/solid/createIcon.tsx.hbs +139 -0
  38. package/dist/templates/svelte/component.js.svelte.hbs +9 -0
  39. package/dist/templates/svelte/component.ts.svelte.hbs +10 -0
  40. package/dist/templates/svelte/icon.js.svelte.hbs +123 -0
  41. package/dist/templates/svelte/icon.ts.svelte.hbs +124 -0
  42. package/dist/templates/template-engine.ts +107 -0
  43. package/dist/templates/vanilla/component.ts.hbs +8 -0
  44. package/dist/templates/vanilla/createIcon.js.hbs +111 -0
  45. package/dist/templates/vanilla/createIcon.ts.hbs +124 -0
  46. package/dist/templates/vue/component.js.vue.hbs +21 -0
  47. package/dist/templates/vue/component.ts.vue.hbs +22 -0
  48. package/dist/templates/vue/icon.js.vue.hbs +155 -0
  49. package/dist/templates/vue/icon.ts.vue.hbs +157 -0
  50. package/package.json +78 -0
@@ -0,0 +1,683 @@
1
+ <div align="center">
2
+
3
+ # Vectify
4
+
5
+ 一个强大的命令行工具,可将 SVG 文件转换为特定框架的图标组件。将你的 SVG 图标转换为完全类型化、可自定义的组件,支持 React、Vue、Svelte 等多个框架。
6
+
7
+ [English](./README.md) | 简体中文
8
+
9
+ [![npm version](https://img.shields.io/npm/v/vectify.svg)](https://www.npmjs.com/package/vectify)
10
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
11
+
12
+ </div>
13
+
14
+ ## 📑 目录
15
+
16
+ - [特性](#特性)
17
+ - [安装](#安装)
18
+ - [快速开始](#快速开始)
19
+ - [CLI 命令](#cli-命令)
20
+ - [配置选项](#配置选项)
21
+ - [基础配置](#基础配置)
22
+ - [生成选项](#生成选项)
23
+ - [监听模式](#监听模式)
24
+ - [SVGO 配置](#svgo-配置)
25
+ - [生命周期钩子](#生命周期钩子)
26
+ - [组件属性](#组件属性)
27
+ - [框架特定说明](#框架特定说明)
28
+ - [最佳实践](#最佳实践)
29
+ - [高级用法](#高级用法)
30
+ - [故障排除](#故障排除)
31
+ - [迁移指南](#迁移指南)
32
+ - [贡献](#贡献)
33
+ - [许可证](#许可证)
34
+
35
+ ## ✨ 特性
36
+
37
+ - **支持 10+ 框架** - React、Vue、Svelte、Solid.js、Preact、Angular、Lit、Qwik、Astro 和 Vanilla JS
38
+ - **TypeScript 优先** - 完整的 TypeScript 支持,自动生成类型定义
39
+ - **自动优化** - 内置 SVGO 进行 SVG 优化
40
+ - **灵活定制** - 运行时控制大小、颜色、描边宽度等
41
+ - **监听模式** - SVG 文件变化时自动重新生成组件
42
+ - **生命周期钩子** - 使用 beforeParse、afterGenerate 和 onComplete 钩子自定义生成过程
43
+ - **预览生成** - 生成所有图标的交互式 HTML 预览
44
+ - **智能清理** - SVG 文件删除时自动移除孤立组件
45
+ - **批量处理** - 高效处理多个 SVG 文件
46
+
47
+ ## 📦 安装
48
+
49
+ ```bash
50
+ # 使用 npm
51
+ npm install -D vectify
52
+
53
+ # 使用 pnpm
54
+ pnpm add -D vectify
55
+
56
+ # 使用 yarn
57
+ yarn add -D vectify
58
+ ```
59
+
60
+ ## 🚀 快速开始
61
+
62
+ ### 1. 初始化配置
63
+
64
+ ```bash
65
+ npx vectify init
66
+ ```
67
+
68
+ 这将在项目根目录创建 `vectify.config.ts` 文件。选择目标框架并配置路径:
69
+
70
+ ```typescript
71
+ import { defineConfig } from 'vectify'
72
+
73
+ export default defineConfig({
74
+ framework: 'react',
75
+ input: './icons',
76
+ output: './src/icons',
77
+ typescript: true,
78
+ optimize: true,
79
+ generateOptions: {
80
+ index: true,
81
+ types: true,
82
+ preview: true,
83
+ },
84
+ })
85
+ ```
86
+
87
+ ### 2. 添加 SVG 文件
88
+
89
+ 将 SVG 文件放入配置的输入目录(默认:`./icons`):
90
+
91
+ ```
92
+ icons/
93
+ ├── arrow-right.svg
94
+ ├── user.svg
95
+ └── settings.svg
96
+ ```
97
+
98
+ ### 3. 生成组件
99
+
100
+ ```bash
101
+ npx vectify generate
102
+ ```
103
+
104
+ 这将在输出目录生成图标组件:
105
+
106
+ ```
107
+ src/icons/
108
+ ├── ArrowRight.tsx
109
+ ├── User.tsx
110
+ ├── Settings.tsx
111
+ ├── index.ts
112
+ └── Icon.tsx
113
+ ```
114
+
115
+ ### 4. 在代码中使用
116
+
117
+ ```tsx
118
+ import { ArrowRight, User, Settings } from './icons'
119
+
120
+ function App() {
121
+ return (
122
+ <div>
123
+ <ArrowRight size={24} color="blue" />
124
+ <User size={32} color="#333" strokeWidth={1.5} />
125
+ <Settings className="icon" />
126
+ </div>
127
+ )
128
+ }
129
+ ```
130
+
131
+ ## 💻 CLI 命令
132
+
133
+ ### `vectify init`
134
+
135
+ 初始化新的配置文件。
136
+
137
+ ```bash
138
+ npx vectify init [选项]
139
+
140
+ 选项:
141
+ -f, --force 覆盖已存在的配置文件
142
+ -o, --output 配置文件输出路径 (默认: vectify.config.ts)
143
+ ```
144
+
145
+ ### `vectify generate`
146
+
147
+ 从 SVG 文件生成图标组件。
148
+
149
+ ```bash
150
+ npx vectify generate [选项]
151
+
152
+ 选项:
153
+ -c, --config 配置文件路径 (默认: vectify.config.ts)
154
+ --dry-run 预览将要生成的内容而不实际写入文件
155
+ ```
156
+
157
+ ### `vectify watch`
158
+
159
+ 监听变化并自动重新生成。
160
+
161
+ ```bash
162
+ npx vectify watch [选项]
163
+
164
+ 选项:
165
+ -c, --config 配置文件路径 (默认: vectify.config.ts)
166
+ ```
167
+
168
+ ## ⚙️ 配置选项
169
+
170
+ ### 完整配置参数说明
171
+
172
+ 所有 `defineConfig()` 可用的配置选项如下表所示。
173
+
174
+ #### 核心配置
175
+
176
+ | 参数 | 类型 | 默认值 | 必填 | 说明 | 示例 |
177
+ |------|------|--------|------|------|------|
178
+ | `framework` | `'react'` \| `'vue'` \| `'svelte'` \| `'solid'` \| `'preact'` \| `'lit'` \| `'angular'` \| `'qwik'` \| `'astro'` \| `'vanilla'` | - | ✅ | 目标框架,决定组件模板、导出风格和文件扩展名 | `framework: 'react'` |
179
+ | `input` | `string` | `'./icons'` | ✅ | 包含源 SVG 文件的目录,可以是相对或绝对路径 | `input: './assets/icons'` |
180
+ | `output` | `string` | `'./src/icons'` | ✅ | 生成组件的输出目录 | `output: './components/icons'` |
181
+ | `configDir` | `string` | `'.'` | ❌ | 配置文件相对于项目根目录的路径。当配置文件位于子目录时,用于正确解析 input/output 路径 | `configDir: '../..'` |
182
+ | `typescript` | `boolean` | `true` | ❌ | 生成带完整类型定义的 TypeScript 组件。设为 `false` 则输出 JavaScript | `typescript: true` |
183
+ | `optimize` | `boolean` | `true` | ❌ | 使用 SVGO 优化 SVG。减小文件大小并清理不必要的属性 | `optimize: true` |
184
+ | `keepColors` | `boolean` | `false` | ❌ | 保留 SVG 文件中的原始颜色。为 `false` 时,使用 `currentColor` 以便自定义单色图标。为 `true` 时,保留原始 fill/stroke 颜色用于多色图标 | `keepColors: false` |
185
+ | `prefix` | `string` | `''` | ❌ | 添加到所有组件名称前的前缀。用于命名空间 | `prefix: 'Icon'` → `IconArrowRight` |
186
+ | `suffix` | `string` | `''` | ❌ | 添加到所有组件名称后的后缀 | `suffix: 'Icon'` → `ArrowRightIcon` |
187
+ | `transform` | `(name: string) => string` | - | ❌ | 自定义函数,将 SVG 文件名转换为组件名。覆盖默认的 PascalCase 转换和 prefix/suffix | `transform: (n) => 'X' + n` |
188
+
189
+ #### `generateOptions` 对象
190
+
191
+ | 参数 | 类型 | 默认值 | 说明 | 示例 |
192
+ |------|------|--------|------|------|
193
+ | `index` | `boolean` | `true` | 生成包含聚合导出的索引文件。导出风格(默认 vs 具名)由框架自动决定 | `index: true` |
194
+ | `types` | `boolean` | `true` | 生成 TypeScript 声明文件 (.d.ts)。仅在 `typescript: true` 时生效 | `types: true` |
195
+ | `preview` | `boolean` | `false` | 生成交互式 `preview.html` 用于本地浏览所有图标。适合设计审查 | `preview: true` |
196
+ | `cleanOutput` | `boolean` | `false` | 移除不再有对应 SVG 文件的孤立组件。帮助保持输出目录整洁 | `cleanOutput: true` |
197
+
198
+ #### `watch` 对象
199
+
200
+ | 参数 | 类型 | 默认值 | 说明 | 示例 |
201
+ |------|------|--------|------|------|
202
+ | `enabled` | `boolean` | `false` | 启用监听模式。在输入目录中添加、修改或删除 SVG 文件时自动重新生成组件 | `enabled: true` |
203
+ | `ignore` | `string[]` | - | 监听时忽略的 glob 模式数组 | `ignore: ['**/node_modules/**', '**/.git/**']` |
204
+ | `debounce` | `number` | `300` | 触发重新生成前的防抖延迟(毫秒)。防止过度重新构建 | `debounce: 500` |
205
+
206
+ #### `svgoConfig` 对象
207
+
208
+ 自定义 SVG 优化行为。直接传递给 SVGO。
209
+
210
+ | 参数 | 类型 | 默认值 | 说明 | 示例 |
211
+ |------|------|--------|------|------|
212
+ | `plugins` | `any[]` | - | SVGO 插件名称和配置数组。可用插件见 [SVGO 文档](https://github.com/svg/svgo) | `plugins: ['preset-default', 'removeViewBox']` |
213
+ | `multipass` | `boolean` | - | 启用多次优化以获得更好效果 | `multipass: true` |
214
+ | `...` | `any` | - | 任何其他 SVGO 支持的配置选项 | - |
215
+
216
+ #### `hooks` 对象
217
+
218
+ 用于自定义生成过程的生命周期钩子。
219
+
220
+ | 钩子 | 签名 | 调用时机 | 参数 | 返回值 | 常见用途 |
221
+ |------|------|----------|------|--------|----------|
222
+ | `beforeParse` | `(svg: string, fileName: string) => Promise<string> \| string` | 读取 SVG 文件后、解析前 | `svg`: 原始 SVG 内容<br>`fileName`: SVG 文件名 | 修改后的 SVG 内容 | 预处理 SVG、替换颜色/属性、规范化 viewBox |
223
+ | `afterGenerate` | `(code: string, iconName: string) => Promise<string> \| string` | 组件代码生成后、写入文件前 | `code`: 生成的组件源码<br>`iconName`: 组件名 | 修改后的组件代码 | 添加注释、注入导出、自定义代码风格 |
224
+ | `onComplete` | `(stats: GenerationStats) => Promise<void> \| void` | 所有图标生成完成后 | `stats`: 生成统计信息(见下) | `void` | 记录统计、运行后处理脚本、发送通知 |
225
+
226
+ #### `GenerationStats` 类型
227
+
228
+ 传递给 `onComplete` 钩子的统计对象。
229
+
230
+ | 属性 | 类型 | 说明 |
231
+ |------|------|------|
232
+ | `success` | `number` | 成功生成的图标数量 |
233
+ | `failed` | `number` | 失败的生成数量 |
234
+ | `total` | `number` | 处理的 SVG 文件总数 |
235
+ | `errors` | `Array<{ file: string; error: string }>` | 失败生成的详细错误信息 |
236
+
237
+ ### 配置示例
238
+
239
+ #### 基础配置
240
+
241
+ ```typescript
242
+ import { defineConfig } from 'vectify'
243
+
244
+ export default defineConfig({
245
+ framework: 'react',
246
+ input: './icons',
247
+ output: './src/icons',
248
+ })
249
+ ```
250
+
251
+ #### 多色图标与自定义 SVGO
252
+
253
+ ```typescript
254
+ export default defineConfig({
255
+ framework: 'vue',
256
+ input: './icons',
257
+ output: './src/icons',
258
+ keepColors: true, // 保留 SVG 原始颜色
259
+ svgoConfig: {
260
+ plugins: [
261
+ 'preset-default',
262
+ {
263
+ name: 'removeAttrs',
264
+ params: { attrs: '(width|height)' },
265
+ },
266
+ ],
267
+ },
268
+ })
269
+ ```
270
+
271
+ #### Monorepo 配置
272
+
273
+ ```typescript
274
+ // packages/web/vectify.config.ts
275
+ export default defineConfig({
276
+ framework: 'svelte',
277
+ configDir: '../..', // 指向 monorepo 根目录
278
+ input: '../../icons', // 共享图标目录
279
+ output: './src/icons', // 包特定输出
280
+ })
281
+ ```
282
+
283
+ #### 带钩子的高级配置
284
+
285
+ ```typescript
286
+ export default defineConfig({
287
+ framework: 'react',
288
+ input: './icons',
289
+ output: './src/icons',
290
+ hooks: {
291
+ beforeParse: (svg, fileName) => {
292
+ // 将黑色替换为 currentColor 以便自定义
293
+ return svg.replace(/#000000/gi, 'currentColor')
294
+ },
295
+ afterGenerate: (code, iconName) => {
296
+ // 为每个组件添加 JSDoc 注释
297
+ return `/**\n * ${iconName} 图标组件\n * @generated by Vectify\n */\n${code}`
298
+ },
299
+ onComplete: (stats) => {
300
+ console.log(`✔ 生成 ${stats.success}/${stats.total} 个图标`)
301
+ if (stats.failed > 0) {
302
+ console.error(`✖ 失败: ${stats.failed}`)
303
+ stats.errors.forEach(({ file, error }) => {
304
+ console.error(` ${file}: ${error}`)
305
+ })
306
+ }
307
+ },
308
+ },
309
+ })
310
+ ```
311
+
312
+ ### 框架特定说明
313
+
314
+ **导出风格:**
315
+ - **默认导出:** Vue、Svelte、Preact
316
+ - **具名导出:** React、Solid、Qwik、Angular、Astro、Vanilla JS、Lit
317
+
318
+ 索引文件会自动为你选择的框架使用正确的导出风格。
319
+
320
+ **命名策略:**
321
+ - 默认情况下,文件名会转换为 PascalCase(如 `arrow-right.svg` → `ArrowRight`)
322
+ - 在转换后应用 `prefix` 和 `suffix`
323
+ - 使用 `transform` 函数完全自定义命名
324
+
325
+ **颜色策略:**
326
+ - `keepColors: false` - 适合应继承文本颜色的单色图标。使用 `currentColor` 并允许通过 `color` 属性进行运行时自定义。
327
+ - `keepColors: true` - 适合多色品牌图标。保留原始 SVG 的 fill/stroke 颜色。
328
+
329
+
330
+ ## 🎨 组件属性
331
+
332
+ 所有生成的组件都接受以下属性:
333
+
334
+ ```typescript
335
+ interface IconProps {
336
+ // 图标大小 (默认: 24)
337
+ size?: number | string
338
+
339
+ // 图标颜色 (默认: 'currentColor')
340
+ color?: string
341
+
342
+ // 描边图标的描边宽度 (默认: 2)
343
+ strokeWidth?: number | string
344
+
345
+ // CSS 类名
346
+ className?: string
347
+
348
+ // 无障碍:图标标题
349
+ title?: string
350
+
351
+ // 无障碍:aria-label
352
+ 'aria-label'?: string
353
+
354
+ // 无障碍:aria-hidden
355
+ 'aria-hidden'?: boolean
356
+
357
+ // 所有其他 SVG 属性
358
+ [key: string]: any
359
+ }
360
+ ```
361
+
362
+ ### 使用示例
363
+
364
+ ```tsx
365
+ // 基础用法
366
+ <IconName />
367
+
368
+ // 自定义大小和颜色
369
+ <IconName size={32} color="#3b82f6" />
370
+
371
+ // 带描边宽度
372
+ <IconName size={24} strokeWidth={1.5} />
373
+
374
+ // 带 CSS 类
375
+ <IconName className="my-icon" />
376
+
377
+ // 带无障碍属性
378
+ <IconName
379
+ title="用户资料"
380
+ aria-label="用户资料图标"
381
+ />
382
+
383
+ // 对屏幕阅读器隐藏
384
+ <IconName aria-hidden={true} />
385
+
386
+ // 带自定义 SVG 属性
387
+ <IconName
388
+ size={28}
389
+ color="red"
390
+ style={{ transform: 'rotate(45deg)' }}
391
+ onClick={() => console.log('clicked')}
392
+ />
393
+ ```
394
+
395
+ ## 🔧 框架特定说明
396
+
397
+ ### React / Preact
398
+
399
+ ```tsx
400
+ import { ArrowRight } from './icons'
401
+
402
+ function Component() {
403
+ return <ArrowRight size={24} color="blue" />
404
+ }
405
+ ```
406
+
407
+ ### Vue 3
408
+
409
+ ```vue
410
+ <script setup>
411
+ import { ArrowRight } from './icons'
412
+ </script>
413
+
414
+ <template>
415
+ <ArrowRight :size="24" color="blue" />
416
+ </template>
417
+ ```
418
+
419
+ ### Svelte
420
+
421
+ ```svelte
422
+ <script>
423
+ import { ArrowRight } from './icons'
424
+ </script>
425
+
426
+ <ArrowRight size={24} color="blue" />
427
+ ```
428
+
429
+ ### Solid.js
430
+
431
+ ```tsx
432
+ import { ArrowRight } from './icons'
433
+
434
+ function Component() {
435
+ return <ArrowRight size={24} color="blue" />
436
+ }
437
+ ```
438
+
439
+ ### Angular
440
+
441
+ ```typescript
442
+ import { ArrowRight } from './icons'
443
+
444
+ @Component({
445
+ selector: 'app-root',
446
+ template: '<ng-container *ngComponentOutlet="ArrowRight; inputs: { size: 24, color: \'blue\' }"></ng-container>',
447
+ })
448
+ export class AppComponent {
449
+ ArrowRight = ArrowRight
450
+ }
451
+ ```
452
+
453
+ ### Vanilla JS
454
+
455
+ ```javascript
456
+ import { ArrowRight } from './icons'
457
+
458
+ const icon = ArrowRight({ size: 24, color: 'blue' })
459
+ document.getElementById('app').appendChild(icon)
460
+ ```
461
+
462
+ ## 📋 最佳实践
463
+
464
+ ### 1. 一致的 SVG 准备
465
+
466
+ - 使用一致的 viewBox(最好是 24x24)
467
+ - 移除不必要的属性(width、height、fill、stroke)
468
+ - 简化路径和形状
469
+ - 使用有意义的文件名
470
+
471
+ ### 2. 命名约定
472
+
473
+ ```
474
+ ✓ 推荐:
475
+ arrow-right.svg → ArrowRight
476
+ user-profile.svg → UserProfile
477
+ settings-gear.svg → SettingsGear
478
+
479
+ ✗ 避免:
480
+ arrow_right.svg
481
+ UserProfile.svg
482
+ settings gear.svg
483
+ ```
484
+
485
+ ### 3. 颜色管理
486
+
487
+ ```typescript
488
+ // 单色图标(默认)
489
+ keepColors: false // 使用 currentColor,可通过 color 属性自定义
490
+
491
+ // 多色图标
492
+ keepColors: true // 保留 SVG 中的原始颜色
493
+ ```
494
+
495
+ ### 4. 项目结构
496
+
497
+ ```
498
+ project/
499
+ ├── icons/ # 源 SVG 文件
500
+ │ ├── arrow-right.svg
501
+ │ └── user.svg
502
+ ├── src/
503
+ │ └── icons/ # 生成的组件(添加到 gitignore)
504
+ │ ├── ArrowRight.tsx
505
+ │ ├── User.tsx
506
+ │ └── index.ts
507
+ └── vectify.config.ts # 配置文件
508
+ ```
509
+
510
+ ### 5. Git 集成
511
+
512
+ 将生成的文件添加到 `.gitignore`:
513
+
514
+ ```gitignore
515
+ # 生成的图标
516
+ src/icons/
517
+
518
+ # 保留配置
519
+ !vectify.config.ts
520
+ ```
521
+
522
+ 添加到 `package.json`:
523
+
524
+ ```json
525
+ {
526
+ "scripts": {
527
+ "icons": "vectify generate",
528
+ "icons:watch": "vectify watch",
529
+ "postinstall": "vectify generate"
530
+ }
531
+ }
532
+ ```
533
+
534
+ ## 🔥 高级用法
535
+
536
+ ### 多配置设置
537
+
538
+ 为多个框架生成图标:
539
+
540
+ ```typescript
541
+ // vectify.react.config.ts
542
+ export default defineConfig({
543
+ framework: 'react',
544
+ output: './packages/react/src/icons',
545
+ })
546
+
547
+ // vectify.vue.config.ts
548
+ export default defineConfig({
549
+ framework: 'vue',
550
+ output: './packages/vue/src/icons',
551
+ })
552
+ ```
553
+
554
+ ```bash
555
+ npx vectify generate -c vectify.react.config.ts
556
+ npx vectify generate -c vectify.vue.config.ts
557
+ ```
558
+
559
+ ### Monorepo 设置
560
+
561
+ ```typescript
562
+ // apps/web/vectify.config.ts
563
+ export default defineConfig({
564
+ configDir: '../..', // 相对于项目根目录
565
+ input: '../../icons',
566
+ output: './src/icons',
567
+ })
568
+ ```
569
+
570
+ ### 自定义转换
571
+
572
+ ```typescript
573
+ export default defineConfig({
574
+ hooks: {
575
+ beforeParse: async (svg, fileName) => {
576
+ // 替换颜色
577
+ svg = svg.replace(/#000000/g, 'currentColor')
578
+
579
+ // 添加自定义属性
580
+ svg = svg.replace('<svg', '<svg data-icon="true"')
581
+
582
+ return svg
583
+ },
584
+
585
+ afterGenerate: async (code, iconName) => {
586
+ // 添加自定义导出
587
+ code += `\nexport const ${iconName}Name = '${iconName}'\n`
588
+
589
+ // 添加 JSDoc 注释
590
+ code = `/**\n * ${iconName} 图标组件\n */\n${code}`
591
+
592
+ return code
593
+ },
594
+ },
595
+ })
596
+ ```
597
+
598
+ ## 🔍 故障排除
599
+
600
+ ### 图标未生成
601
+
602
+ 1. 检查输入目录中是否存在 SVG 文件
603
+ 2. 验证配置文件路径是否正确
604
+ 3. 确保 SVG 文件是有效的 XML
605
+ 4. 检查文件权限
606
+
607
+ ### TypeScript 错误
608
+
609
+ 1. 确保配置中 `typescript: true`
610
+ 2. 使用 `npx vectify generate` 重新生成
611
+ 3. 检查生成的文件未被错误地添加到 gitignore
612
+
613
+ ### 颜色不生效
614
+
615
+ 1. 对于可自定义颜色:使用 `keepColors: false`
616
+ 2. 对于保留原始颜色:使用 `keepColors: true`
617
+ 3. 从源 SVG 中移除 fill/stroke 属性以获得更好的自定义效果
618
+
619
+ ### 构建错误
620
+
621
+ 1. 确保生成的文件包含在构建中
622
+ 2. 检查输出目录是否在 tsconfig.json 的 include 路径中
623
+ 3. 验证是否安装了框架特定的依赖
624
+
625
+ ## 🔄 迁移指南
626
+
627
+ ### 从 react-icons 迁移
628
+
629
+ ```tsx
630
+ // 之前
631
+ import { FiArrowRight } from 'react-icons/fi'
632
+ <FiArrowRight size={24} />
633
+
634
+ // 之后
635
+ import { ArrowRight } from './icons'
636
+ <ArrowRight size={24} />
637
+ ```
638
+
639
+ ### 从 @iconify 迁移
640
+
641
+ ```tsx
642
+ // 之前
643
+ import { Icon } from '@iconify/react'
644
+ <Icon icon="mdi:arrow-right" />
645
+
646
+ // 之后
647
+ import { ArrowRight } from './icons'
648
+ <ArrowRight />
649
+ ```
650
+
651
+ ## 🤝 贡献
652
+
653
+ 欢迎贡献!请先阅读我们的[贡献指南](CONTRIBUTING.md)。
654
+
655
+ 1. Fork 本仓库
656
+ 2. 创建特性分支 (`git checkout -b feature/amazing-feature`)
657
+ 3. 提交更改 (`git commit -m 'feat: add amazing feature'`)
658
+ 4. 推送到分支 (`git push origin feature/amazing-feature`)
659
+ 5. 开启 Pull Request
660
+
661
+ ## 📄 许可证
662
+
663
+ MIT © [Xiaobing Zhu](https://github.com/hixb)
664
+
665
+ ## 🙏 致谢
666
+
667
+ - [SVGO](https://github.com/svg/svgo) - SVG 优化
668
+ - [Handlebars](https://handlebarsjs.com/) - 模板引擎
669
+ - 所有框架社区的启发
670
+
671
+ ## 🔗 链接
672
+
673
+ - [GitHub 仓库](https://github.com/hixb/vectify)
674
+ - [问题追踪](https://github.com/hixb/vectify/issues)
675
+ - [更新日志](CHANGELOG.md)
676
+
677
+ ---
678
+
679
+ <div align="center">
680
+
681
+ 由 [Xiaobing Zhu](https://github.com/hixb) 用 ❤️ 制作
682
+
683
+ </div>