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-svgfallback

v5.0.1

Published

Generate png sprite from svg icons

Downloads

170

Readme

gulp-svgfallback Build Status

Generate png sprite from svg sources.

  1. Runs phantomjs only once for all svg sources
  2. Doesn't create any temporary files
  3. Allows to use custom CSS template

Options

  • backgroundUrl — url of background image, is set to output png fileName by default;
  • cssTemplate — path to custom CSS lodash template;
  • spriteWidth — maximum width of the sprite, default: 400.

Automatic options:

  • css class of each icon is set to the name of corresponding file;
  • output png and css filenames are set to the name of base directory of the first file.

If your workflow is different, please use gulp-rename to rename sources or result files.

Usage

The following task will output icons.png and icons.css:

var svgfallback = require('gulp-svgfallback');
var gulp = require('gulp');

gulp.task('svgfallback', function () {
    return gulp
        .src('src/icons/*.svg', {base: 'src/icons'})
        .pipe(svgfallback())
        .pipe(gulp.dest('dest'));
});

Caveats

Please ensure that svg sources have width and height: these dimensions will be used to generate png sprite.

Custom css template

An additional option cssTemplate allows you to override template that is used to generate css. For more info, please check the default template in 'templates/style.css'.

Here is an example of data that is passed to css template:

{
    "backgroundUrl": "sprite.png",
    "icons": [
        {
            "name": "circle",
            "width": 40,
            "height": 40,
            "left": 0,
            "top": 0
        },
        {
            "name": "square",
            "width": 40,
            "height": 40,
            "left": 40,
            "top": 0
        }
    ]
}

Custom css classes

If you need to add prefix to each css class, please use gulp-rename:

var gulp = require('gulp');
var rename = require('gulp-rename');
var svgfallback = require('gulp-svgfallback');

gulp.task('default', function () {
    return gulp
        .src('src/icons/*.svg', {base: 'src/icons'})
        .pipe(rename({prefix: 'icon-'})
        .pipe(svgfallback())
        .pipe(gulp.dest('dest'));
});

Since css class for each icon should be unique, you cannot pass files with the same name. If you need to have nested directories that may have files with the same name, please use gulp-rename. The following example will concatenate relative path with the name of the file, e.g. src/icons/one/two/three/circle.svg becomes one-two-three-circle.

var gulp = require('gulp');
var rename = require('gulp-rename');
var svgfallback = require('gulp-svgfallback');

gulp.task('default', function () {
    return gulp
        .src('src/icons/**/*.svg', {base: 'src/icons'})
        .pipe(rename(function (path) {
            var name = path.dirname.split(path.sep);
            name.push(path.basename);
            path.basename = name.join('-');
        }))
        .pipe(svgfallback())
        .pipe(gulp.dest('dest'));
});

Add variations

To add variations (e.g. different colors) into your sprite, you can combine gulp-svgfallback with other gulp plugins.

The following task will add .circle-red and .circle-blue into your sprite.

var path = require('path');
var gulp = require('gulp');
var gulpif = require('gulp-if');
var lazypipe = require('lazypipe');
var clone = require('gulp-clone');
var cheerio = require('gulp-cheerio');
var rename = require('gulp-rename');
var svgfallback = require('gulp-svgfallback');

function isCircle (file) {
    return path.basename(file.relative) === 'circle.svg';
}

function colorize (color) {
    var sink;
    return (lazypipe()
        .pipe(function () {
            sink = clone.sink();
            return sink;
        })
        .pipe(cheerio, function ($) {
            $('svg').attr('fill', color);
        })
        .pipe(rename, {suffix: '-' + color})
        .pipe(function () {
            return sink.tap();
        })
    )();
}

gulp.task('svgfallback', function () {
    return gulp
        .src('src/icons/*.svg', {base: 'src/icons'})
        .pipe(gulpif(isCircle, colorize('red')))
        .pipe(gulpif(isCircle, colorize('blue')))
        .pipe(svgfallback())
        .pipe(gulp.dest('dest'));
});

Changelog

  • 5.0.1 Update lodash dep #14
  • 5.0.0 Remove gulp-util dep, drop node 0.12 support
  • 4.0.1 Update to PhantomJS 2, drop node 0.10 support
  • 3.0.2 Update direct dependencies
  • 3.0.1 Reduced dev dependencies
  • 2.0.0 Reduced amount of options, improved readme
  • 1.0.1 Added example of how to add variations
  • 1.0.0 Initial release