Skip to content

Instantly share code, notes, and snippets.

@stasgavrylov
Last active December 24, 2018 11:39
Show Gist options
  • Save stasgavrylov/40efd7b9fc8b83021c0118bec7c8987a to your computer and use it in GitHub Desktop.
Save stasgavrylov/40efd7b9fc8b83021c0118bec7c8987a to your computer and use it in GitHub Desktop.
Script for SVG sprites creation
'use strict';
let OUTPUT_DIR = 'public';
let OUTPUT_FILE = 'icons.svg';
let SVGSpriter = require('svg-sprite'),
path = require('path'),
fs = require('fs'),
File = require('vinyl'),
glob = require('glob'),
spriter = new SVGSpriter({
mode: { symbol: true },
shape: { id: { separator: '' } },
svg: { namespaceIDs: false }
}),
cwd = path.resolve('src');
// Find all SVG files in 'src' folder recursively
glob.glob('**/*.svg', { cwd: cwd }, function (err, files) {
files.forEach(function (file) {
// Create and add a vinyl file instance for each SVG
spriter.add(new File({
path: path.join(cwd, file), // Absolute path to the SVG file
base: cwd, // Base path (see `name` argument)
contents: fs.readFileSync(path.join(cwd, file)) // SVG file contents
}));
})
spriter.compile(function (error, result, data) {
fs.writeFileSync(path.resolve(`${OUTPUT_DIR}/${OUTPUT_FILE}`), result.symbol.sprite.contents);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment