Skip to content

Instantly share code, notes, and snippets.

@friartuck6000
Created July 14, 2016 20:24
Show Gist options
  • Save friartuck6000/adf653e20b7dd0de065bb636dca8a60e to your computer and use it in GitHub Desktop.
Save friartuck6000/adf653e20b7dd0de065bb636dca8a60e to your computer and use it in GitHub Desktop.
Gulp plugin - takes a bunch of SVG files and builds a Sass partial
'use strict';
var _ = require('lodash');
var consolidate = require('consolidate');
var gutil = require('gulp-util');
var path = require('path');
var through = require('through2');
var File = gutil.File;
var PluginError = gutil.PluginError;
function configure(opts) {
return _.extend({
prefix: 'svg',
template: null,
aliases: {}
}, opts);
}
module.exports = function(opts) {
opts = configure(opts || {});
var buffer = {
opts: opts,
sprites: []
};
function buildBuffer(file, encoding, callback) {
if (!opts.template) {
this.emit('error', new PluginError('svg-icon', 'No output template specified'));
return;
}
var spriteName = path.basename(file.path, path.extname(file.path));
var spriteData = {
path: file.path,
name: path.basename(file.path, path.extname(file.path)),
data: file.contents.toString('base64'),
aliases: opts.aliases[spriteName] || []
};
buffer.sprites.push(spriteData);
callback(null);
}
function flush(callback) {
var that = this;
consolidate.lodash(opts.template, {
prefix: opts.prefix,
sprites: buffer.sprites
}).then(function(output) {
var newFilename = path.basename(opts.template, path.extname(opts.template)) + '.scss';
var outputFile = new File({
path: path.resolve(newFilename)
});
outputFile.contents = new Buffer(output);
that.push(outputFile);
callback();
});
}
return through.obj(buildBuffer, flush);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment