vite-awesome-svg-loader 2.0.1 → 3.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/README.md CHANGED
@@ -13,395 +13,31 @@ A Vite plugin that:
13
13
  1. Will automatically minimize your SVGs using [SVGO](https://github.com/svg/svgo).
14
14
  1. Allows you to create SVG sprites using provided integrations.
15
15
 
16
- `vite-awesome-svg-loader` is framework-agnostic. All integrations are done in form of subpath imports (for example, `vite-awesome-svg-loader/vue-integration`). This means that you'll get only what you need in your app bundle. This also means that you can develop your own integration using `vite-awesome-svg-loader/integration-utils` import.
16
+ `vite-awesome-svg-loader` is framework-agnostic. All integrations are done as subpath imports
17
+ (for example, `vite-awesome-svg-loader/vue-integration`). This means that you'll get only what you need in your
18
+ app bundle. This also means that you can
19
+ [develop your own integration](https://matafokka.github.io/vite-awesome-svg-loader/guides/custom-integrations)
20
+ using `vite-awesome-svg-loader/integration-utils` import.
17
21
 
18
- ## Examples and demos
22
+ ## Installation
19
23
 
20
- - Vanilla JS: [source](https://github.com/matafokka/vite-awesome-svg-loader/tree/main/apps/vanilla-demo/README.md), [demo](https://matafokka.github.io/vite-awesome-svg-loader/vanilla-demo), [docs](https://matafokka.github.io/vite-awesome-svg-loader/vanilla-integration-docs).
21
- - Vue 3: [source](https://github.com/matafokka/vite-awesome-svg-loader/tree/main/apps/vue-demo/README.md), [demo](https://matafokka.github.io/vite-awesome-svg-loader/vue-demo).
22
- - React: [source](https://github.com/matafokka/vite-awesome-svg-loader/tree/main/apps/react-demo/README.md), [demo](https://matafokka.github.io/vite-awesome-svg-loader/react-demo).
23
-
24
- ## Usage
25
-
26
- 1. Grab this plugin from NPM: `npm i vite-awesome-svg-loader`.
27
- 2. Add it to `vite.config.js` or `vite.config.ts` (whatever you use):
28
-
29
- ```ts
30
- import { defineConfig } from "vite";
31
- import vue from "@vitejs/plugin-vue";
32
-
33
- // Import vite-awesome-svg-loader
34
- import { viteAwesomeSvgLoader } from "vite-awesome-svg-loader";
35
-
36
- export default defineConfig({
37
- plugins: [
38
- vue(), // Or whatever framework you're using
39
- viteAwesomeSvgLoader(), // This plugin
40
- // Other plugins...
41
- ],
42
- // Rest of the configuration...
43
- });
44
- ```
45
-
46
- 3. Optional, TypeScript only. Add some _(unfortunately, it's impossible to implement all)_ of the import types to make TypeScript happy:
47
-
48
- 1. Create `env.d.ts` file or use existing file where you store [triple slash directives](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html).
49
- 2. Add following string to it: `/// <reference types="vite-awesome-svg-loader" />`.
50
- 3. Open `tsconfig.json` and add `env.d.ts` to the `include` property: ` { "include": ["env.d.ts"] }`.
51
-
52
- 4. Use it to import files:
53
-
54
- ```ts
55
- // Source code
56
- import imageSrc from "./path/to/image.svg";
57
-
58
- // URL
59
- import imageUrl from "./path/to/image.svg?url";
60
-
61
- // Source code Data URI
62
- import imageSrcDataUri from "./path/to/image.svg?source-data-uri";
63
-
64
- // Base64
65
- import imageBase64 from "./path/to/image.svg?base-64";
66
-
67
- // Base64 data URI
68
- import imageBase64DataUri from "./path/to/image.svg?base-64-data-uri";
69
-
70
- // Preserve line width
71
- import imagePreWidthSrc from "./path/to/image.svg?preserve-line-width";
72
-
73
- // Replace fill, stroke and stop-color with currentColor
74
- import imageCurColorSrc from "./path/to/image.svg?set-current-color";
75
-
76
- // Mix and match. We need @ts-ignore here because it's impossible to generate types for all possible URLs.
77
- // It is recommended to configure paths to avoid putting @ts-ignore for every import and writing long URLs.
78
- // @ts-ignore
79
- import transformedImageUrl from "./path/to/image.svg?url&preserve-line-width&set-current-color";
80
-
81
- // explicitly disable any parameter by setting it to false. This takes precedence over config.
82
- import initialLineWidthImageUrl from "./path/to/image.svg?url&preserve-line-width=false";
83
- ```
84
-
85
- 5. Optional, but highly recommended. Configure loader, so you can import SVGs without URL parameters:
86
-
87
- ```ts
88
- viteAwesomeSvgLoader({
89
- // Set default import type. "source" is the default value.
90
- //
91
- // Available values:
92
- // source - load SVG source code.
93
- // url - load URL pointing to the SVG file. Loader will generate that file for you.
94
- // source-data-uri - Source code put into a data URI.
95
- // base64 - SVG source code encoded in base64.
96
- // base64-data-uri - SVG source code in base64 put into a data URI.
97
- defaultImport: "source",
98
-
99
- // A list of files or directories to preserve line width of.
100
- preserveLineWidthList: [/config-demo\/preserve-line-width\//, /config-demo\/all\//],
101
-
102
- // A list of files to skip while preserving line width. Overrides preserveLineWidthList.
103
- skipPreserveLineWidthList: [/line-width-not-preserved\.svg/],
104
-
105
- // A list of files or directories to preserve color of
106
- replaceColorsList: [
107
- // File names
108
- "some-file.svg",
109
-
110
- // Regexes that are checked against whole path and file name with extension
111
- /config-demo\/set-current-color\//,
112
- /config-demo\/all\//,
113
-
114
- // Map of color replacements. Key is an original color, value is its replacement. Both can be any values:
115
- // HEX, name, rgb() or arbitrary custom values. Applied to all files.
116
- {
117
- "#003147": "red",
118
- "rgb(0, 49, 71)": "#003147",
119
- "myCustomColor": "var(--some-color-var)",
120
- },
121
-
122
- // Map of color replacements per files
123
- {
124
- files: ["vars.svg"], // File names or regexes, same format as above
125
-
126
- // Replacements, same format as above
127
- replacements: {
128
- red: "var(--primary-color)",
129
- green: "var(--secondary-color)",
130
- blue: "var(--tertiary-color)",
131
- },
132
-
133
- // Default value for colors that are not in replacements map. Set an empty string to preserve original colors.
134
- // Default value is "currentColor",
135
- default: "currentColor"
136
- },
137
- ],
138
-
139
- // A list of files to skip while replacing colors. Overrides replaceColorsList.
140
- skipReplaceColorsList: [/colors-not-preserved\.svg/],
141
-
142
- // A list of files to skip while transforming. File skip-transforms.svg is present in every directory.
143
- skipTransformsList: [/skip-transforms\.svg/, /ignore-elements-orig\.svg/],
144
-
145
- // A list of files to skip loading of. File skip-loading.svg is present in every directory.
146
- skipFilesList: [/skip-loading\.svg/],
147
-
148
- // A list of selectors to skip while preserving line width
149
- skipPreserveLineWidthSelectors: [
150
- // It can be a list of CSS selectors like this one. Every element in every file will be checked against it.
151
- '*[data-original-line-width="true"], *[data-original-line-width="true"] *',
152
-
153
- // Or it can be configured on per-file basis:
154
- {
155
- files: [/ignore-elements\.svg/, /some-other-file\.svg/],
156
- selectors: ['*[data-original-line-width="true"], *[data-original-line-width="true"] *'],
157
- },
158
- ],
159
-
160
- // These options are not recommended due to architectural and performance reasons (see JSDoc):
161
-
162
- // A list of selectors to skip while replacing colors. Same format as above.
163
- skipReplaceColorsSelectors: ['*[data-original-color="true"], *[data-original-color="true"] *'],
164
-
165
- // A list of selectors to skip while transforming. Same format as above.
166
- skipTransformsSelectors: ['*[data-no-transforms="true"], *[data-no-transforms="true"] *'],
167
- })
168
- ```
169
-
170
- 6. Optional. Use integrations to create sprite sheets. Or [write your own integration](https://github.com/matafokka/vite-awesome-svg-loader/tree/main/#custom-integration).
171
-
172
- ## Integrations
173
-
174
- All integrations work only on client side. However, they won't break SSR.
175
-
176
- ### Vanilla JS
177
-
178
- **Warning**: do NOT use this package to create custom integrations, use `vite-awesome-svg-loader/integration-utils` instead.
179
-
180
- Make sure to check the [docs](https://matafokka.github.io/vite-awesome-svg-loader/vanilla-integration-docs).
181
-
182
- 1. Import classes:
183
-
184
- ```ts
185
- import {
186
- SvgImage, // Basically implements SVG sprites
187
- SvgIcon, // Basic SVG icon that uses SvgImage class internally
188
- } from "vite-awesome-svg-loader/vanilla-integration";
189
- ```
190
-
191
- 2. Import images:
192
-
193
- ```ts
194
- import imageSrc from "@/path/to/image.svg";
195
- ```
196
-
197
- 3. Use classes:
198
-
199
- ```ts
200
- new SvgImage(imageSrc, "#my-container"); // Create an image and mount it to the element with "my-container" id.
201
-
202
- new SvgIcon(imageSrc, "#my-container") // Create an icon and mount it to the element with "my-container" id.
203
- .setSize("24px") // Set 24px size
204
- .setColor("red") // Set red color
205
- .setColorTransition("0.3s ease-out"); // Set color transition
206
- ```
207
-
208
- See full example: [source](https://github.com/matafokka/vite-awesome-svg-loader/tree/main/apps/vanilla-demo/README.md), [demo](https://matafokka.github.io/vite-awesome-svg-loader/vanilla-demo).
209
-
210
- ### Vue 3
211
-
212
- 1. Import components:
213
-
214
- ```ts
215
- import {
216
- SvgImage, // Basically implements SVG sprites
217
- SvgIcon, // Basic SVG icon that uses SvgImage component internally
218
- } from "vite-awesome-svg-loader/vue-integration";
219
- ```
220
-
221
- 2. Import images:
222
-
223
- ```ts
224
- import imageSrc from "@/path/to/image.svg";
225
- ```
226
-
227
- 3. Use components:
228
-
229
- ```vue
230
- <template>
231
- <div class="main">
232
- <SvgImage :src="imageSrc" />
233
-
234
- <SvgIcon
235
- :src="imageSrc"
236
- size="24px"
237
- color="red"
238
- color-transition="0.3s ease-out"
239
- />
240
- </div>
241
- </template>
24
+ ```sh
25
+ npm i vite-awesome-svg-loader
242
26
  ```
243
27
 
244
- See full example: [source](https://github.com/matafokka/vite-awesome-svg-loader/tree/main/apps/vue-demo/README.md), [demo](https://matafokka.github.io/vite-awesome-svg-loader/vue-demo).
245
-
246
- ### React
247
-
248
-
249
- 1. Import components:
250
-
251
- ```ts
252
- import {
253
- SvgImage, // Basically implements SVG sprites
254
- SvgIcon, // Basic SVG icon that uses SvgImage component internally
255
- } from "vite-awesome-svg-loader/react-integration";
256
- ```
257
-
258
- 2. Import images:
259
-
260
- ```ts
261
- import imageSrc from "@/path/to/image.svg";
262
- ```
263
- 3. Use components:
264
-
265
- ```tsx
266
- export function MyComponent() {
267
- return (
268
- <div class="main">
269
- <SvgImage src={imageSrc} />
270
-
271
- <SvgIcon
272
- src={imageSrc}
273
- size="24px"
274
- color="red"
275
- colorTransition="0.3s ease-out"
276
- />
277
- </div>
278
- )
279
- }
280
- ```
281
-
282
- See full example: [source](https://github.com/matafokka/vite-awesome-svg-loader/tree/main/apps/react-demo/README.md), [demo](https://matafokka.github.io/vite-awesome-svg-loader/react-demo).
283
-
284
- ### Custom integration
285
-
286
- 1. Import helper functions and styles:
287
-
288
- ```ts
289
- import "integration-utils/styles.css";
290
-
291
- // There function will run only on client side. However, they won't break SSR.
292
-
293
- import {
294
- onSrcUpdate, // Should be called when component is mounted, and source code is updated
295
- onUnmount, // Should be called when component is unmounted
296
- } from "integration-utils";
297
- ```
298
-
299
- 2. Write your integration. Use [vue-integration](https://github.com/matafokka/vite-awesome-svg-loader/tree/main/packages/vue-integration/README.md) as an example.
300
-
301
- 3. Please, submit your integration in a PR. Or just create an issue and provide the source code if you don't have time
302
- or don't want to deal with this project's development process.
303
-
304
- ## Additional information
305
-
306
- ### What are loading/import types (url, source, etc)?
307
-
308
- Let's see how we can load SVGs, and what tradeoffs do they have:
309
-
310
- 1. Put whole SVG source code into the DOM every time image is displayed. This is the most popular method.
311
- 1. Advantages:
312
- 1. Simplicity.
313
- 1. Great customization. This is actually the only option, if you need to manipulate the content, or need more than just CSS styles.
314
- 1. Disadvantages:
315
- 1. Very bad performance. Browser needs to parse and update good number of DOM nodes. It doesn't really matter when images count is low, but it may slow down the page when 1000+ images are used. Another loading method is always preferred when customization is not required.
316
- 1. Link an image like so: `<img href="/image.svg">` or `background-image: url("/image.svg")`.
317
- 1. Advantages:
318
- 1. Simplicity.
319
- 1. Great performance thanks to instancing.
320
- 2. Disadvantages:
321
- 1. Lack of any customization, good only for static images. You can't modify external assets using JS or CSS. The only thing you can do is filters. _**Note:** don't try to implement icons this way. Browsers implement filters incorrectly, it's impossible to write 100% correct color to filter conversion function. This discrepancies are often way off the desired result. If you try to fix them, you'll lose all performance benefits. Trust me, I've tried implementing such system, wasted a lot of time and horribly failed._
322
- 1. Symbols: put all images into a single `<svg>` node and wrap each image with a `<symbol>` element.
323
- 1. Advantages:
324
- 1. Performance is way better than when using whole SVG each time.
325
- 1. Images can be customized via CSS, for example, you can set custom stroke and fill colors.
326
- 1. Disadvantages:
327
- 1. Requires a bit of maintenance in form of using provided integrations.
328
- 1. Performance is worse than when linking an image.
329
- 1. Customization is not that great when using whole SVG source code.
330
- 1. Data URI: either use source code or base64 in a [data URI](https://en.wikipedia.org/wiki/Data_URI_scheme).
331
- 1. Advantages:
332
- 1. Great performance, although a tiny bit worse than when linking an image.
333
- 2. Disadvantages:
334
- 1. Lack of any customization, good only for static images.
335
- 2. Increased memory and traffic usage when linking images.
336
-
337
- To summarize:
338
-
339
- 1. If you don't need customization, i.e. you want to display static images, use linking method.
340
- 1. If you only need CSS customization, use symbols. This is preferred option for icons and line art that's needed to be customized.
341
- 1. If you need heavy customization and interactivity, put whole SVG source code into the DOM.
342
- 1. As for the data URI, not sure why you would need it. It's implemented just for completeness sake. If you have a use for it, please, notify me.
343
-
344
- ### Can this loader put whole SVG source code every time I use an image?
345
-
346
- Yes, load an image as a source code and insert that code into the DOM like shown in the demos.
347
-
348
- ### Should I always import my images like shown in the examples? Can I simplify this?
349
-
350
- Yes, use `import.meta.glob()` like shown in the demos.
351
-
352
- Such components are not present in the integrations because glob imports do not support variable interpolation.
353
- See: https://vitejs.dev/guide/features#glob-import
354
-
355
- **Note**: if you want to support browsers without [ES modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import), use [@vitejs/plugin-legacy](https://www.npmjs.com/package/@vitejs/plugin-legacy) as shown in the demos.
356
-
357
- ### For library authors
358
-
359
- If your icon set is not extensible, just add `vite-awesome-svg-loader` and build your library.
360
-
361
- If your icon set is extensible, you'll need to:
362
-
363
- 1. Provide a plugin that wraps `vite-awesome-svg-loader` and sets your settings.
364
- 1. Tell your users to use your plugin, import icons as source code and pass it to your components.
365
-
366
- ### Comparison with other loaders
367
-
368
- **[vite-svg-loader](https://github.com/jpkleemans/vite-svg-loader)**
369
-
370
- Implements a portion `vite-awesome-svg-loader` functionality. This loader can also import SVGs as Vue components.
371
-
372
- Due to framework-agnostic nature of `vite-awesome-svg-loader` and relative simplicity of outputting raw HTML, this task is delegated to the user, and can be done as easy as you would with [vite-svg-loader](https://github.com/jpkleemans/vite-svg-loader) as shown in the [Vue demo](https://github.com/matafokka/vite-awesome-svg-loader/tree/main/apps/vue-demo/README.md):
373
-
374
- ```vue
375
- <template>
376
- <div v-html="imageSrc" />
377
- </template>
378
-
379
- <script lang="ts" setup>
380
- import imageSrc from "./path/to/image.svg";
381
- </script>
382
- ```
383
-
384
- Similar approach can be applied to any other framework or vanilla JS.
385
-
386
- **[@poppanator/sveltekit-svg](https://github.com/poppa/sveltekit-svg), [vite-plugin-react-svg](https://github.com/damianstasik/vite-svg), [@svgx/vite-plugin-react](https://github.com/salihbenlalla/svgx), [rollup-plugin-svelte-svg](https://www.npmjs.com/package/rollup-plugin-svelte-svg), [vite-plugin-solid-svg](https://github.com/jfgodoy/vite-plugin-solid-svg) and many more**
387
-
388
- Same as above, but for other frameworks.
389
-
390
- **[vite-plugin-svg-icons](https://github.com/vbenjs/vite-plugin-svg-icons/), [vite-plugin-magical-svg](https://github.com/cyyynthia/vite-plugin-magical-svg), [vite-plugin-svg-sprite](https://github.com/meowtec/vite-plugin-svg-sprite)**
391
-
392
- Implements a portion `vite-awesome-svg-loader` functionality.
393
-
394
- **Other loaders and plugins**
28
+ ## Docs
395
29
 
396
- Same as above. If your loader is different, and you want it to get roasted, or if it kills `vite-awesome-svg-loader`, please, let the community know by creating an issue :p
30
+ > The [docs website](https://matafokka.github.io/vite-awesome-svg-loader/) is the central knowledge base for `vite-awesome-svg-loader`.
397
31
 
398
- ### How can I help?
32
+ ### Loader
399
33
 
400
- 1. Create an integration with your favorite framework.
401
- 1. Add new functionality to the loader itself.
402
- 1. Report bugs.
403
- 1. Create feature requests by creating an issue.
34
+ - [Home](https://matafokka.github.io/vite-awesome-svg-loader/)
35
+ - [Getting started](https://matafokka.github.io/vite-awesome-svg-loader/guides/getting-started/)
36
+ - [Configuration](https://matafokka.github.io/loader-api-reference/interfaces/svgloaderoptions/)
404
37
 
405
- ### I want to participate in vite-awesome-svg-loader development. Where do I start?
38
+ ### Framework integrations, examples and demos
406
39
 
407
- Start by reading [README_DEV.md](https://github.com/matafokka/vite-awesome-svg-loader/tree/main/README_DEV.md). Then go on and do your thing :)
40
+ - [React](https://matafokka.github.io/vite-awesome-svg-loader/react/quick-start/)
41
+ - [Vue](https://matafokka.github.io/vite-awesome-svg-loader/vue/quick-start/)
42
+ - [Vanilla JS](https://matafokka.github.io/vite-awesome-svg-loader/vanilla-js/quick-start/)
43
+ - [Custom framework](https://matafokka.github.io/vite-awesome-svg-loader/guides/custom-integrations/)