1 | 'use strict';
|
2 |
|
3 | var path = require('path'),
|
4 | spritesmith = require('gulp.spritesmith');
|
5 |
|
6 | module.exports = function(options) {
|
7 | function compile(stream) {
|
8 | return stream.pipe(spritesmith({
|
9 | padding: 2,
|
10 | imgName: 'sprites.png',
|
11 | imgPath: 'sprites.png',
|
12 | cssName: 'sprites.css',
|
13 | cssTemplate: function(params) {
|
14 | if (!params.items.length) {
|
15 | return '';
|
16 | }
|
17 |
|
18 | var css = [],
|
19 | retina = [],
|
20 | prefix = 'sprite';
|
21 |
|
22 | css.push('i[class^="' + prefix + '-"], i[class*=" ' + prefix + '-"] { display: inline-block; background-image: url(' + params.items[0].image + '); font-size: 0; }');
|
23 |
|
24 | params.items.forEach(function(sprite) {
|
25 | if (sprite.source_image.indexOf('@2x') > -1) {
|
26 | retina.push('i.' + prefix + '-' + (sprite.name.replace('@2x', '')) + ' { background-position: -' + (Math.floor(sprite.x / 2)) + 'px -' + (Math.floor(sprite.y / 2)) + 'px; }');
|
27 | } else {
|
28 | css.push('i.' + prefix + '-' + sprite.name + ' { width: ' + sprite.px.width + '; height: ' + sprite.px.height + '; background-position: ' + sprite.px.offset_x + ' ' + sprite.px.offset_y + '; }');
|
29 | }
|
30 | });
|
31 |
|
32 | if (retina.length) {
|
33 | css.push('@media only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (min--moz-device-pixel-ratio: 2), only screen and (-o-min-device-pixel-ratio: 2/1), only screen and (min-device-pixel-ratio: 2), only screen and (min-resolution: 192dpi), only screen and (min-resolution: 2dppx) {');
|
34 | css.push('i[class^="' + prefix + '-"], i[class*=" ' + prefix + '-"] { background-size: ' + (Math.floor(params.items[0].total_width / 2)) + 'px ' + (Math.floor(params.items[0].total_height / 2)) + 'px; }');
|
35 | css.push(retina.join('\n') + '\n}');
|
36 | }
|
37 |
|
38 | return css.join('\n');
|
39 | }
|
40 | }));
|
41 | }
|
42 |
|
43 | return {
|
44 | src: path.join(options.paths.sprites.cwd, options.paths.sprites.glob),
|
45 | dest: path.join(options.paths.dest, options.paths.sprites.dest),
|
46 | pipe: compile
|
47 | };
|
48 | };
|