Skip to content

Instantly share code, notes, and snippets.

@dohoons
Last active May 11, 2018 01:34
Show Gist options
  • Save dohoons/ea33e0dbc87dc0f1b76cb77df36b6032 to your computer and use it in GitHub Desktop.
Save dohoons/ea33e0dbc87dc0f1b76cb77df36b6032 to your computer and use it in GitHub Desktop.
multi depth img sprite
var gulp = require('gulp'),
path = require('path'),
merge = require('merge-stream'),
listFilepaths = require('list-filepaths'),
spritesmith = require('gulp.spritesmith-multi');
// 자동 스프라이트
gulp.task('auto-sprite', function() {
var stream = merge();
listFilepaths('./src/img-sprites')
.then(filepaths => {
const dirs = [
// get unique array
...new Set(
// filepath map loop
// [ ~~/dir/two/one/file.png, ... ]
// => [ ~~/dir/two, ... ]
filepaths.map(v => {
var dir = v.split(path.sep);
dir.pop();
dir.pop();
dir = dir.join(path.sep);
dir = path.relative('./src/img-sprites', dir);
return dir;
})
)
];
return dirs;
})
.catch(console.error)
.then(function(dirs) {
dirs.forEach(function(dir, index) {
var spriteData = gulp.src(`./src/img-sprites/${dir}/**/*.png`)
.pipe(spritesmith({
spritesmith: function (options, sprite, icons){
options.imgPath = `@@img/sprite/${options.imgName}`;
options.cssName = `_${sprite}-sprite.scss`;
options.cssTemplate = null;
options.cssSpritesheetName = sprite;
options.padding = 10;
options.cssVarMap = function(sp) {
sp.name = `${sprite}-${sp.name}`;
};
return options;
}
}))
.on('error', function (err) {
console.log(err)
});
var imgStream = spriteData.img.pipe(gulp.dest(`./dist/images/${dir}/sprite`));
var cssStream = spriteData.css.pipe(gulp.dest(`./src/scss/${dir}/vendors`));
stream.add(imgStream);
stream.add(cssStream);
});
});
return stream;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment