mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 15:17:25 +01:00
Reformat sprite tasks
This commit is contained in:
@@ -3,23 +3,26 @@ import imagemin from 'gulp-imagemin';
|
|||||||
import spritesmith from 'gulp.spritesmith';
|
import spritesmith from 'gulp.spritesmith';
|
||||||
import clean from 'gulp-clean';
|
import clean from 'gulp-clean';
|
||||||
import sizeOf from 'image-size';
|
import sizeOf from 'image-size';
|
||||||
import merge from 'merge-stream';
|
import mergeStream from 'merge-stream';
|
||||||
import {basename} from 'path';
|
import {basename} from 'path';
|
||||||
import {sync} from 'glob';
|
import {sync} from 'glob';
|
||||||
import {times, each} from 'lodash';
|
import {each} from 'lodash';
|
||||||
|
|
||||||
// https://github.com/Ensighten/grunt-spritesmith/issues/67#issuecomment-34786248
|
// https://github.com/Ensighten/grunt-spritesmith/issues/67#issuecomment-34786248
|
||||||
const MAX_SPRITESHEET_SIZE = 1024 * 1024 * 3;
|
const MAX_SPRITESHEET_SIZE = 1024 * 1024 * 3;
|
||||||
const DIST_PATH = 'common/dist/sprites/';
|
const DIST_PATH = 'common/dist/sprites/';
|
||||||
const SPRITES_SRC = sync('common/img/sprites/spritesmith/**/*.png');
|
|
||||||
|
|
||||||
let spritesTasks = ['sprites:clean'];
|
gulp.task('sprites:compile', ['sprites:clean', 'sprites:main', 'sprites:largeSprites', 'sprites:checkCompiledDimensions']);
|
||||||
|
|
||||||
let mainSrc = sync('common/img/sprites/spritesmith/**/*.png');
|
gulp.task('sprites:main', () => {
|
||||||
_generateSpritesTasks('main', mainSrc);
|
let mainSrc = sync('common/img/sprites/spritesmith/**/*.png');
|
||||||
|
return _createSpritesStream('main', mainSrc);
|
||||||
|
});
|
||||||
|
|
||||||
let largeSrc = sync('common/img/sprites/spritesmith_large/**/*.png');
|
gulp.task('sprites:largeSprites', () => {
|
||||||
_generateSpritesTasks('largeSprites', largeSrc);
|
let largeSrc = sync('common/img/sprites/spritesmith_large/**/*.png');
|
||||||
|
return _createSpritesStream('largeSprites', largeSrc);
|
||||||
|
});
|
||||||
|
|
||||||
gulp.task('sprites:clean', (done) => {
|
gulp.task('sprites:clean', (done) => {
|
||||||
gulp.src(`${DIST_PATH}spritesmith*`)
|
gulp.src(`${DIST_PATH}spritesmith*`)
|
||||||
@@ -28,7 +31,7 @@ gulp.task('sprites:clean', (done) => {
|
|||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
gulp.task('sprites:checkCompiledDimensions', () => {
|
gulp.task('sprites:checkCompiledDimensions', ['sprites:main', 'sprites:largeSprites'], () => {
|
||||||
console.log('Verifiying that images do not exceed max dimensions');
|
console.log('Verifiying that images do not exceed max dimensions');
|
||||||
|
|
||||||
let numberOfSheetsThatAreTooBig = 0;
|
let numberOfSheetsThatAreTooBig = 0;
|
||||||
@@ -53,39 +56,35 @@ gulp.task('sprites:checkCompiledDimensions', () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
gulp.task('sprites:compile', spritesTasks, () => {
|
function _createSpritesStream(name, src) {
|
||||||
gulp.run('sprites:checkCompiledDimensions');
|
|
||||||
});
|
|
||||||
|
|
||||||
function _generateSpritesTasks(name, src) {
|
|
||||||
let spritesheetSliceIndicies = _calculateSpritesheetsSrcIndicies(src);
|
let spritesheetSliceIndicies = _calculateSpritesheetsSrcIndicies(src);
|
||||||
|
let stream = mergeStream();
|
||||||
|
|
||||||
each(spritesheetSliceIndicies, (start, index) => {
|
each(spritesheetSliceIndicies, (start, index) => {
|
||||||
let slicedSrc = src.slice(start, spritesheetSliceIndicies[index + 1]);
|
let slicedSrc = src.slice(start, spritesheetSliceIndicies[index + 1]);
|
||||||
let taskName = `sprites:${name}:${index}`;
|
|
||||||
spritesTasks.push(taskName);
|
|
||||||
|
|
||||||
gulp.task(taskName, () => {
|
let spriteData = gulp.src(slicedSrc)
|
||||||
let spriteData = gulp.src(slicedSrc)
|
.pipe(spritesmith({
|
||||||
.pipe(spritesmith({
|
imgName: `spritesmith-${name}-${index}.png`,
|
||||||
imgName: `spritesmith-${name}-${index}.png`,
|
cssName: `spritesmith-${name}-${index}.css`,
|
||||||
cssName: `spritesmith-${name}-${index}.css`,
|
algorithm: 'binary-tree',
|
||||||
algorithm: 'binary-tree',
|
padding: 1,
|
||||||
padding: 1,
|
cssTemplate: 'common/css/css.template.mustache',
|
||||||
cssTemplate: 'common/css/css.template.mustache',
|
cssVarMap: _cssVarMap
|
||||||
cssVarMap: _cssVarMap
|
}));
|
||||||
}));
|
|
||||||
|
|
||||||
let imgStream = spriteData.img
|
let imgStream = spriteData.img
|
||||||
.pipe(imagemin())
|
.pipe(imagemin())
|
||||||
.pipe(gulp.dest(DIST_PATH));
|
.pipe(gulp.dest(DIST_PATH));
|
||||||
|
|
||||||
let cssStream = spriteData.css
|
let cssStream = spriteData.css
|
||||||
.pipe(gulp.dest(DIST_PATH));
|
.pipe(gulp.dest(DIST_PATH));
|
||||||
|
|
||||||
return merge(imgStream, cssStream);
|
stream.add(imgStream);
|
||||||
});
|
stream.add(cssStream);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
function _calculateSpritesheetsSrcIndicies(src) {
|
function _calculateSpritesheetsSrcIndicies(src) {
|
||||||
|
|||||||
Reference in New Issue
Block a user