vite-awesome-svg-loader 2.1.0 → 3.0.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 CHANGED
@@ -13,401 +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
- // Get classes and IDs prefix to target DOM elements later
85
- import imageSrc, { prefix as imagePrefix } from "./path/to/image.svg";
86
-
87
- // or:
88
- import { src as imageSrc, prefix as imagePrefix } from "./path/to/image.svg";
89
- ```
90
-
91
- 5. Optional, but highly recommended. Configure loader, so you can import SVGs without URL parameters:
92
-
93
- ```ts
94
- viteAwesomeSvgLoader({
95
- // Set default import type. "source" is the default value.
96
- //
97
- // Available values:
98
- // source - load SVG source code.
99
- // url - load URL pointing to the SVG file. Loader will generate that file for you.
100
- // source-data-uri - Source code put into a data URI.
101
- // base64 - SVG source code encoded in base64.
102
- // base64-data-uri - SVG source code in base64 put into a data URI.
103
- defaultImport: "source",
104
-
105
- // A list of files or directories to preserve line width of.
106
- preserveLineWidthList: [/config-demo\/preserve-line-width\//, /config-demo\/all\//],
107
-
108
- // A list of files to skip while preserving line width. Overrides preserveLineWidthList.
109
- skipPreserveLineWidthList: [/line-width-not-preserved\.svg/],
110
-
111
- // A list of files or directories to preserve color of
112
- replaceColorsList: [
113
- // File names
114
- "some-file.svg",
115
-
116
- // Regexes that are checked against whole path and file name with extension
117
- /config-demo\/set-current-color\//,
118
- /config-demo\/all\//,
119
-
120
- // Map of color replacements. Key is an original color, value is its replacement. Both can be any values:
121
- // HEX, name, rgb() or arbitrary custom values. Applied to all files.
122
- {
123
- "#003147": "red",
124
- "rgb(0, 49, 71)": "#003147",
125
- "myCustomColor": "var(--some-color-var)",
126
- },
127
-
128
- // Map of color replacements per files
129
- {
130
- files: ["vars.svg"], // File names or regexes, same format as above
131
-
132
- // Replacements, same format as above
133
- replacements: {
134
- red: "var(--primary-color)",
135
- green: "var(--secondary-color)",
136
- blue: "var(--tertiary-color)",
137
- },
138
-
139
- // Default value for colors that are not in replacements map. Set an empty string to preserve original colors.
140
- // Default value is "currentColor",
141
- default: "currentColor"
142
- },
143
- ],
144
-
145
- // A list of files to skip while replacing colors. Overrides replaceColorsList.
146
- skipReplaceColorsList: [/colors-not-preserved\.svg/],
147
-
148
- // A list of files to skip while transforming. File skip-transforms.svg is present in every directory.
149
- skipTransformsList: [/skip-transforms\.svg/, /ignore-elements-orig\.svg/],
150
-
151
- // A list of files to skip loading of. File skip-loading.svg is present in every directory.
152
- skipFilesList: [/skip-loading\.svg/],
153
-
154
- // A list of selectors to skip while preserving line width
155
- skipPreserveLineWidthSelectors: [
156
- // It can be a list of CSS selectors like this one. Every element in every file will be checked against it.
157
- '*[data-original-line-width="true"], *[data-original-line-width="true"] *',
158
-
159
- // Or it can be configured on per-file basis:
160
- {
161
- files: [/ignore-elements\.svg/, /some-other-file\.svg/],
162
- selectors: ['*[data-original-line-width="true"], *[data-original-line-width="true"] *'],
163
- },
164
- ],
165
-
166
- // These options are not recommended due to architectural and performance reasons (see JSDoc):
167
-
168
- // A list of selectors to skip while replacing colors. Same format as above.
169
- skipReplaceColorsSelectors: ['*[data-original-color="true"], *[data-original-color="true"] *'],
170
-
171
- // A list of selectors to skip while transforming. Same format as above.
172
- skipTransformsSelectors: ['*[data-no-transforms="true"], *[data-no-transforms="true"] *'],
173
- })
174
- ```
175
-
176
- 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).
177
-
178
- ## Integrations
179
-
180
- All integrations work only on client side. However, they won't break SSR.
181
-
182
- ### Vanilla JS
183
-
184
- **Warning**: do NOT use this package to create custom integrations, use `vite-awesome-svg-loader/integration-utils` instead.
185
-
186
- Make sure to check the [docs](https://matafokka.github.io/vite-awesome-svg-loader/vanilla-integration-docs).
187
-
188
- 1. Import classes:
189
-
190
- ```ts
191
- import {
192
- SvgImage, // Basically implements SVG sprites
193
- SvgIcon, // Basic SVG icon that uses SvgImage class internally
194
- } from "vite-awesome-svg-loader/vanilla-integration";
195
- ```
196
-
197
- 2. Import images:
198
-
199
- ```ts
200
- import imageSrc from "@/path/to/image.svg";
201
- ```
202
-
203
- 3. Use classes:
204
-
205
- ```ts
206
- new SvgImage(imageSrc, "#my-container"); // Create an image and mount it to the element with "my-container" id.
207
-
208
- new SvgIcon(imageSrc, "#my-container") // Create an icon and mount it to the element with "my-container" id.
209
- .setSize("24px") // Set 24px size
210
- .setColor("red") // Set red color
211
- .setColorTransition("0.3s ease-out"); // Set color transition
212
- ```
213
-
214
- 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).
215
-
216
- ### Vue 3
217
-
218
- 1. Import components:
219
-
220
- ```ts
221
- import {
222
- SvgImage, // Basically implements SVG sprites
223
- SvgIcon, // Basic SVG icon that uses SvgImage component internally
224
- } from "vite-awesome-svg-loader/vue-integration";
225
- ```
226
-
227
- 2. Import images:
228
-
229
- ```ts
230
- import imageSrc from "@/path/to/image.svg";
231
- ```
232
-
233
- 3. Use components:
234
-
235
- ```vue
236
- <template>
237
- <div class="main">
238
- <SvgImage :src="imageSrc" />
239
-
240
- <SvgIcon
241
- :src="imageSrc"
242
- size="24px"
243
- color="red"
244
- color-transition="0.3s ease-out"
245
- />
246
- </div>
247
- </template>
248
- ```
249
-
250
- 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).
251
-
252
- ### React
253
-
254
-
255
- 1. Import components:
256
-
257
- ```ts
258
- import {
259
- SvgImage, // Basically implements SVG sprites
260
- SvgIcon, // Basic SVG icon that uses SvgImage component internally
261
- } from "vite-awesome-svg-loader/react-integration";
262
- ```
263
-
264
- 2. Import images:
265
-
266
- ```ts
267
- import imageSrc from "@/path/to/image.svg";
268
- ```
269
- 3. Use components:
270
-
271
- ```tsx
272
- export function MyComponent() {
273
- return (
274
- <div class="main">
275
- <SvgImage src={imageSrc} />
276
-
277
- <SvgIcon
278
- src={imageSrc}
279
- size="24px"
280
- color="red"
281
- colorTransition="0.3s ease-out"
282
- />
283
- </div>
284
- )
285
- }
286
- ```
287
-
288
- 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).
289
-
290
- ### Custom integration
291
-
292
- 1. Import helper functions and styles:
293
-
294
- ```ts
295
- import "integration-utils/styles.css";
296
-
297
- // There function will run only on client side. However, they won't break SSR.
298
-
299
- import {
300
- onSrcUpdate, // Should be called when component is mounted, and source code is updated
301
- onUnmount, // Should be called when component is unmounted
302
- } from "integration-utils";
303
- ```
304
-
305
- 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.
306
-
307
- 3. Please, submit your integration in a PR. Or just create an issue and provide the source code if you don't have time
308
- or don't want to deal with this project's development process.
309
-
310
- ## Additional information
311
-
312
- ### What are loading/import types (url, source, etc)?
313
-
314
- Let's see how we can load SVGs, and what tradeoffs do they have:
315
-
316
- 1. Put whole SVG source code into the DOM every time image is displayed. This is the most popular method.
317
- 1. Advantages:
318
- 1. Simplicity.
319
- 1. Great customization. This is actually the only option, if you need to manipulate the content, or need more than just CSS styles.
320
- 1. Disadvantages:
321
- 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.
322
- 1. Link an image like so: `<img href="/image.svg">` or `background-image: url("/image.svg")`.
323
- 1. Advantages:
324
- 1. Simplicity.
325
- 1. Great performance thanks to instancing.
326
- 2. Disadvantages:
327
- 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._
328
- 1. Symbols: put all images into a single `<svg>` node and wrap each image with a `<symbol>` element.
329
- 1. Advantages:
330
- 1. Performance is way better than when using whole SVG each time.
331
- 1. Images can be customized via CSS, for example, you can set custom stroke and fill colors.
332
- 1. Disadvantages:
333
- 1. Requires a bit of maintenance in form of using provided integrations.
334
- 1. Performance is worse than when linking an image.
335
- 1. Customization is not that great when using whole SVG source code.
336
- 1. Data URI: either use source code or base64 in a [data URI](https://en.wikipedia.org/wiki/Data_URI_scheme).
337
- 1. Advantages:
338
- 1. Great performance, although a tiny bit worse than when linking an image.
339
- 2. Disadvantages:
340
- 1. Lack of any customization, good only for static images.
341
- 2. Increased memory and traffic usage when linking images.
342
-
343
- To summarize:
344
-
345
- 1. If you don't need customization, i.e. you want to display static images, use linking method.
346
- 1. If you only need CSS customization, use symbols. This is preferred option for icons and line art that's needed to be customized.
347
- 1. If you need heavy customization and interactivity, put whole SVG source code into the DOM.
348
- 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.
349
-
350
- ### Can this loader put whole SVG source code every time I use an image?
351
-
352
- Yes, load an image as a source code and insert that code into the DOM like shown in the demos.
353
-
354
- ### Should I always import my images like shown in the examples? Can I simplify this?
355
-
356
- Yes, use `import.meta.glob()` like shown in the demos.
357
-
358
- Such components are not present in the integrations because glob imports do not support variable interpolation.
359
- See: https://vitejs.dev/guide/features#glob-import
360
-
361
- **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.
362
-
363
- ### For library authors
364
-
365
- If your icon set is not extensible, just add `vite-awesome-svg-loader` and build your library.
366
-
367
- If your icon set is extensible, you'll need to:
368
-
369
- 1. Provide a plugin that wraps `vite-awesome-svg-loader` and sets your settings.
370
- 1. Tell your users to use your plugin, import icons as source code and pass it to your components.
371
-
372
- ### Comparison with other loaders
373
-
374
- **[vite-svg-loader](https://github.com/jpkleemans/vite-svg-loader)**
375
-
376
- Implements a portion `vite-awesome-svg-loader` functionality. This loader can also import SVGs as Vue components.
377
-
378
- 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):
379
-
380
- ```vue
381
- <template>
382
- <div v-html="imageSrc" />
383
- </template>
384
-
385
- <script lang="ts" setup>
386
- import imageSrc from "./path/to/image.svg";
387
- </script>
24
+ ```sh
25
+ npm i vite-awesome-svg-loader
388
26
  ```
389
27
 
390
- Similar approach can be applied to any other framework or vanilla JS.
391
-
392
- **[@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**
393
-
394
- Same as above, but for other frameworks.
395
-
396
- **[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)**
397
-
398
- Implements a portion `vite-awesome-svg-loader` functionality.
399
-
400
- **Other loaders and plugins**
28
+ ## Docs
401
29
 
402
- 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`.
403
31
 
404
- ### How can I help?
32
+ ### Loader
405
33
 
406
- 1. Create an integration with your favorite framework.
407
- 1. Add new functionality to the loader itself.
408
- 1. Report bugs.
409
- 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/)
410
37
 
411
- ### I want to participate in vite-awesome-svg-loader development. Where do I start?
38
+ ### Framework integrations, examples and demos
412
39
 
413
- 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/)