@@ -29,7 +29,7 @@ var GROUP_MASK = '*';
29
29
var BACKGROUND = 'background' ;
30
30
var BACKGROUND_IMAGE = 'background-image' ;
31
31
32
- module . exports = postcss . plugin ( 'postcss-easysprites' , function ( opts ) {
32
+ module . exports = postcss . plugin ( 'postcss-easysprites' , function ( opts ) {
33
33
opts = opts || { } ;
34
34
35
35
// opts
@@ -42,29 +42,29 @@ module.exports = postcss.plugin('postcss-easysprites', function(opts) {
42
42
opts . spritePath = path . resolve ( process . cwd ( ) , opts . spritePath || '' ) ;
43
43
44
44
// Group retina images
45
- opts . groupBy . unshift ( function ( image ) {
45
+ opts . groupBy . unshift ( function ( image ) {
46
46
if ( image . ratio > 1 ) { return '@' + image . ratio + 'x' ; }
47
47
48
48
return null ;
49
49
} ) ;
50
50
51
- return function ( css ) {
51
+ return function ( css ) {
52
52
// if file path
53
53
54
54
return Q
55
55
56
56
// prepare part
57
57
. all ( [ collectImages ( css , opts ) , opts ] )
58
58
. spread ( applyGroupBy )
59
- . spread ( function ( images , opts ) {
59
+ . spread ( function ( images , opts ) {
60
60
return setTokens ( images , opts , css ) ;
61
61
} )
62
62
63
63
// compilation part
64
64
. spread ( runSpriteSmith )
65
65
. spread ( saveSprites )
66
66
. spread ( mapSpritesProperties )
67
- . spread ( function ( images , opts , sprites ) {
67
+ . spread ( function ( images , opts , sprites ) {
68
68
return updateReferences ( images , opts , sprites , css ) ;
69
69
} ) ;
70
70
} ;
@@ -77,7 +77,7 @@ function collectImages(css, opts) {
77
77
throw 'Stylesheets path is undefined, please use option stylesheetPath!' ;
78
78
}
79
79
80
- css . walkRules ( function ( rule ) {
80
+ css . walkRules ( function ( rule ) {
81
81
var image = {
82
82
path : null ,
83
83
url : null ,
@@ -115,7 +115,7 @@ function collectImages(css, opts) {
115
115
log ( 'Easysprites:' , gutil . colors . red ( image . path ) , 'file unreachable or not exists' ) ;
116
116
117
117
// remove hash from link
118
- lodash . each ( rule . nodes , function ( node ) {
118
+ lodash . each ( rule . nodes , function ( node ) {
119
119
node . value = node . value . replace ( '#' + image . hash , '' ) ;
120
120
} ) ;
121
121
@@ -130,11 +130,11 @@ function collectImages(css, opts) {
130
130
}
131
131
132
132
function applyGroupBy ( images , opts ) {
133
- return Q . Promise ( function ( resolve , reject ) {
134
- async . reduce ( opts . groupBy , images , function ( images , group , next ) {
135
- async . map ( images , function ( image , done ) {
133
+ return Q . Promise ( function ( resolve , reject ) {
134
+ async . reduce ( opts . groupBy , images , function ( images , group , next ) {
135
+ async . map ( images , function ( image , done ) {
136
136
new Q ( group ( image ) )
137
- . then ( function ( group ) {
137
+ . then ( function ( group ) {
138
138
if ( group ) {
139
139
image . groups . push ( group ) ;
140
140
}
@@ -143,7 +143,7 @@ function applyGroupBy(images, opts) {
143
143
} )
144
144
. catch ( done ) ;
145
145
} , next ) ;
146
- } , function ( err , images ) {
146
+ } , function ( err , images ) {
147
147
148
148
if ( err ) {
149
149
return reject ( err ) ;
@@ -155,8 +155,8 @@ function applyGroupBy(images, opts) {
155
155
}
156
156
157
157
function setTokens ( images , opts , css ) {
158
- return Q . Promise ( function ( resolve ) {
159
- css . walkDecls ( / ^ b a c k g r o u n d ( - i m a g e ) ? $ / , function ( decl ) {
158
+ return Q . Promise ( function ( resolve ) {
159
+ css . walkDecls ( / ^ b a c k g r o u n d ( - i m a g e ) ? $ / , function ( decl ) {
160
160
var rule = decl . parent ;
161
161
var url , image , color ;
162
162
@@ -170,8 +170,8 @@ function setTokens(images, opts, css) {
170
170
// We remove these declarations since
171
171
// our plugin will insert them when
172
172
// they are necessary.
173
- rule . walkDecls ( / ^ b a c k g r o u n d - ( r e p e a t | s i z e | p o s i t i o n ) $ / , function ( decl ) {
174
- decl . removeSelf ( ) ;
173
+ rule . walkDecls ( / ^ b a c k g r o u n d - ( r e p e a t | s i z e | p o s i t i o n ) $ / , function ( decl ) {
174
+ decl . remove ( ) ;
175
175
} ) ;
176
176
177
177
if ( decl . prop === BACKGROUND ) {
@@ -181,7 +181,7 @@ function setTokens(images, opts, css) {
181
181
if ( color && color . length === 1 ) {
182
182
rule . prop = 'background-color' ;
183
183
rule . value = color [ 0 ] ;
184
- rule . raws . before = ' ' ;
184
+ rule . before = ' ' ;
185
185
}
186
186
}
187
187
@@ -191,7 +191,7 @@ function setTokens(images, opts, css) {
191
191
raws : {
192
192
before : ' ' ,
193
193
left : '@replace|' ,
194
- right : ''
194
+ right : '' ,
195
195
} ,
196
196
} ) ;
197
197
@@ -208,18 +208,18 @@ function setTokens(images, opts, css) {
208
208
}
209
209
210
210
function runSpriteSmith ( images , opts ) {
211
- return Q . Promise ( function ( resolve , reject ) {
211
+ return Q . Promise ( function ( resolve , reject ) {
212
212
var all = lodash
213
213
. chain ( images )
214
- . groupBy ( function ( image ) {
214
+ . groupBy ( function ( image ) {
215
215
var temp ;
216
216
217
217
temp = image . groups . map ( mask ( true ) ) ;
218
218
temp . unshift ( '_' ) ;
219
219
220
220
return temp . join ( GROUP_DELIMITER ) ;
221
221
} )
222
- . map ( function ( images , temp ) {
222
+ . map ( function ( images , temp ) {
223
223
var config = lodash . merge ( { } , opts , {
224
224
src : lodash . pluck ( images , 'path' ) ,
225
225
} ) ;
@@ -242,7 +242,7 @@ function runSpriteSmith(images, opts) {
242
242
243
243
// collect images datechanged
244
244
config . spriteName = temp . replace ( / ^ _ ./ , '' ) . replace ( / .@ / , '@' ) ;
245
- lodash . each ( config . src , function ( image ) {
245
+ lodash . each ( config . src , function ( image ) {
246
246
checkstring . push ( image + '=' + md5 ( fs . readFileSync ( image ) . toString ( ) ) ) ;
247
247
} ) ;
248
248
@@ -259,7 +259,7 @@ function runSpriteSmith(images, opts) {
259
259
}
260
260
261
261
return Q . nfcall ( spritesmith , config )
262
- . then ( function ( result ) {
262
+ . then ( function ( result ) {
263
263
temp = temp . split ( GROUP_DELIMITER ) ;
264
264
temp . shift ( ) ;
265
265
@@ -282,10 +282,10 @@ function runSpriteSmith(images, opts) {
282
282
. value ( ) ;
283
283
284
284
Q . all ( all )
285
- . then ( function ( results ) {
285
+ . then ( function ( results ) {
286
286
resolve ( [ images , opts , results ] ) ;
287
287
} )
288
- . catch ( function ( err ) {
288
+ . catch ( function ( err ) {
289
289
if ( err ) {
290
290
reject ( err ) ;
291
291
}
@@ -294,15 +294,15 @@ function runSpriteSmith(images, opts) {
294
294
}
295
295
296
296
function saveSprites ( images , opts , sprites ) {
297
- return Q . Promise ( function ( resolve , reject ) {
297
+ return Q . Promise ( function ( resolve , reject ) {
298
298
299
299
if ( ! fs . existsSync ( opts . spritePath ) ) {
300
300
mkdirp . sync ( opts . spritePath ) ;
301
301
}
302
302
303
303
var all = lodash
304
304
. chain ( sprites )
305
- . map ( function ( sprite ) {
305
+ . map ( function ( sprite ) {
306
306
sprite . path = makeSpritePath ( opts , sprite . groups ) ;
307
307
308
308
// if this file is up to date
@@ -315,17 +315,17 @@ function saveSprites(images, opts, sprites) {
315
315
316
316
// save new file version
317
317
return Q . nfcall ( fs . writeFile , sprite . path , new Buffer ( sprite . image , 'binary' ) )
318
- . then ( function ( ) {
318
+ . then ( function ( ) {
319
319
log ( 'Easysprites:' , gutil . colors . yellow ( sprite . path ) , 'generated.' ) ;
320
320
return sprite ;
321
321
} ) ;
322
322
} )
323
323
. value ( ) ;
324
324
325
325
Q . all ( all )
326
- . then ( function ( sprites ) {
326
+ . then ( function ( sprites ) {
327
327
resolve ( [ images , opts , sprites ] ) ; } )
328
- . catch ( function ( err ) {
328
+ . catch ( function ( err ) {
329
329
if ( err ) {
330
330
reject ( err ) ;
331
331
}
@@ -342,9 +342,9 @@ function saveSprites(images, opts, sprites) {
342
342
* @return {Promise }
343
343
*/
344
344
function mapSpritesProperties ( images , opts , sprites ) {
345
- return Q . Promise ( function ( resolve ) {
346
- sprites = lodash . map ( sprites , function ( sprite ) {
347
- return lodash . map ( sprite . coordinates , function ( coordinates , imagePath ) {
345
+ return Q . Promise ( function ( resolve ) {
346
+ sprites = lodash . map ( sprites , function ( sprite ) {
347
+ return lodash . map ( sprite . coordinates , function ( coordinates , imagePath ) {
348
348
349
349
return lodash . merge ( lodash . find ( images , { path : imagePath } ) , {
350
350
coordinates : coordinates ,
@@ -359,8 +359,8 @@ function mapSpritesProperties(images, opts, sprites) {
359
359
}
360
360
361
361
function updateReferences ( images , opts , sprites , css ) {
362
- return Q . Promise ( function ( resolve ) {
363
- css . walkComments ( function ( comment ) {
362
+ return Q . Promise ( function ( resolve ) {
363
+ css . walkComments ( function ( comment ) {
364
364
var rule , image , backgroundImage , backgroundPosition , backgroundSize ;
365
365
366
366
// Manipulate only token comments
@@ -388,11 +388,16 @@ function updateReferences(images, opts, sprites, css) {
388
388
// Output the dimensions
389
389
rule = backgroundImage . parent ;
390
390
if ( opts . outputDimensions ) {
391
- [ 'height' , 'width' ] . forEach ( function ( prop ) {
392
- rule . insertAfter ( backgroundImage , postcss . decl ( {
393
- prop : prop ,
394
- value : ( image . ratio > 1 ? image . coordinates [ prop ] / image . ratio : image . coordinates [ prop ] ) + 'px' ,
395
- } ) ) ;
391
+ [ 'height' , 'width' ] . forEach ( function ( prop ) {
392
+ rule . insertAfter (
393
+ backgroundImage ,
394
+ postcss . decl ( {
395
+ prop : prop ,
396
+ value : ( image . ratio > 1 ?
397
+ image . coordinates [ prop ] / image . ratio :
398
+ image . coordinates [ prop ] ) + 'px' ,
399
+ } )
400
+ ) ;
396
401
} ) ;
397
402
}
398
403
@@ -424,7 +429,7 @@ function mask(toggle) {
424
429
var input = new RegExp ( '[' + ( toggle ? GROUP_DELIMITER : GROUP_MASK ) + ']' , 'gi' ) ;
425
430
var output = toggle ? GROUP_MASK : GROUP_DELIMITER ;
426
431
427
- return function ( value ) {
432
+ return function ( value ) {
428
433
return value . replace ( input , output ) ;
429
434
} ;
430
435
}
@@ -473,7 +478,7 @@ function getColor(decl) {
473
478
var regexes = [ '(#([0-9a-f]{3}){1,2})' , 'rgba?\\([^\\)]+\\)' ] ;
474
479
var matches = null ;
475
480
476
- lodash . forEach ( regexes , function ( regex ) {
481
+ lodash . forEach ( regexes , function ( regex ) {
477
482
regex = new RegExp ( regex , 'gi' ) ;
478
483
479
484
if ( regex . test ( decl . value ) ) {
@@ -564,7 +569,7 @@ function getRetinaRatio(url) {
564
569
* @return {Boolean }
565
570
*/
566
571
function areAllRetina ( images ) {
567
- return lodash . every ( images , function ( image ) {
572
+ return lodash . every ( images , function ( image ) {
568
573
return image . ratio > 1 ;
569
574
} ) ;
570
575
}
0 commit comments