|
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'; |
@@ -261,26 +260,24 @@ export function setTokens(root, opts, images) { |
261 | 260 | color = getColor(decl.value); |
262 | 261 |
|
263 | 262 | if (color) { |
264 | | - backgroundColorDecl = postcss.decl({ |
| 263 | + decl.cloneAfter({ |
265 | 264 | prop: 'background-color', |
266 | | - value: getColor(decl.value) |
267 | | - }); |
268 | | - backgroundColorDecl.raws.before = ONE_SPACE; |
269 | | - |
270 | | - rule.append(backgroundColorDecl); |
| 265 | + value: color |
| 266 | + }).raws.before = ONE_SPACE; |
271 | 267 | } |
272 | 268 | } |
273 | 269 |
|
274 | 270 | // Replace with comment token |
275 | | - if (decl.prop === BACKGROUND || decl.prop === BACKGROUND_IMAGE) { |
276 | | - commentDecl = postcss.comment({ |
| 271 | + if (_.includes([BACKGROUND, BACKGROUND_IMAGE], decl.prop)) { |
| 272 | + commentDecl = decl.cloneAfter({ |
| 273 | + type: 'comment', |
277 | 274 | text: image.url |
278 | 275 | }); |
279 | 276 |
|
280 | 277 | commentDecl.raws.left = `${ONE_SPACE}${COMMENT_TOKEN_PREFIX}`; |
281 | 278 | image.token = commentDecl.toString(); |
282 | 279 |
|
283 | | - decl.replaceWith(commentDecl); |
| 280 | + decl.remove(); |
284 | 281 | } |
285 | 282 | } |
286 | 283 | } |
@@ -452,25 +449,17 @@ export function updateRule(rule, token, image) { |
452 | 449 | const sizeX = spriteWidth / ratio; |
453 | 450 | const sizeY = spriteHeight / ratio; |
454 | 451 |
|
455 | | - const backgroundImageDecl = postcss.decl({ |
| 452 | + token.cloneAfter({ |
| 453 | + type: 'decl', |
456 | 454 | prop: 'background-image', |
457 | 455 | value: `url(${spriteUrl})` |
458 | | - }); |
459 | | - |
460 | | - const backgroundPositionDecl = postcss.decl({ |
| 456 | + }).cloneAfter({ |
461 | 457 | prop: 'background-position', |
462 | 458 | value: `${posX}px ${posY}px` |
463 | | - }); |
464 | | - |
465 | | - rule.insertAfter(token, backgroundImageDecl); |
466 | | - rule.insertAfter(backgroundImageDecl, backgroundPositionDecl); |
467 | | - |
468 | | - const backgroundSizeDecl = postcss.decl({ |
| 459 | + }).cloneAfter({ |
469 | 460 | prop: 'background-size', |
470 | 461 | value: `${sizeX}px ${sizeY}px` |
471 | 462 | }); |
472 | | - |
473 | | - rule.insertAfter(backgroundPositionDecl, backgroundSizeDecl); |
474 | 463 | } |
475 | 464 |
|
476 | 465 | ///////////////////////// |
|
0 commit comments