Skip to content

Instantly share code, notes, and snippets.

@jayperryworks
Last active August 29, 2015 14:11
Show Gist options
  • Save jayperryworks/34944196fabe8654c867 to your computer and use it in GitHub Desktop.
Save jayperryworks/34944196fabe8654c867 to your computer and use it in GitHub Desktop.
Create SVG sprites with Gulp, using gulp-svgstore
"use strict";
var gulp = require("gulp");
// useful file paths
var icons = {
src : "source/assets/images/icons",
dest : "source/assets/images"
};
var fileName = "spritemap.svg";
// tell me what the error is!
// -> prevent .pipe from dying on error w/ gulp-plumber
// -> and give more useful error messages
var showError = function(err) {
$.util.beep();
console.log(err);
};
// load plugins
var $ = require("gulp-load-plugins")();
gulp.task("default", ["icons"]);
gulp.task("icons", function() {
return gulp.src(icons.src + "/*.svg")
.pipe($.plumber({
errorHandler: showError
}))
.pipe($.svgmin())
.pipe($.svgstore({
fileName: fileName,
prefix: ""
}))
.pipe($.cheerio(function($) {
// add "display: none" to root svg tag
$('svg').attr('style', 'display:none');
// remove 'fill' & 'stroke' attributes from everything
// -> replace in CSS
$('svg').find('[fill]').removeAttr('fill');
$('svg').find('[stroke]').removeAttr('stroke');
$('svg').find('desc').remove();
}))
.pipe(gulp.dest(icons.dest + "/"));
});
{
"devDependencies": {
"gulp": "~3.8.10",
"gulp-svgstore": "~4.0.1",
"svgo": "~0.5.0",
"gulp-svgmin": "~1.1.0",
"gulp-plumber": "~0.6.6",
"gulp-load-plugins": "~0.8.0",
"gulp-cheerio": "~0.5.1",
"cheerio": "~0.18.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment