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