npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jebka/webpack-svg-sprite-loader

v1.1.7

Published

Load SVGs into webpack and split them into sprites at will

Downloads

21

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.