Skip to content

Instantly share code, notes, and snippets.

@airton
Created July 5, 2019 04:43
Show Gist options
  • Save airton/5b732b4d82837405f1470299a07d2f74 to your computer and use it in GitHub Desktop.
Save airton/5b732b4d82837405f1470299a07d2f74 to your computer and use it in GitHub Desktop.
'use strict';
var SVGSpriter = require('svg-sprite'),
path = require('path'),
mkdirp = require('mkdirp'),
fs = require('fs'),
// config = {
// log: "debug",
// dest: ".",
// mode: {
// // inline: true, // Prepare for inline embedding
// symbol: {
// dest: "symbol",
// prefix: "ia-%s",
// dimensions: "-dims",
// sprite: "sprite.symbol.svg",
// render: {
// styl: true // Render a Sass stylesheet
// }
// },
// css: {
// dest: "css",
// prefix: "ia-%s",
// dimensions: "-dims",
// sprite: "sprite.css.svg",
// }
// },
// },
config = {
"log": "debug",
"mode": {
"symbol": {
"dest": ".",
"prefix": "ia-%s",
"sprite": "sprite-symbol.svg",
"inline": true,
"example": {
"template": "template.html",
"dest": "sprite.html"
}
}
},
"variables": {
now: +new Date(),
png: function() {
return function(sprite, render) {
console.log('--->',sprite, render)
return render(sprite).split('.svg').join('.png');
}
}
}
},
spriter = new SVGSpriter(config);
// Register some SVG files with the spriter
var file = 'svgs/brand.svg'; // <-- Replace with your local file path
var file2 = 'svgs/sebrae.svg'; // <-- Replace with your local file path
console.log('x',path.resolve(file))
spriter.add(path.resolve(file), file, fs.readFileSync(path.resolve(file), {encoding: 'utf-8'}));
spriter.add(path.resolve(file2), file2, fs.readFileSync(path.resolve(file2), {encoding: 'utf-8'}));
// ...
// Compile the sprite
spriter.compile(function(error, result, cssData) {
// console.log('result',result)
// Run through all configured output modes
for (var mode in result) {
// Run through all created resources and write them to disk
for (var type in result[mode]) {
// console.log('xx',result[mode][type].path)
// console.log('zz', result[mode][type].contents)
mkdirp.sync(path.dirname(result[mode][type].path));
fs.writeFileSync(result[mode][type].path, result[mode][type].contents);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment