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 🙏

© 2025 – Pkg Stats / Ryan Hefner

gulp-embed-svg

v1.4.13

Published

Gulp plugin to embed/inline svg images and optionally create a spritesheet.

Downloads

830

Readme

gulp-embed-svg

NPM

npm version CircleCI

Gulp plugin to inline/embed SVG images into html files.

Features

  • Inline/embed any images with an SVG source attribute (i.e. <img src="some.svg">) and <svg> tags with a src attribute (i.e. <svg src="some.svg">).

  • Preserves all/select attributes via RegEx.

  • Optionally create a spritesheet from the inlined SVGs.

Installation

npm i --save-dev gulp-embed-svg

Quick Start

const embedSvg = require('gulp-embed-svg');

gulp.task('embedSvgs', () =>
  gulp.src('*.html')
    .pipe(embedSvg())
    .pipe(gulp.dest('dist/')));

This gulp task will inline/embed any images with an SVG source attribute (i.e. <img src="some.svg">) and <svg> tags with a src attribute.

Options

  • attrs: Provide a regular expression to transfer select attributes from matched tags to embedded <svg>s.
  • createSpritesheet: Set to true to embed SVGs via a spritesheet. This reduces generated HTML filesize if you use the same SVG several times on a page.
  • decodeEntities: Set to true to decode HTML entities within the document.
  • root: Provide the root folder where SVG source images are located.
  • selectors: Provide custom CSS selectors to specify which tags should be replaced by embedded SVGs.
  • spriteIdFn: Customize the id assigned to the sprites by providing a function that resolves path and index to a string.
  • spritesheetClass: Customize the CSS class assigned to the generated spritesheet.
  • xmlMode: Whether or not to read files in xml mode.

selectors string | Array<string>

Provide custom CSS selectors to specify which tags should be replaced by embedded SVGs.

default: ['img[src$=".svg"]', 'svg[src$=".svg"]']

All <img> and <svg> tags with an svg source.

Example: Only embed tags with a specific class

HTML layout

<html>
  <head><!-- ... --></head>
  <body>
    <!-- ... -->
    <svg src="github-icon.svg" class="inline-svg"></svg>
    <img src="other-icon.svg" />
    <!-- ... -->
  </body>
</html>

Gulp task

const embedSvg = require('gulp-embed-svg');

gulp.task('embedSvgs', () =>
  gulp.src('*.html')
    .pipe(embedSvg({
      selectors: '.inline-svg' // only replace tags with the class inline-svg
    }))
    .pipe(gulp.dest('dist/')));

Output

<html>
  <head><!-- ... --></head>
  <body>
    <!-- ... -->
    <svg class="inline-svg"><!-- svg markup from github-icon.svg --></svg>
    <img src="other-icon.svg" />
    <!-- ... -->
  </body>
</html>

attrs string | RegExp

Provide a regular expression to transfer select attributes from matched tags to embedded <svg>s.

Attention: Attributes from matched tags take precedence over corresponding attributes in the source .svg file.

default: ^(?!src).*$

Transfer/preserve any attribute but src.

Example: Preserve/transfer specific attribute

HTML layout

<html>
<head><!-- ... --></head>
<body>
  <!-- ... -->
  <svg src="github-icon.svg" class="icon"></svg>
  <!-- ... -->
</body>
</html>

Gulp task

const embedSvg = require('gulp-embed-svg');

gulp.task('embedSvgs', () =>
  gulp.src('*.html')
    .pipe(embedSvg({
      attrs: /class/ // only transfer/preserve class attribute
    }))
    .pipe(gulp.dest('dist/')));

Output

<html>
  <head><!-- ... --></head>
  <body>
    <!-- ... -->
    <svg class="icon"><!-- svg markup from github-icon.svg --></svg>
    <!-- ... -->
  </body>
</html>

decodeEntities boolean

Set to true to decode HTML entities within the document.

default: false

Example: Replace potential entities in document with html entities

HTML layout

<html>
  <head><!-- ... --></head>
  <body>
    <!-- ... -->
    <span>Foo © bar 𝌆 baz ☃ qux</span>
    <svg src="github-icon.svg"></svg>
    <!-- ... -->
  </body>
</html>

Gulp task

const embedSvg = require('gulp-embed-svg');

gulp.task('embedSvgs', () =>
  gulp.src('*.html')
    .pipe(embedSvg({
      decodeEntities: true
    }))
    .pipe(gulp.dest('dist/')));

Output

<html>
  <head><!-- ... --></head>
  <body>
    <!-- ... -->
    <span>Foo &#xA9; bar &#x1D306; baz &#x2603; qux</span>
    <svg class="icon"><!-- svg markup from github-icon.svg --></svg>
    <!-- ... -->
  </body>
</html>

root string

Provide the root folder where SVG source images are located.

default: __dirname

The folder in which the task is executed.

Example: Alternate svg root

HTML layout

<html>
  <head><!-- ... --></head>
  <body>
    <!-- ... -->
    <svg src="github-icon.svg"></svg>
    <!-- ... -->
  </body>
