Skip to content
This repository was archived by the owner on Feb 21, 2025. It is now read-only.

Commit 6b0dee8

Browse files
committed
feat(sprite): add sprite feature
1 parent 24f9e87 commit 6b0dee8

File tree

8 files changed

+119
-0
lines changed

8 files changed

+119
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ This stack core is to be included in your main project and sets up many Gulp tas
1919
- webpack module bundling
2020
- SVG => Font Icons compiling with support for adding mixins and classes to SCSS along with a demo page
2121
- Drupal file watching to trigger Drush cache clears
22+
- Copy any files to an other location
23+
- Sprite generator
2224

2325
All is easily configurable by changing values in your `gulpfile.yml` file in your project. These values are merged into the `gulpfile.default.yml` file - look there for the available options and defaults.
2426

docs/features/sprite.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Uses [gulp.spritesmith](https://github.com/twolfson/gulp.spritesmith). Grabs a folder of images and turns them into a sprite, creates a Sass mixin and class for each based on filename.
2+
3+
## Usage
4+
5+
Given a file named `facebook.png`, you can use this Sass mixin:
6+
7+
```scss
8+
@include sprite('sprite-facebook');
9+
```
10+
11+
Or this HTML class:
12+
13+
```html
14+
<span class="sprite_facebook"></span>
15+
```
16+
17+
## Commands
18+
19+
- `gulp sprite` - Generates the sprite
20+
- `gulp watch:sprite` - Watch for images modifed and regenerate the sprite
21+
22+
## Config
23+
24+
- `config.sprite.src` - Array or String of globbed PNG files
25+
- `config.sprite.imgDest` - Destination directory for the sprite image file
26+
- `config.sprite.cssDest` - Destination directory for the sprite SCSS file
27+
- `config.sprite.imgName` - Name of the sprite image file
28+
- `config.sprite.cssName` - Name of the sprite SCSS file
29+
- `config.sprite.imgPathPrefix` - Sprite image path prefix
30+
- `config.sprite.spritesheetName` - Name of the sprite
31+
- `config.sprite.imagemin` - Enable imagemin compression for the sprite image file
32+
33+
---

docs/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ This stack core is to be included in your main project and sets up many Gulp tas
1414
- webpack module bundling
1515
- SVG => Font Icons compiling with support for adding mixins and classes to SCSS along with a demo page
1616
- Drupal file watching to trigger Drush cache clears
17+
- Copy any files to an other location
18+
- Sprite generator
1719

1820
All is easily configurable by changing values in your `gulpfile.yml` file in your project. These values are merged into the `gulpfile.default.yml` file - look there for the available options and defaults.
1921

gulpfile.default.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,17 @@ icons:
8686
- eot
8787
- woff
8888
- svg
89+
sprite:
90+
enabled: false
91+
src:
92+
- images/sprite/*.png
93+
imgDest: dist/
94+
cssDest: scss/base/
95+
imgName: sprite.png
96+
cssName: _sprite.scss
97+
imgPathPrefix: ../dist/
98+
spritesheetName: sprite
99+
imagemin: true
89100
patternLab:
90101
enabled: false
91102
configFile: pattern-lab/config/config.yml

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ module.exports = (gulp, userConfig, tasks) => {
1919
require('./lib/icons')(gulp, config, tasks);
2020
}
2121

22+
if (config.sprite.enabled) {
23+
require('./lib/sprite')(gulp, config, tasks);
24+
}
25+
2226
if (config.js.enabled) {
2327
require('./lib/js')(gulp, config, tasks);
2428
}

lib/sprite.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict';
2+
3+
const gulpif = require('gulp-if');
4+
const join = require('path').join;
5+
const del = require('del');
6+
const spritesmith = require('gulp.spritesmith');
7+
const buffer = require('vinyl-buffer');
8+
const imagemin = require('gulp-imagemin');
9+
const merge = require('merge-stream');
10+
11+
module.exports = (gulp, config, tasks) => {
12+
function sprite() {
13+
const spriteData = gulp.src(config.sprite.src)
14+
.pipe(spritesmith({
15+
imgName: config.sprite.imgName,
16+
imgPath: `${config.sprite.imgPathPrefix}${config.sprite.imgName}`,
17+
cssName: config.sprite.cssName,
18+
cssSpritesheetName: config.sprite.spritesheetName,
19+
cssVarMap(datas) {
20+
// eslint-disable-next-line no-param-reassign
21+
datas.name = `${config.sprite.spritesheetName}_${datas.name}`;
22+
}
23+
}));
24+
25+
// Pipe image stream through image optimizer and onto disk
26+
const imgStream = spriteData.img
27+
// DEV: We must buffer our stream into a Buffer for `imagemin`
28+
.pipe(buffer())
29+
.pipe(gulpif(config.sprite.imagemin, imagemin()))
30+
.pipe(gulp.dest(config.sprite.imgDest));
31+
32+
// Pipe CSS stream onto disk
33+
const cssStream = spriteData.css
34+
.pipe(gulp.dest(config.sprite.cssDest));
35+
36+
// Return a merged stream to handle both `end` events
37+
return merge(imgStream, cssStream);
38+
}
39+
40+
sprite.description = 'Generates a sprite (img and scss files).';
41+
42+
gulp.task('sprite', sprite);
43+
44+
gulp.task('clean:sprite', (done) => {
45+
del([
46+
join(config.sprite.imgDest, config.sprite.imgName),
47+
join(config.sprite.cssDest, config.sprite.cssName)
48+
], { force: true }).then(() => {
49+
done();
50+
});
51+
});
52+
53+
gulp.task('watch:sprite', () => {
54+
gulp.watch(config.sprite.src, sprite);
55+
});
56+
57+
tasks.watch.push('watch:sprite');
58+
59+
tasks.compile.push('sprite');
60+
61+
tasks.clean.push('clean:sprite');
62+
};

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pages:
99
- JS: features/js.md
1010
- Webpack: features/webpack.md
1111
- Icons: features/icons.md
12+
- Sprite: features/sprite.md
1213
- Drupal-Drush: features/drupal-drush.md
1314
- Browsersync: features/browser-sync.md
1415
- Copy: features/copy.md

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"gulp-flatten": "^0.4.0",
4646
"gulp-iconfont": "^9.1.0",
4747
"gulp-if": "^2.0.2",
48+
"gulp-imagemin": "^4.1.0",
4849
"gulp-inject": "^4.3.0",
4950
"gulp-notify": "^3.2.0",
5051
"gulp-plumber": "^1.2.0",
@@ -54,14 +55,17 @@
5455
"gulp-sourcemaps": "^2.6.4",
5556
"gulp-stylelint": "^6.0.0",
5657
"gulp-uglify": "^3.0.0",
58+
"gulp.spritesmith": "^6.9.0",
5759
"js-yaml": "^3.10.0",
5860
"lodash": "^4.17.5",
5961
"main-bower-files": "^2.13.1",
62+
"merge-stream": "^1.0.1",
6063
"node-notifier": "^5.2.1",
6164
"sassdoc": "^2.5.0",
6265
"stylelint": "^8.4.0",
6366
"stylelint-scss": "^2.3.0",
6467
"through2": "^2.0.3",
68+
"vinyl-buffer": "^1.0.1",
6569
"webpack": "^3.11.0"
6670
},
6771
"devDependencies": {

0 commit comments

Comments
 (0)