@onedarnleyroad/vite-plugin-svg-sprite 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 One Darnley Road
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,66 @@
1
+ # @onedarnleyroad/vite-plugin-svg-sprite
2
+
3
+ A Vite plugin that builds an SVG sprite sheet from a directory of SVG files.
4
+
5
+ ## Features
6
+
7
+ - Combines individual SVGs into a single `<svg>` sprite with `<symbol>` elements
8
+ - Namespaces internal IDs to prevent conflicts across icons
9
+ - Preserves existing `<title>` elements, or falls back to the filename
10
+ - Strips comments and empty `<defs>` blocks
11
+ - Rebuilds automatically in watch mode (HMR-aware)
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ npm install -D @onedarnleyroad/vite-plugin-svg-sprite
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ```js
22
+ // vite.config.js
23
+ import { defineConfig } from 'vite'
24
+ import svgSprite from '@onedarnleyroad/vite-plugin-svg-sprite'
25
+
26
+ export default defineConfig({
27
+ plugins: [
28
+ svgSprite({
29
+ inputDir: 'src/icons',
30
+ outputFile: 'web/dist/sprite.svg',
31
+ }),
32
+ ],
33
+ })
34
+ ```
35
+
36
+ ### Options
37
+
38
+ | Option | Type | Description |
39
+ |--------|------|-------------|
40
+ | `inputDir` | `string` | Directory containing the source `.svg` files |
41
+ | `outputFile` | `string` | Path for the generated sprite sheet |
42
+
43
+ Both paths are resolved relative to the project root (where `vite.config.js` lives).
44
+
45
+ ## Output
46
+
47
+ Each SVG becomes a `<symbol>` with an `id` prefixed with `svg-`. For example, `arrow.svg` becomes:
48
+
49
+ ```html
50
+ <symbol id="svg-arrow" viewBox="0 0 24 24">
51
+ <title>arrow icon</title>
52
+ <!-- ... -->
53
+ </symbol>
54
+ ```
55
+
56
+ Use it in your HTML with `<use>`:
57
+
58
+ ```html
59
+ <svg aria-hidden="true">
60
+ <use href="/dist/sprite.svg#svg-arrow" />
61
+ </svg>
62
+ ```
63
+
64
+ ## License
65
+
66
+ MIT © [One Darnley Road](https://onedarnleyroad.com)
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@onedarnleyroad/vite-plugin-svg-sprite",
3
+ "version": "1.0.0",
4
+ "description": "Vite plugin that builds an SVG sprite sheet from a directory of SVG files",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": "./src/index.js"
8
+ },
9
+ "files": [
10
+ "src"
11
+ ],
12
+ "keywords": [
13
+ "vite",
14
+ "vite-plugin",
15
+ "svg",
16
+ "sprite",
17
+ "svg-sprite"
18
+ ],
19
+ "author": "One Darnley Road <hello@onedarnleyroad.com>",
20
+ "license": "MIT",
21
+ "peerDependencies": {
22
+ "vite": ">=4.0.0"
23
+ }
24
+ }
package/src/index.js ADDED
@@ -0,0 +1,59 @@
1
+ import { readdirSync, readFileSync, writeFileSync, mkdirSync } from 'fs'
2
+ import { resolve, basename, extname, dirname } from 'path'
3
+
4
+ export default function svgSpritePlugin({ inputDir, outputFile }) {
5
+ const input = resolve(inputDir)
6
+ const output = resolve(outputFile)
7
+
8
+ function build() {
9
+ const symbols = readdirSync(input)
10
+ .filter(f => f.endsWith('.svg'))
11
+ .sort()
12
+ .map(file => {
13
+ const name = basename(file, extname(file))
14
+ const svg = readFileSync(resolve(input, file), 'utf-8')
15
+
16
+ // Preserve existing <title> if present, otherwise use filename
17
+ const existingTitle = svg.match(/<title[^>]*>([\s\S]*?)<\/title>/i)?.[1]?.trim()
18
+ const title = existingTitle ?? `${name} icon`
19
+
20
+ // Strip comments, existing <title> tags, and empty <defs> blocks
21
+ const cleaned = svg
22
+ .replace(/<!--[\s\S]*?-->/g, '')
23
+ .replace(/<title[^>]*>[\s\S]*?<\/title>/gi, '')
24
+ .replace(/<defs[^>]*>\s*<\/defs>/gi, '')
25
+
26
+ // Namespace internal IDs to avoid conflicts when sprites are merged
27
+ const namespaced = cleaned
28
+ .replace(/\bid="([^"]+)"/g, `id="${name}-$1"`)
29
+ .replace(/\burl\(#([^)]+)\)/g, `url(#${name}-$1)`)
30
+ .replace(/\bhref="#([^"]+)"/g, `href="#${name}-$1"`)
31
+ .replace(/\bxlink:href="#([^"]+)"/g, `xlink:href="#${name}-$1"`)
32
+
33
+ const viewBox = namespaced.match(/viewBox="([^"]+)"/)?.[1] ?? '0 0 24 24'
34
+
35
+ const inner = namespaced
36
+ .replace(/<svg[^>]*>/i, '')
37
+ .replace(/<\/svg>/i, '')
38
+ .replace(/\s*\n\s*/g, ' ')
39
+ .replace(/>\s+</g, '><')
40
+ .trim()
41
+
42
+ return `<symbol id="svg-${name}" viewBox="${viewBox}"><title>${title}</title>${inner}</symbol>`
43
+ })
44
+
45
+ mkdirSync(dirname(output), { recursive: true })
46
+ writeFileSync(output, `<svg xmlns="http://www.w3.org/2000/svg">\n${symbols.join('\n')}\n</svg>`)
47
+ }
48
+
49
+ return {
50
+ name: 'svg-sprite',
51
+ buildStart() {
52
+ if (this.meta.watchMode) build()
53
+ },
54
+ writeBundle: build,
55
+ handleHotUpdate({ file }) {
56
+ if (file.startsWith(input)) build()
57
+ },
58
+ }
59
+ }