</html>

Folder structure

  /src
    index.html
    gulpfile.js
    /assets
      github-icon.svg

Gulp task

const embedSvg = require('gulp-embed-svg');

gulp.task('embedSvgs', () =>
  gulp.src('*.html')
    .pipe(embedSvg({
      root: './assets'
    }))
    .pipe(gulp.dest('dist/')));

createSpritesheet boolean

Set to true to embed SVGs via a spritesheet. This reduces generated HTML filesize if you use the same SVG several times on a page.

default: false

Attention: If your SVGs contain gradients, please make sure their respective id attribute values are unique among all gradients that are embedded on your page. The reason behind this is that, in order to support most (if not all) browsers, we need to extract gradient definitions from the SVGs and save them separately in the spritesheet. If there are duplicate gradient ids, the browser is having a hard time determining which one to use.

Example: Create an svg spritesheet

HTML Layout

<html>
  <head><!-- ... --></head>
  <body>
    <!-- ... -->
    <svg src="github-icon.svg"></svg>
    <!-- ... -->
    <svg src="github-icon.svg"></svg>
    <!-- ... -->
  </body>
</html>

Folder structure

  /src
    index.html
    gulpfile.js
    /assets
      github-icon.svg

Gulp task

const embedSvg = require('gulp-embed-svg');

gulp.task('embedSvgs', () =>
  gulp.src('*.html')
    .pipe(embedSvg({
      root: './assets',
      createSpritesheet: true
    }))
    .pipe(gulp.dest('dist/')));

Output

<html>
  <head><!-- ... --></head>
  <body>
    <svg class="svg-sprites">
      <symbol id="svg-sprite-0">
        <!-- source of github-icon.svg -->
      </symbol>
    </svg>
    <!-- ... -->
    <svg>
      <use xlink:href="#svg-sprite-0">
    </svg>
    <!-- ... -->
    <svg>
      <use xlink:href="#svg-sprite-0">
    </svg>
    <!-- ... -->
  </body>
</html>

spritesheetClass string

Customize the CSS class assigned to the generated spritesheet.

default: svg-sprites

Example: Change spritesheet class to my-sprites

HTML Layout

<html>
  <head><!-- ... --></head>
  <body>
    <!-- ... -->
    <svg src="github-icon.svg"></svg>
    <!-- ... -->
    <svg src="github-icon.svg"></svg>
    <!-- ... -->
  </body>
</html>

Folder structure

  /src
    index.html
    gulpfile.js
    /assets
      github-icon.svg

Gulp task

const embedSvg = require('gulp-embed-svg');

gulp.task('embedSvgs', () =>
  gulp.src('*.html')
    .pipe(embedSvg({
      root: './assets',
      createSpritesheet: true,
      spritesheetClass: 'my-sprites'
    }))
    .pipe(gulp.dest('dist/')));

Output

<html>
  <head><!-- ... --></head>
  <body>
    <svg class="my-sprites">
      <symbol id="svg-sprite-0">
        <!-- source of github-icon.svg -->
      </symbol>
    </svg>
    <!-- ... -->
    <svg>
      <use xlink:href="#svg-sprite-0">
    </svg>
    <!-- ... -->
    <svg>
      <use xlink:href="#svg-sprite-0">
    </svg>
    <!-- ... -->
  </body>
</html>

spriteIdFn function

Customize the id assigned to the sprites by providing a function that resolves path and index to a string. The function receives the absolute/resolved path to the source SVG file as well as the index of the sprite within the page as parameters.

default: (path, i) => `svg-sprite-${i}`

Example: Use the filename as id

HTML Layout

<html>
  <head><!-- ... --></head>
  <body>
    <!-- ... -->
    <svg src="github-icon.svg"></svg>
    <!-- ... -->
    <svg src="javascript-icon.svg"></svg>
    <!-- ... -->
  </body>
</html>

Folder structure

  /src
    index.html
    gulpfile.js
    /assets
      github-icon.svg
      javascript-icon.svg

Gulp task

const embedSvg = require('gulp-embed-svg');
const basename = require('path').basename;

gulp.task('embedSvgs', () =>
  gulp.src('*.html')
    .pipe(embedSvg({
      root: './assets',
      createSpritesheet: true,
      spriteIdFn: (path, i) => basename(path, '.svg')
    }))
    .pipe(gulp.dest('dist/')));

Output

<html>
  <head><!-- ... --></head>
  <body>
    <svg class="svg-sprites">
      <symbol id="github-icon">
        <!-- source of github-icon.svg -->
      </symbol>
      <symbol id="javascript-icon">
        <!-- source of javascript-icon.svg -->
      </symbol>
    </svg>
    <!-- ... -->
    <svg>
      <use xlink:href="#github-icon">
    </svg>
    <!-- ... -->
    <svg>
      <use xlink:href="#javascript-icon">
    </svg>
    <!-- ... -->
  </body>
</html>

xmlMode boolean

Flag to toggle xml mode.

default: true

Changelog

License