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

svg-sprite-html-webpack

v2.4.1

Published

Webpack loader and plugin to generate a SVG sprite with <symbol> elements and inject it in html built by html-webpack-plugin

Downloads

866

Readme

SVG sprite html webpack loader and plugin

Webpack loader and plugin to generate a SVG sprite with <symbol> elements and inject it in html built by html-webpack-plugin

Advantages :

  • sprite injected at compilation time instead of browser run time
  • svg automaticaly optimize without other loader

Why inject SVG sprite in HTML instead of using an external file ?

Each time we inject an element <use xlink:href="file.svg#icon" /> in the DOM, a request is launch to get "file.svg". So the icon appear with a delay depending on http response time. If you have an animation when svg symbol is injected in DOM, the icon will appear in the middle of animation.

The goal of this plugin + loader is to avoid those http requests when injecting symbol reference in the DOM.

Installation

yarn add -D svg-sprite-html-webpack

# or with npm

npm install -D svg-sprite-html-webpack

Compatibility

  • This plugin works with Webpack v3 or Webpack v4.
  • This plugin works only with html-webpack-plugin (v2 or v3).
  • Webpack v5 and html-webpack-plugin v5 compatibilty has been added by contributors #21 and the plugin is no longer maintained by the author.

How to use

Webpack configuration :

Warning: Since Webpack 4, SvgSpriteHtmlWebpackPlugin must be declare after HtmlWebpackPlugin

const HtmlWebpackPlugin = require('html-webpack-plugin');
const SvgSpriteHtmlWebpackPlugin = require('svg-sprite-html-webpack');

const webpackConfig = {
  ...
  module: {
    rules: [
      ...
      {
        test: /\.svg$/,
        exclude: /node_modules/,
        use: SvgSpriteHtmlWebpackPlugin.getLoader(),
      },
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: 'index.html',
    }),
    new SvgSpriteHtmlWebpackPlugin(),
    ...
  ]
}

Code in your browser app

import checkmark from './icons/checkmark.svg';

const rendered = `
<svg>
  <use xlink:href="${checkmark}" />
</svg>`;

In JSX

import React from 'react';
import checkmark from './icons/checkmark.svg';

const icon = (
  <svg>
    <use xlinkHref={checkmark} />
  </svg>
);

SvgSpriteHtmlWebpackPlugin options

(optional) append: boolean

Determines whether the svg string should be inserted before or after the content in the body tag.

example (default behaviour):

const webpackConfig = {
  ...
  new SvgSpriteHtmlWebpackPlugin({
    append: false
  }),
  ...

Code in your html file :

<body>
<svg>...</svg>
...
</body>

reverse example:

const webpackConfig = {
  ...
  new SvgSpriteHtmlWebpackPlugin({
    append: true
  }),
  ...

Code in your html file :

<body>
...
<svg>...</svg>
</body>

(optional) includeFiles: string[]

List of file path to include without javacript import. You can use "glob" pattern to include a list of files in a folder (more details here: https://github.com/isaacs/node-glob). Files path is relative from the path where webpack is launched.

By default symbol id generated will be the name of svg file without extension. If you include several folder which includes files with the same name, you can use generateSymbolId option to generate symbol id depending of file path or content hash.

Note: you can import with javascript a file already included with this option. It will use the id of the included file without inject svg in sprite twice.

Warning: if you include 2 files with different name but with exactly the same content, only one file will be injected in svg sprite.

example :

const HtmlWebpackPlugin = require('html-webpack-plugin');
const SvgSpriteHtmlWebpackPlugin = require('svg-sprite-html-webpack');

const webpackConfig = {
  ...
  plugins: [
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: 'index.html',
    }),
    new SvgSpriteHtmlWebpackPlugin({
      includeFiles: [
        'src/icons/*.svg',
      ],
    }),
    ...
  ]
}

Code in your html file :

<svg>
  <!-- if there is a file named `checkmark.svg` in includePaths -->
   <use xlink:href="#checkmark"></use>
</svg>

(optional) generateSymbolId: (svgFilePath: string, svgHash: number, svgContent: string) => string

function which generate the symbol id of each svg imported.

example :

const HtmlWebpackPlugin = require('html-webpack-plugin');
const SvgSpriteHtmlWebpackPlugin = require('svg-sprite-html-webpack');

const webpackConfig = {
  ...
  plugins: [
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: 'index.html',
    }),
    new SvgSpriteHtmlWebpackPlugin({
      generateSymbolId: function(svgFilePath, svgHash, svgContent) {
        return svgHash.toString();
      },
    }),
    ...
  ]
}