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

Commit 8ea05d4

Browse files
author
blaryjp
authored
Merge pull request #14 from ovh-ux/feat/sprite
feat(sprite): add sprite feature
2 parents 24f9e87 + fbfa0c9 commit 8ea05d4

File tree

8 files changed

+147
-0
lines changed

8 files changed

+147
-0
lines changed

README.md

+2
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 (with Retina Display support)
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

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
A documentation for SCSS features is available [here](https://www.bignerdranch.com/blog/css-sprite-management-with-gulp-part2/).
4+
5+
## Usage
6+
7+
Given a file named `facebook.png`, you can use this Sass mixin:
8+
9+
```scss
10+
@include sprite('sprite-facebook');
11+
```
12+
13+
Or this HTML class:
14+
15+
```html
16+
<span class="sprite-facebook"></span>
17+
```
18+
19+
### Retina Display support
20+
21+
You can generate a second sprite for Retina Display.
22+
First, you need to duplicates all your images and append "@2x" in the filename. For example: "facebook.png" and "[email protected]".
23+
24+
After enabled it, you can now use the mixin:
25+
26+
```scss
27+
@include retina-sprite('sprite-facebook');
28+
```
29+
30+
It will automatically take sprite@2x for Retina Display, and normal sprite for others.
31+
32+
33+
## Commands
34+
35+
- `gulp sprite` - Generates the sprite
36+
- `gulp watch:sprite` - Watch for images modifed and regenerate the sprite
37+
38+
## Config
39+
40+
- `config.sprite.src` - Array or String of globbed PNG files
41+
- `config.sprite.imgDest` - Destination directory for the sprite image file
42+
- `config.sprite.cssDest` - Destination directory for the sprite SCSS file
43+
- `config.sprite.imgName` - Name of the sprite image file
44+
- `config.sprite.cssName` - Name of the sprite SCSS file
45+
- `config.sprite.imgPathPrefix` - Sprite image path prefix
46+
- `config.sprite.spritesheetName` - Name of the sprite
47+
- `config.sprite.imagemin` - Enable imagemin compression for the sprite image file
48+
- `config.sprite.retina.enabled` - Enable retina sprite generation
49+
- `config.sprite.retina.imgName` - Name of the retina sprite image file
50+
- `config.sprite.retina.filter` - Images filter that match the retina files
51+
52+
---

docs/index.md

+2
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 (with Retina Display support)
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

+16
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,22 @@ 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
100+
retina:
101+
enabled: false
102+
103+
filter:
104+
- images/sprite/*@2x.png
89105
patternLab:
90106
enabled: false
91107
configFile: pattern-lab/config/config.yml

index.js

+4
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

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
retinaSrcFilter: config.sprite.retina.enabled ? config.sprite.retina.filter : undefined,
20+
retinaImgName: config.sprite.retina.enabled ? config.sprite.retina.imgName : undefined,
21+
retinaImgPath: config.sprite.retina.enabled ? `${config.sprite.imgPathPrefix}${config.sprite.retina.imgName}` : undefined,
22+
cssRetinaSpritesheetName: config.sprite.retina.enabled ? `${config.sprite.spritesheetName}-2x` : undefined,
23+
cssVarMap(datas) {
24+
// eslint-disable-next-line no-param-reassign
25+
datas.name = `${config.sprite.spritesheetName}-${datas.name}`;
26+
}
27+
}));
28+
29+
// Pipe image stream through image optimizer and onto disk
30+
const imgStream = spriteData.img
31+
// DEV: We must buffer our stream into a Buffer for `imagemin`
32+
.pipe(buffer())
33+
.pipe(gulpif(config.sprite.imagemin, imagemin()))
34+
.pipe(gulp.dest(config.sprite.imgDest));
35+
36+
// Pipe CSS stream onto disk
37+
const cssStream = spriteData.css
38+
.pipe(gulp.dest(config.sprite.cssDest));
39+
40+
// Return a merged stream to handle both `end` events
41+
return merge(imgStream, cssStream);
42+
}
43+
44+
sprite.description = 'Generates a sprite (img and scss files).';
45+
46+
gulp.task('sprite', sprite);
47+
48+
gulp.task('clean:sprite', (done) => {
49+
del([
50+
join(config.sprite.imgDest, config.sprite.imgName),
51+
join(config.sprite.cssDest, config.sprite.cssName)
52+
], { force: true }).then(() => {
53+
done();
54+
});
55+
});
56+
57+
gulp.task('watch:sprite', () => {
58+
gulp.watch(config.sprite.src, sprite);
59+
});
60+
61+
tasks.watch.push('watch:sprite');
62+
63+
tasks.compile.push('sprite');
64+
65+
tasks.clean.push('clean:sprite');
66+
};

mkdocs.yml

+1
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

+4
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)