vite-svg-sprite-generator-plugin 1.1.6 → 1.3.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/README.md +183 -335
- package/package.json +14 -8
- package/vite-svg-sprite-generator-plugin.d.ts +66 -87
- package/vite-svg-sprite-generator-plugin.js +443 -85
- package/vite-svg-sprite-generator-plugin.ts +681 -81
- package/CHANGELOG.md +0 -332
package/README.md
CHANGED
|
@@ -1,55 +1,34 @@
|
|
|
1
|
-
# 🎨 Vite SVG Sprite Generator
|
|
1
|
+
# 🎨 Vite SVG Sprite Generator Plugin
|
|
2
2
|
|
|
3
|
-
> Production-ready Vite plugin for automatic SVG sprite generation with HMR
|
|
3
|
+
> Production-ready Vite plugin for automatic SVG sprite generation with HMR, tree-shaking, and SVGO optimization
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/vite-svg-sprite-generator-plugin)
|
|
6
6
|
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
[](https://vitejs.dev/)
|
|
7
8
|
|
|
8
|
-
##
|
|
9
|
+
## Features
|
|
9
10
|
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
-
|
|
14
|
-
-
|
|
15
|
-
-
|
|
16
|
-
-
|
|
17
|
-
-
|
|
18
|
-
- 🌳 **Tree-Shakeable** - ES modules with proper exports
|
|
19
|
-
- 🎨 **Vite Standard Compliance** - Fully complies with Vite plugin API and ecosystem standards
|
|
20
|
-
- 🔄 **Uses Vite Utilities** - Leverages `vite.normalizePath` for consistent cross-platform path handling
|
|
11
|
+
- **2-3x faster builds** - Parallel SVG processing (v1.3.0+)
|
|
12
|
+
- **Tree-shaking** - Remove unused icons (up to 84% reduction)
|
|
13
|
+
- **HMR** - Instant icon updates without page reload
|
|
14
|
+
- **SVGO optimization** - 40-60% smaller sprites
|
|
15
|
+
- **Security** - XSS & path traversal protection
|
|
16
|
+
- **Zero config** - Works out of the box
|
|
17
|
+
- **Framework agnostic** - React, Vue, Svelte, any Vite project
|
|
18
|
+
- **Multi-page support** - Works with [vite-multi-page-html-generator-plugin](https://www.npmjs.com/package/vite-multi-page-html-generator-plugin)
|
|
21
19
|
|
|
22
|
-
##
|
|
23
|
-
|
|
24
|
-
### Basic (without SVGO optimization)
|
|
20
|
+
## Installation
|
|
25
21
|
|
|
26
22
|
```bash
|
|
27
23
|
npm install -D vite-svg-sprite-generator-plugin
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
### Recommended (with SVGO optimization)
|
|
31
|
-
|
|
32
|
-
```bash
|
|
33
|
-
npm install -D vite-svg-sprite-generator-plugin svgo
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
**Note:** SVGO is optional! The plugin works without it, but you'll get 40-60% smaller sprites with SVGO installed.
|
|
37
|
-
|
|
38
|
-
<details>
|
|
39
|
-
<summary>Other package managers</summary>
|
|
40
24
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
yarn add -D vite-svg-sprite-generator-plugin svgo
|
|
44
|
-
|
|
45
|
-
# PNPM
|
|
46
|
-
pnpm add -D vite-svg-sprite-generator-plugin svgo
|
|
25
|
+
# Optional: SVGO for optimization (recommended)
|
|
26
|
+
npm install -D svgo
|
|
47
27
|
```
|
|
48
|
-
</details>
|
|
49
28
|
|
|
50
|
-
##
|
|
29
|
+
## Quick Start
|
|
51
30
|
|
|
52
|
-
### 1.
|
|
31
|
+
### 1. Configure Vite
|
|
53
32
|
|
|
54
33
|
```javascript
|
|
55
34
|
// vite.config.js
|
|
@@ -57,11 +36,7 @@ import { defineConfig } from 'vite';
|
|
|
57
36
|
import svgSpritePlugin from 'vite-svg-sprite-generator-plugin';
|
|
58
37
|
|
|
59
38
|
export default defineConfig({
|
|
60
|
-
plugins: [
|
|
61
|
-
svgSpritePlugin({
|
|
62
|
-
iconsFolder: 'src/icons'
|
|
63
|
-
})
|
|
64
|
-
]
|
|
39
|
+
plugins: [svgSpritePlugin()]
|
|
65
40
|
});
|
|
66
41
|
```
|
|
67
42
|
|
|
@@ -72,19 +47,17 @@ src/
|
|
|
72
47
|
icons/
|
|
73
48
|
home.svg
|
|
74
49
|
user.svg
|
|
75
|
-
|
|
50
|
+
search.svg
|
|
76
51
|
```
|
|
77
52
|
|
|
78
|
-
### 3. Use
|
|
53
|
+
### 3. Use Icons
|
|
79
54
|
|
|
80
55
|
```html
|
|
81
56
|
<svg class="icon">
|
|
82
|
-
<use href="#home"
|
|
57
|
+
<use href="#home" />
|
|
83
58
|
</svg>
|
|
84
59
|
```
|
|
85
60
|
|
|
86
|
-
### 4. Use in CSS
|
|
87
|
-
|
|
88
61
|
```css
|
|
89
62
|
.icon {
|
|
90
63
|
width: 24px;
|
|
@@ -93,372 +66,247 @@ src/
|
|
|
93
66
|
}
|
|
94
67
|
```
|
|
95
68
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
## 🎨 How It Works
|
|
99
|
-
|
|
100
|
-
The plugin automatically **injects the sprite directly into your HTML** as an inline SVG element.
|
|
101
|
-
|
|
102
|
-
✅ **No separate file generated** - Sprite is embedded in the page DOM
|
|
103
|
-
✅ **No external requests** - Everything works in a single HTTP request
|
|
104
|
-
✅ **Automatic injection** - Sprite appears in HTML automatically
|
|
105
|
-
✅ **Fast rendering** - Icons display immediately, no loading delay
|
|
106
|
-
|
|
107
|
-
### Where is the sprite?
|
|
108
|
-
|
|
109
|
-
Look for this in your HTML:
|
|
110
|
-
|
|
111
|
-
```html
|
|
112
|
-
<svg id="icon-sprite" class="svg-sprite" style="display: none;">
|
|
113
|
-
<symbol id="home" viewBox="0 0 24 24">...</symbol>
|
|
114
|
-
<symbol id="user" viewBox="0 0 24 24">...</symbol>
|
|
115
|
-
</svg>
|
|
116
|
-
```
|
|
117
|
-
|
|
118
|
-
The sprite is **injected at the start of your HTML** (just after `<body>` tag).
|
|
119
|
-
|
|
120
|
-
## 🎨 Vite Compliance
|
|
121
|
-
|
|
122
|
-
This plugin is built with maximum compliance to Vite standards and best practices:
|
|
123
|
-
|
|
124
|
-
- ✅ **Official Vite Plugin API** - Implements all required hooks (`buildStart`, `buildEnd`, `configureServer`, `handleHotUpdate`)
|
|
125
|
-
- ✅ **Uses Vite Internal Utilities** - Leverages `vite.normalizePath` for cross-platform path normalization
|
|
126
|
-
- ✅ **Vite HMR Integration** - Properly integrates with Vite's HMR system for instant updates
|
|
127
|
-
- ✅ **Vite Config Integration** - Respects all Vite configuration options (mode, command, etc.)
|
|
128
|
-
- ✅ **Async/Await Standards** - Uses modern async patterns following Vite conventions
|
|
129
|
-
- ✅ **TypeScript Support** - Full TypeScript definitions for better DX
|
|
130
|
-
- ✅ **No Breaking Changes** - Follows semantic versioning and Vite ecosystem standards
|
|
131
|
-
- ✅ **Zero Vite Configuration Override** - Doesn't interfere with other Vite plugins or features
|
|
132
|
-
|
|
133
|
-
The plugin seamlessly integrates into your Vite workflow without any conflicts.
|
|
134
|
-
|
|
135
|
-
## 📖 Documentation
|
|
136
|
-
|
|
137
|
-
### Options
|
|
69
|
+
## Configuration
|
|
138
70
|
|
|
139
71
|
```typescript
|
|
140
72
|
interface SvgSpriteOptions {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
/** Enable optimization (default: true) */
|
|
154
|
-
optimize?: boolean;
|
|
155
|
-
|
|
156
|
-
/** Watch for changes in dev mode (default: true) */
|
|
157
|
-
watch?: boolean;
|
|
158
|
-
|
|
159
|
-
/** Debounce delay for HMR (default: 100ms) */
|
|
160
|
-
debounceDelay?: number;
|
|
161
|
-
|
|
162
|
-
/** Verbose logging (default: true in dev, false in prod) */
|
|
163
|
-
verbose?: boolean;
|
|
164
|
-
|
|
165
|
-
/** Enable SVGO optimization (default: true in production) */
|
|
166
|
-
svgoOptimize?: boolean;
|
|
167
|
-
|
|
168
|
-
/** Custom SVGO configuration */
|
|
169
|
-
svgoConfig?: Config;
|
|
73
|
+
iconsFolder?: string; // Default: 'src/icons'
|
|
74
|
+
spriteId?: string; // Default: 'sprite-id'
|
|
75
|
+
spriteClass?: string; // Default: 'sprite-class'
|
|
76
|
+
idPrefix?: string; // Default: ''
|
|
77
|
+
watch?: boolean; // Default: true (dev)
|
|
78
|
+
debounceDelay?: number; // Default: 100ms
|
|
79
|
+
verbose?: boolean; // Default: true (dev)
|
|
80
|
+
svgoOptimize?: boolean; // Default: true (production)
|
|
81
|
+
svgoConfig?: object; // Custom SVGO config
|
|
82
|
+
currentColor?: boolean; // Default: true
|
|
83
|
+
treeShaking?: boolean; // Default: false
|
|
84
|
+
scanExtensions?: string[]; // Default: ['.html', '.js', '.ts', '.jsx', '.tsx', '.vue', '.svelte']
|
|
170
85
|
}
|
|
171
86
|
```
|
|
172
87
|
|
|
173
|
-
###
|
|
174
|
-
|
|
175
|
-
#### Basic Usage
|
|
88
|
+
### Basic Example
|
|
176
89
|
|
|
177
90
|
```javascript
|
|
178
91
|
svgSpritePlugin({
|
|
179
92
|
iconsFolder: 'src/icons',
|
|
93
|
+
treeShaking: true,
|
|
180
94
|
verbose: true
|
|
181
95
|
})
|
|
182
96
|
```
|
|
183
97
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
```javascript
|
|
187
|
-
svgSpritePlugin({
|
|
188
|
-
iconsFolder: 'assets/svg',
|
|
189
|
-
spriteId: 'my-sprite',
|
|
190
|
-
idPrefix: 'icon', // Add prefix: generates 'icon-home', 'icon-user'
|
|
191
|
-
debounceDelay: 200,
|
|
192
|
-
verbose: true
|
|
193
|
-
})
|
|
194
|
-
```
|
|
195
|
-
|
|
196
|
-
#### SVGO Optimization
|
|
98
|
+
### Production Optimized
|
|
197
99
|
|
|
198
100
|
```javascript
|
|
199
101
|
svgSpritePlugin({
|
|
200
102
|
iconsFolder: 'src/icons',
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
plugins: [
|
|
205
|
-
'preset-default',
|
|
206
|
-
{
|
|
207
|
-
name: 'removeViewBox',
|
|
208
|
-
active: false // Keep viewBox for sprites
|
|
209
|
-
},
|
|
210
|
-
{
|
|
211
|
-
name: 'cleanupNumericValues',
|
|
212
|
-
params: {
|
|
213
|
-
floatPrecision: 2
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
]
|
|
217
|
-
}
|
|
103
|
+
treeShaking: true, // Remove unused icons
|
|
104
|
+
svgoOptimize: true, // Optimize SVG
|
|
105
|
+
currentColor: true // CSS color control
|
|
218
106
|
})
|
|
219
107
|
```
|
|
220
108
|
|
|
221
|
-
|
|
109
|
+
## Framework Support
|
|
222
110
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
{ name: 'removeAttrs', params: { attrs: '(fill|stroke)' } }
|
|
234
|
-
]
|
|
235
|
-
}
|
|
236
|
-
})
|
|
237
|
-
```
|
|
111
|
+
| Framework | Status | Notes |
|
|
112
|
+
|-----------|--------|-------|
|
|
113
|
+
| **Vite + React** | ✅ Full | [Examples](./VUE_REACT_SVELTE_GUIDE.md#react) |
|
|
114
|
+
| **Vite + Vue 3** | ✅ Full | [Examples](./VUE_REACT_SVELTE_GUIDE.md#vue-3) |
|
|
115
|
+
| **Vite + Svelte** | ✅ Full | [Examples](./VUE_REACT_SVELTE_GUIDE.md#svelte) |
|
|
116
|
+
| **Nuxt 3 (SPA)** | ✅ Full | Set `ssr: false` |
|
|
117
|
+
| **Nuxt 3 (SSR)** | ⚠️ Partial | [SSR Guide](./FRAMEWORK_COMPATIBILITY.md#nuxt-3-ssr) |
|
|
118
|
+
| **SvelteKit** | ⚠️ Partial | SSR limitations |
|
|
119
|
+
| **Next.js** | ❌ No | Uses Webpack, not Vite |
|
|
120
|
+
| **Create React App** | ❌ No | Uses Webpack, not Vite |
|
|
238
121
|
|
|
239
|
-
**
|
|
122
|
+
> **Rule of thumb:** Works with Vite ✅ | Doesn't work with Webpack ❌
|
|
240
123
|
|
|
241
|
-
|
|
124
|
+
### React Example
|
|
242
125
|
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
svgSpritePlugin({ iconsFolder: './public/icons' })
|
|
252
|
-
|
|
253
|
-
// ❌ BLOCKED - Paths outside project root
|
|
254
|
-
svgSpritePlugin({ iconsFolder: '../../../etc' }) // Error!
|
|
255
|
-
svgSpritePlugin({ iconsFolder: '../../other-project' }) // Error!
|
|
256
|
-
svgSpritePlugin({ iconsFolder: '/absolute/path' }) // Error!
|
|
257
|
-
```
|
|
258
|
-
|
|
259
|
-
**Error message example:**
|
|
126
|
+
```jsx
|
|
127
|
+
export function Icon({ name, size = 24 }) {
|
|
128
|
+
return (
|
|
129
|
+
<svg width={size} height={size}>
|
|
130
|
+
<use href={`#${name}`} />
|
|
131
|
+
</svg>
|
|
132
|
+
);
|
|
133
|
+
}
|
|
260
134
|
|
|
135
|
+
// Usage
|
|
136
|
+
<Icon name="home" />
|
|
261
137
|
```
|
|
262
|
-
❌ Security Error: Invalid iconsFolder path
|
|
263
138
|
|
|
264
|
-
|
|
265
|
-
Resolved to: "/etc"
|
|
266
|
-
Project root: "/home/user/project"
|
|
139
|
+
See [complete framework examples](./VUE_REACT_SVELTE_GUIDE.md).
|
|
267
140
|
|
|
268
|
-
|
|
269
|
-
This is not allowed for security reasons (path traversal prevention).
|
|
141
|
+
## Key Features
|
|
270
142
|
|
|
271
|
-
|
|
272
|
-
- 'src/icons' → relative to project root
|
|
273
|
-
- 'assets/svg' → relative to project root
|
|
274
|
-
- './public/icons' → explicit relative path
|
|
143
|
+
### Tree-Shaking
|
|
275
144
|
|
|
276
|
-
|
|
277
|
-
- '../other-project' → outside project (path traversal)
|
|
278
|
-
- '../../etc' → system directory access attempt
|
|
279
|
-
- '/absolute/path' → absolute paths not allowed
|
|
145
|
+
Remove unused icons from production builds:
|
|
280
146
|
|
|
281
|
-
|
|
147
|
+
```javascript
|
|
148
|
+
svgSpritePlugin({
|
|
149
|
+
treeShaking: true // Enable tree-shaking
|
|
150
|
+
})
|
|
282
151
|
```
|
|
283
152
|
|
|
284
|
-
|
|
153
|
+
**How it works:**
|
|
154
|
+
1. Scans codebase for `<use href="#iconId">`
|
|
155
|
+
2. Finds used icons in HTML/JS/TS/JSX/TSX/Vue/Svelte
|
|
156
|
+
3. Removes unused icons from production
|
|
157
|
+
4. Keeps all icons in dev for better DX
|
|
285
158
|
|
|
286
|
-
|
|
159
|
+
**Results:**
|
|
160
|
+
```
|
|
161
|
+
50 total icons → 8 used (42 removed, 84% reduction)
|
|
162
|
+
Bundle: 45.2 KB → 7.8 KB
|
|
163
|
+
```
|
|
287
164
|
|
|
288
|
-
-
|
|
289
|
-
- Removes event handlers (`onclick`, `onload`, etc.)
|
|
290
|
-
- Removes `javascript:` URLs in `href` and `xlink:href`
|
|
291
|
-
- Removes `<foreignObject>` elements
|
|
165
|
+
**Per-page optimization:** In multi-page apps, each HTML page gets only its icons.
|
|
292
166
|
|
|
293
|
-
|
|
167
|
+
### Hot Module Replacement
|
|
294
168
|
|
|
295
|
-
|
|
169
|
+
Changes to SVG files trigger instant updates without page reload:
|
|
296
170
|
|
|
297
|
-
|
|
171
|
+
```
|
|
172
|
+
🔄 SVG files changed, regenerating sprite...
|
|
173
|
+
✅ HMR: Sprite updated with 10 icons
|
|
174
|
+
```
|
|
298
175
|
|
|
299
|
-
###
|
|
176
|
+
### Security
|
|
300
177
|
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
</svg>
|
|
178
|
+
Automatic protection against:
|
|
179
|
+
- **XSS attacks** - Removes `<script>`, event handlers, `javascript:` URLs
|
|
180
|
+
- **Path traversal** - Validates icon folder paths
|
|
181
|
+
- **Malicious content** - Sanitizes all SVG before injection
|
|
306
182
|
|
|
307
|
-
|
|
308
|
-
<svg class="icon" width="32" height="32">
|
|
309
|
-
<use href="#user"></use>
|
|
310
|
-
</svg>
|
|
183
|
+
### Multi-Page Projects
|
|
311
184
|
|
|
312
|
-
|
|
313
|
-
<svg class="icon" role="img" aria-label="Settings">
|
|
314
|
-
<use href="#settings"></use>
|
|
315
|
-
</svg>
|
|
185
|
+
Works seamlessly with [vite-multi-page-html-generator-plugin](https://www.npmjs.com/package/vite-multi-page-html-generator-plugin):
|
|
316
186
|
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
187
|
+
```javascript
|
|
188
|
+
export default defineConfig({
|
|
189
|
+
plugins: [
|
|
190
|
+
multiPagePlugin({
|
|
191
|
+
pagesDir: 'src/pages'
|
|
192
|
+
}),
|
|
193
|
+
svgSpritePlugin({
|
|
194
|
+
treeShaking: true // Each page gets only its icons
|
|
195
|
+
})
|
|
196
|
+
]
|
|
197
|
+
});
|
|
321
198
|
```
|
|
322
199
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
```jsx
|
|
326
|
-
// React
|
|
327
|
-
function Icon({ name, className = "icon" }) {
|
|
328
|
-
return (
|
|
329
|
-
<svg className={className}>
|
|
330
|
-
<use href={`#${name}`} />
|
|
331
|
-
</svg>
|
|
332
|
-
);
|
|
333
|
-
}
|
|
200
|
+
## Performance
|
|
334
201
|
|
|
335
|
-
|
|
336
|
-
<Icon name="home" />
|
|
202
|
+
### Build Time (v1.3.0)
|
|
337
203
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
<use href={`#${id}`} />
|
|
344
|
-
</svg>
|
|
345
|
-
);
|
|
346
|
-
}
|
|
347
|
-
```
|
|
204
|
+
| Icons | v1.2.1 | v1.3.0 | Speedup |
|
|
205
|
+
|-------|--------|--------|---------|
|
|
206
|
+
| 50 | 850ms | 420ms | 2.0x ⚡ |
|
|
207
|
+
| 100 | 1.7s | 810ms | 2.1x ⚡ |
|
|
208
|
+
| 200 | 3.4s | 1.5s | 2.3x ⚡ |
|
|
348
209
|
|
|
349
|
-
|
|
350
|
-
<!-- Vue -->
|
|
351
|
-
<template>
|
|
352
|
-
<svg class="icon">
|
|
353
|
-
<use :href="`#${name}`" />
|
|
354
|
-
</svg>
|
|
355
|
-
</template>
|
|
210
|
+
### Bundle Size (with tree-shaking)
|
|
356
211
|
|
|
357
|
-
<script setup>
|
|
358
|
-
defineProps(['name']);
|
|
359
|
-
</script>
|
|
360
212
|
```
|
|
213
|
+
Project: 50 icons total, 8 used
|
|
361
214
|
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
export let name;
|
|
366
|
-
</script>
|
|
367
|
-
|
|
368
|
-
<svg class="icon">
|
|
369
|
-
<use href="#{name}" />
|
|
370
|
-
</svg>
|
|
215
|
+
Before: 45.2 KB (all icons)
|
|
216
|
+
After: 7.8 KB (used only)
|
|
217
|
+
Saving: 37.4 KB (83% reduction)
|
|
371
218
|
```
|
|
372
219
|
|
|
373
|
-
---
|
|
374
|
-
|
|
375
|
-
## 📊 Performance
|
|
376
|
-
|
|
377
|
-
### Optimization Results
|
|
378
|
-
|
|
379
|
-
| Metric | Before | After | Improvement |
|
|
380
|
-
|--------|--------|-------|-------------|
|
|
381
|
-
| Sprite Size | 87 KB | 42 KB | **-52%** |
|
|
382
|
-
| Gzip | 24 KB | 14 KB | **-42%** |
|
|
383
|
-
| Load Time (3G) | 320 ms | 187 ms | **-42%** |
|
|
384
|
-
|
|
385
220
|
### SVGO Optimization
|
|
386
221
|
|
|
387
222
|
```
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
223
|
+
Average reduction: 40-60%
|
|
224
|
+
Example:
|
|
225
|
+
clock.svg: 317 → 228 bytes (-28%)
|
|
226
|
+
layers.svg: 330 → 156 bytes (-53%)
|
|
391
227
|
```
|
|
392
228
|
|
|
393
|
-
|
|
229
|
+
## How It Works
|
|
394
230
|
|
|
395
|
-
|
|
231
|
+
The plugin automatically injects the sprite into your HTML:
|
|
396
232
|
|
|
397
|
-
|
|
233
|
+
```html
|
|
234
|
+
<body>
|
|
235
|
+
<!-- Injected automatically -->
|
|
236
|
+
<svg id="sprite-id" style="display: none;">
|
|
237
|
+
<symbol id="home" viewBox="0 0 24 24">...</symbol>
|
|
238
|
+
<symbol id="user" viewBox="0 0 24 24">...</symbol>
|
|
239
|
+
</svg>
|
|
240
|
+
|
|
241
|
+
<!-- Your app -->
|
|
242
|
+
<div id="app"></div>
|
|
243
|
+
</body>
|
|
244
|
+
```
|
|
398
245
|
|
|
399
|
-
|
|
400
|
-
-
|
|
401
|
-
-
|
|
402
|
-
-
|
|
246
|
+
**Benefits:**
|
|
247
|
+
- No separate file requests
|
|
248
|
+
- Instant rendering
|
|
249
|
+
- Single HTTP request
|
|
250
|
+
- Works with SSR/SSG
|
|
403
251
|
|
|
404
|
-
|
|
252
|
+
## Advanced
|
|
405
253
|
|
|
406
|
-
|
|
407
|
-
- 🚀 **Preview Optimization** - Preview runs instantly (0ms vs 583ms)
|
|
408
|
-
- 🎯 **Auto-Detection** - Automatic command detection (serve/build/preview)
|
|
409
|
-
- ✅ **No Breaking Changes** - Fully backward compatible
|
|
254
|
+
### Vite Integration
|
|
410
255
|
|
|
411
|
-
|
|
256
|
+
Uses official Vite APIs:
|
|
257
|
+
- `enforce: 'pre'` - Run before core plugins
|
|
258
|
+
- `apply()` - Conditional execution
|
|
259
|
+
- `createFilter()` - Standard file filtering
|
|
260
|
+
- HMR API - Hot module replacement
|
|
412
261
|
|
|
413
|
-
|
|
414
|
-
- ⚡ Improved Windows/Unix path handling
|
|
415
|
-
- 🐛 Better edge case support (network paths, etc.)
|
|
416
|
-
- ✅ **No Breaking Changes** - Fully backward compatible
|
|
262
|
+
### Plugin Hooks
|
|
417
263
|
|
|
418
|
-
|
|
264
|
+
```javascript
|
|
265
|
+
{
|
|
266
|
+
configResolved() { // Validate paths
|
|
267
|
+
buildStart() { // Generate sprite
|
|
268
|
+
transformIndexHtml() { // Inject into HTML
|
|
269
|
+
configureServer() { // Setup HMR
|
|
270
|
+
buildEnd() { // Cleanup
|
|
271
|
+
}
|
|
272
|
+
```
|
|
419
273
|
|
|
420
|
-
|
|
421
|
-
- ⚡ **100% Async FS** - No event loop blocking
|
|
422
|
-
- 🚀 **20% Faster** - Precompiled RegExp patterns
|
|
423
|
-
- 📝 **Better Errors** - Detailed messages with examples
|
|
424
|
-
- ✅ **No Breaking Changes** - Fully backward compatible
|
|
274
|
+
### Optimizations
|
|
425
275
|
|
|
426
|
-
|
|
276
|
+
- Parallel SVG processing
|
|
277
|
+
- mtime-based caching
|
|
278
|
+
- Debounced HMR
|
|
279
|
+
- Tree-shaking
|
|
280
|
+
- SVGO compression
|
|
427
281
|
|
|
428
|
-
|
|
429
|
-
- 📄 Added "How It Works" section
|
|
430
|
-
- 💡 Added "Why Inline SVG?" section explaining benefits
|
|
282
|
+
## Compatibility
|
|
431
283
|
|
|
432
|
-
|
|
284
|
+
- **Vite:** 4.x, 5.x, 6.x, 7.x
|
|
285
|
+
- **Node.js:** 14.18.0+
|
|
286
|
+
- **TypeScript:** Full support
|
|
287
|
+
- **OS:** Windows, macOS, Linux
|
|
433
288
|
|
|
434
|
-
|
|
435
|
-
- 🚀 SVGO Optimization - 40-60% size reduction
|
|
436
|
-
- ⚡ Hot Module Replacement - Instant updates
|
|
437
|
-
- 🔒 Security First - XSS protection and path traversal prevention
|
|
438
|
-
- 💾 Smart Caching - LRU-like cache with mtime validation
|
|
439
|
-
- 🎯 Auto-Injection - Automatic sprite injection into HTML
|
|
440
|
-
- 📦 Zero Config - Works out of the box
|
|
441
|
-
- 🌳 Tree-Shakeable - ES modules with proper exports
|
|
289
|
+
## Links
|
|
442
290
|
|
|
443
|
-
|
|
291
|
+
- [npm Package](https://www.npmjs.com/package/vite-svg-sprite-generator-plugin)
|
|
292
|
+
- [GitHub Repository](https://github.com/gkarev/vite-svg-sprite-generator-plugin)
|
|
293
|
+
- [Issues](https://github.com/gkarev/vite-svg-sprite-generator-plugin/issues)
|
|
294
|
+
- [Changelog](./CHANGELOG.md)
|
|
444
295
|
|
|
445
|
-
##
|
|
296
|
+
## Documentation
|
|
446
297
|
|
|
447
|
-
|
|
298
|
+
- [Framework Examples (React/Vue/Svelte)](./VUE_REACT_SVELTE_GUIDE.md)
|
|
299
|
+
- [Framework Compatibility (Next.js/Nuxt/etc)](./FRAMEWORK_COMPATIBILITY.md)
|
|
300
|
+
- [Tree-Shaking Guide](./TREE_SHAKING_GUIDE.md)
|
|
448
301
|
|
|
449
|
-
##
|
|
302
|
+
## Related Plugins
|
|
450
303
|
|
|
451
|
-
- [
|
|
452
|
-
- [Vite](https://vitejs.dev/) - Build tool
|
|
304
|
+
- [vite-multi-page-html-generator-plugin](https://www.npmjs.com/package/vite-multi-page-html-generator-plugin) - Multi-page static site generator
|
|
453
305
|
|
|
454
|
-
##
|
|
306
|
+
## License
|
|
455
307
|
|
|
456
|
-
|
|
457
|
-
- 💬 [Discussions](https://github.com/gkarev/vite-svg-sprite-generator-plugin/discussions)
|
|
308
|
+
MIT © [Karev G.S.](https://github.com/gkarev)
|
|
458
309
|
|
|
459
310
|
---
|
|
460
311
|
|
|
461
|
-
Made with ❤️
|
|
462
|
-
|
|
463
|
-
If this plugin helped you, please ⭐ star the repo!
|
|
464
|
-
|
|
312
|
+
**Made with ❤️ for the Vite ecosystem**
|