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-sprinter-html-webpack

v2.0.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

4

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

Original creator @Epimodev. Only adds improvements (such as ability to generate SVG spritesheets on demand without using imports. Also fixes the .tap deprecation warning and uses the new Hooks API. Credits to: Epimodev/svg-sprite-html-webpack :thumbsup::thumbsup::thumbsup:.

Advantages :

  • sprite injected at compilation time instead of browser run time
  • svg automaticaly optimize without other loader
  • adds the ability to include svg folder

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).

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>
);

Generate plain svg spritesheet without any JS/ JSX imports

// In webpack.config file
  ...
  plugins: [
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: 'index.html',
    }),
    new SvgSpriteHtmlWebpackPlugin({
      includeFiles: [
        'path/to/file1.svg', // include as many files as you want
      ]
    })
    ...

In html you can declare it anywhere as so:

  <svg>
    <use xlinkHref='#0' />
  </svg>

Note: By default the plugin generates numeric IDs for the SVG sprites. This may be quite hard to use in the real world, so it's better to use generateSymbolId() function to rewrite the svg id.

SvgSpriteHtmlWebpackPlugin options

(optional) includeFiles: ['file.svg']

Property that expects an array object of strings that reflect the path to the svgs, which will be appened in the stylesheet. Usefull when you do not directly want to include svgs in JS but want to use them in your markup

(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({
      includeFiles: [
        'path/to/file1.svg',
        'path/to/file2.svg',
        'path/to/file3.svg',
      ],
    }),
    ...
  ]
}

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();
      },
    }),
    ...
  ]
}