|
1 | 1 | import path from 'path'; |
2 | 2 | import fs from 'fs-extra'; |
3 | | -import postcss from 'postcss'; |
4 | 3 | import Promise from 'bluebird'; |
5 | 4 | import _ from 'lodash'; |
6 | 5 | import debug from 'debug'; |
@@ -247,43 +246,46 @@ export function setTokens(root, opts, images) { |
247 | 246 | const ruleStr = rule.toString(); |
248 | 247 | let url, image, color, backgroundColorDecl, commentDecl; |
249 | 248 |
|
| 249 | + if (!hasImageInRule(ruleStr)) { |
| 250 | + return; |
| 251 | + } |
| 252 | + |
250 | 253 | // Manipulate only rules with image in them |
251 | | - if (hasImageInRule(ruleStr)) { |
252 | | - url = getImageUrl(ruleStr)[1]; |
253 | | - image = _.find(images, { url }); |
254 | | - |
255 | | - if (image) { |
256 | | - // Remove all necessary background declarations |
257 | | - rule.walkDecls(/^background-(repeat|size|position)$/, decl => decl.remove()); |
258 | | - |
259 | | - // Extract color to background-color property |
260 | | - if (decl.prop === BACKGROUND) { |
261 | | - color = getColor(decl.value); |
262 | | - |
263 | | - if (color) { |
264 | | - backgroundColorDecl = postcss.decl({ |
265 | | - prop: 'background-color', |
266 | | - value: getColor(decl.value) |
267 | | - }); |
268 | | - backgroundColorDecl.raws.before = ONE_SPACE; |
269 | | - |
270 | | - rule.append(backgroundColorDecl); |
271 | | - } |
272 | | - } |
273 | | - |
274 | | - // Replace with comment token |
275 | | - if (decl.prop === BACKGROUND || decl.prop === BACKGROUND_IMAGE) { |
276 | | - commentDecl = postcss.comment({ |
277 | | - text: image.url |
278 | | - }); |
279 | | - |
280 | | - commentDecl.raws.left = `${ONE_SPACE}${COMMENT_TOKEN_PREFIX}`; |
281 | | - image.token = commentDecl.toString(); |
282 | | - |
283 | | - decl.replaceWith(commentDecl); |
284 | | - } |
| 254 | + |
| 255 | + url = getImageUrl(ruleStr)[1]; |
| 256 | + image = _.find(images, { url }); |
| 257 | + |
| 258 | + if (!image) { |
| 259 | + return; |
| 260 | + } |
| 261 | + |
| 262 | + // Remove all necessary background declarations |
| 263 | + rule.walkDecls(/^background-(repeat|size|position)$/, decl => decl.remove()); |
| 264 | + |
| 265 | + // Extract color to background-color property |
| 266 | + if (decl.prop === BACKGROUND) { |
| 267 | + color = getColor(decl.value); |
| 268 | + |
| 269 | + if (color) { |
| 270 | + decl.cloneAfter({ |
| 271 | + prop: 'background-color', |
| 272 | + value: color |
| 273 | + }).raws.before = ONE_SPACE; |
285 | 274 | } |
286 | 275 | } |
| 276 | + |
| 277 | + // Replace with comment token |
| 278 | + if (_.includes([BACKGROUND, BACKGROUND_IMAGE], decl.prop)) { |
| 279 | + commentDecl = decl.cloneAfter({ |
| 280 | + type: 'comment', |
| 281 | + text: image.url |
| 282 | + }); |
| 283 | + |
| 284 | + commentDecl.raws.left = `${ONE_SPACE}${COMMENT_TOKEN_PREFIX}`; |
| 285 | + image.token = commentDecl.toString(); |
| 286 | + |
| 287 | + decl.remove(); |
| 288 | + } |
287 | 289 | }); |
288 | 290 |
|
289 | 291 | resolve([root, opts, images]); |
@@ -452,25 +454,17 @@ export function updateRule(rule, token, image) { |
452 | 454 | const sizeX = spriteWidth / ratio; |
453 | 455 | const sizeY = spriteHeight / ratio; |
454 | 456 |
|
455 | | - const backgroundImageDecl = postcss.decl({ |
| 457 | + token.cloneAfter({ |
| 458 | + type: 'decl', |
456 | 459 | prop: 'background-image', |
457 | 460 | value: `url(${spriteUrl})` |
458 | | - }); |
459 | | - |
460 | | - const backgroundPositionDecl = postcss.decl({ |
| 461 | + }).cloneAfter({ |
461 | 462 | prop: 'background-position', |
462 | 463 | value: `${posX}px ${posY}px` |
463 | | - }); |
464 | | - |
465 | | - rule.insertAfter(token, backgroundImageDecl); |
466 | | - rule.insertAfter(backgroundImageDecl, backgroundPositionDecl); |
467 | | - |
468 | | - const backgroundSizeDecl = postcss.decl({ |
| 464 | + }).cloneAfter({ |
469 | 465 | prop: 'background-size', |
470 | 466 | value: `${sizeX}px ${sizeY}px` |
471 | 467 | }); |
472 | | - |
473 | | - rule.insertAfter(backgroundPositionDecl, backgroundSizeDecl); |
474 | 468 | } |
475 | 469 |
|
476 | 470 | ///////////////////////// |
|
0 commit comments