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

gulp-svg-sprites

v4.1.2

Published

Create SVG sprites with PNG fallbacks

Downloads

9,496

Readme

gulp-svg-sprites Build Status

Table of contents

Install

Install it locally to your project.

$ npm install --save-dev gulp-svg-sprites

Windows note: Using Version < 4.0.0, make sure you also have all prerequisites for node-gyp.

Usage

With no configuration, gulp-svg-sprites will create the following files:

  1. svg/sprite.svg - Sprite Sheet containing all of your SVGs
  2. sprite.html - A preview page with instructions & snippets
  3. css/sprite.css - A CSS file with the code needed to use the sprite
var svgSprite = require("gulp-svg-sprites");

gulp.task('sprites', function () {
    return gulp.src('assets/svg/*.svg')
        .pipe(svgSprite())
        .pipe(gulp.dest("assets"));
});

Then, if you had a facebook.svg file, you'd be able to use the following markup in your webpage:

<i class="icon facebook"></i>

PNG fallback

You can easily support old browsers by piping the new SVG sprite through to another gulp task. There will be a no-svg class generated automatically in the CSS, so you'll just need to use something like Modernizr to set the no-svg class on the <body> tag of your website.

var svgSprite = require("gulp-svg-sprites");
var filter    = require('gulp-filter');
var svg2png   = require('gulp-svg2png');

gulp.task('sprites', function () {
    return gulp.src('assets/svg/*.svg')
        .pipe(svgSprite())
        .pipe(gulp.dest("assets")) // Write the sprite-sheet + CSS + Preview
        .pipe(filter("**/*.svg"))  // Filter out everything except the SVG file
        .pipe(svg2png())           // Create a PNG
        .pipe(gulp.dest("assets"));
});

Symbols mode

Pass mode: "symbols" to output SVG data as this CSS Tricks article outlines. You'll get an SVG file and a preview file showing how to use it.

gulp.task('sprites', function () {
    return gulp.src('assets/svg/*.svg')
        .pipe(svgSprite({mode: "symbols"}))
        .pipe(gulp.dest("assets"));
});

Defs mode

Pass mode: "defs" to output SVG data as this CSS Tricks article outlines. You'll get an SVG file and a preview file showing how to use it.

gulp.task('sprites', function () {
    return gulp.src('assets/svg/*.svg')
        .pipe(svgSprite({mode: "defs"}))
        .pipe(gulp.dest("assets"));
});

Custom selectors

By default, the filename will be used as the selector in the CSS, but this is how you'd override it (the %f will be replaced with the filename):

gulp.task('sprites', function () {
    return gulp.src('assets/svg/*.svg')
        .pipe(svgSprite({
            selector: "icon-%f"
        }))
        .pipe(gulp.dest("assets"));
});

Custom IDs

With the symbols or defs mode, it's probably the ID you'll want to override. No problem.

gulp.task('sprites', function () {
    return gulp.src('assets/svg/*.svg')
        .pipe(svgSprite({
            svgId: "svg-%f"
        }))
        .pipe(gulp.dest("assets"));
});

Custom filenames

You can also change the generated filenames with ease. For example, if you want to create a scss partial instead, you could just do:

// Custom CSS filename
gulp.task('sprites', function () {
    return gulp.src('assets/svg/*.svg')
        .pipe(svgSprite({
            cssFile: "scss/_sprite.scss"
        }))
        .pipe(gulp.dest("assets"));
});

// Custom SVG filename
gulp.task('sprites', function () {
    return gulp.src('assets/svg/*.svg')
        .pipe(svgSprite({
            svg: {
                sprite: "svg.svg"
            }
        }))
        .pipe(gulp.dest("assets"));
});

// Custom preview filename + custom SVG filename
gulp.task('sprites', function () {
    return gulp.src('assets/svg/*.svg')
        .pipe(svgSprite({
            svg: {
                sprite: "svg.svg"
            },
            preview: {
                sprite: "index.html"
            }
        }))
        .pipe(gulp.dest("assets"));
});

Base size

Set the font-size of the .icon class. Just pass a plain number, no units.

gulp.task('sprites', function () {
    return gulp.src('assets/svg/*.svg')
        .pipe(svgSprite({
            baseSize: 16
        }))
        .pipe(gulp.dest("assets"));
});

No previews

If you don't want 'em. Works in all modes.

gulp.task('sprites', function () {
    return gulp.src('assets/svg/*.svg')
        .pipe(svgSprite({
            preview: false
        }))
        .pipe(gulp.dest("assets"));
});

Using the built-in SCSS template

gulp.task('sprites', function () {
    return gulp.src('assets/svg/*.svg')
        .pipe(svgSprite({
            templates: { scss: true }
        }))
        .pipe(gulp.dest("assets"));
});

Advanced: custom templates

Templates use Lodash Templates - check out their docs for usage instructions. Or take a look at the default CSS or the default SCSS for tips.

You can get your hands on JUST the SVG Data and provide your own templates. For example, if you want to provide your own template for the CSS output, you could do this:

var config = {
    templates: {
        css: require("fs").readFileSync("./path/to/your/template.css", "utf-8")
    }
};

gulp.task('sprites', function () {
    return gulp.src('assets/svg/*.svg')
        .pipe(svgSprite(config))
        .pipe(gulp.dest("assets"));
});

You can override all the templates used in the same way.

Advanced: data transforms

If you want to do some custom stuff with your templates, you might need to transform the SVG data before it gets to your template. There are two functions you can provide to do this and they'll override the internal ones. Override transformData and you'll have direct access to the data returned from svg-sprite-data. This will skip the few transformations that this library applies - so use with caution. (If you want to modify the data as well after our internal modifications, use afterTransform instead.)


// Synchronous
var config = {
    transformData: function (data, config) {
        return data; // modify the data and return it
    },
    templates: {
        css: require("fs").readFileSync("./path/to/your/template.css", "utf-8")
    }
};

// Asynchronous
var config = {
    asyncTransforms: true,
    transformData: function (data, config, done) {
        done(data); // modify the data and pass it
    },
    templates: {
        css: require("fs").readFileSync("./path/to/your/template.css", "utf-8")
    }
};

gulp.task('sprites', function () {
    return gulp.src('assets/svg/*.svg')
        .pipe(svgSprite(config))
        .pipe(gulp.dest("assets"));
});

You can override all the templates used here in the same way. If you are doing any async work in these callbacks set asyncTransforms to true in the config.

Options

License

Copyright (c) 2017 Shane Osbourne

Licensed under the MIT license.