@jebka/webpack-svg-sprite-loader
v1.1.7
Published
Load SVGs into webpack and split them into sprites at will
Downloads
19
Maintainers
Readme
@jebka/webpack-svg-sprite-loader
Loader for webpack, that helps you split SVGs into sprites based on the SVG name and the entry point. This plugins should be useful for React, React-like and JSX apps.
If you're using Next.js, you might want to check out @jebka/next-svg-sprite-loader.
Why
On a large UI project, you might get into situation with hundreds of SVG icons. Inlining SVGs is performance costly, however, embedding hundreds of icons into a single SVG will be bandwidth costly.
This loader let's you decide how to optimize your SVGs.
Install
npm install --save-dev @jebka/webpack-svg-sprite-loader
yarn add -D @jebka/webpack-svg-sprite-loader
Configure
defaultBehavior
The default behaviour applies to .svg
files without recognizable prefix. By
default, the loader will put these into the Global Shared
Sprite, but you can change this.
global | shared | module | entry | raw | inline
optimize
Turn on or off SVG optimization By default on.
boolean
outputFolder
Folder that will contain built sprites. Default: 'sprites'
string
typeDetector
Override detection of the SVG import type. See defaultBehavior for allowed values.
(resourcePath: string) => SpriteType
symbolParser
Override the default symbol ID parsing function
(resourcePath: string, rootPath: string) => string
Plug in
Put the plugin in your Webpack plugins. All options are optional and defaults should be good enough for most projects.
import SvgSpriteLoader from "@jebka/webpack-svg-sprite-loader";
{
// ...
plugins: [
new SvgSpriteLoader({
// options
}),
];
// ...
}
And this in your webpack loaders
{
// ...
loaders: [
{
test: /\.svg$/,
loader: "@jebka/webpack-svg-sprite-loader",
},
];
// ...
}
Imports
All the import methods can be combined with single loader. If no import method is specified, the loader will fall back to defaultBehaviour setting.
Global Shared Sprite
Name your file with .shared.svg
suffix. This will serve your SVG from a
single global sprite. This is the preferred way of importing SVGs, when you're
dealing with small amount of icons and you do not need extra features.
import HelloWorld from './hello-world.shared.svg`
Module Sprite
Name your file with .module.svg
suffix. This will serve your SVG from a sprite
generated from a barel file. This kind of optimization is useful for apps with
large amount of icons, where some are to features or components.
The SVG will get duplicated if it is imported from multiple files.
import HelloWorld from './hello-world.module.svg`
Entrypoint Sprite
Name your file with .entry.svg
suffix. This will serve your SVG from a sprite
generated for the entrypoint. This kind of optimization is useful for apps with
large amount of icons, where some are specific to views/pages.
The SVG will get duplicated if it is imported from multiple files.
import HelloWorld from './hello-world.entry.svg`
Inline Import
Name your file with .inline.svg
suffix. This will return JSX Component.
import HelloWorld from './hello-world.inline.svg`
Raw Import
Name your file with .inline.svg
suffix. This will return SVG string
import HelloWorld from './hello-world.inline.svg`
console.log(HelloWorld)
// '<svg>...</svg>'
TypeScript
You will need to cast your SVG imports at your own. Create a svg.d.ts
file
in your project:
import type { SvgSymbolImport } from '@jebka/webpack-svg-sprite-loader';
// based on your import paths
declare module '*.module.svg' {
const content: SvgSymbolImport;
export default content;
}
Caveats
This plugin may break down in the future, because it is using the forbidden webpack loader API.