Skip to content

Commit 62c78e2

Browse files
author
Jed Mao
committed
Preserve PostCSS declaration sources
1 parent bf83fb9 commit 62c78e2

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

src/core.js

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import path from 'path';
22
import fs from 'fs-extra';
3-
import postcss from 'postcss';
43
import Promise from 'bluebird';
54
import _ from 'lodash';
65
import debug from 'debug';
@@ -261,26 +260,24 @@ export function setTokens(root, opts, images) {
261260
color = getColor(decl.value);
262261

263262
if (color) {
264-
backgroundColorDecl = postcss.decl({
263+
decl.cloneAfter({
265264
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;
271267
}
272268
}
273269

274270
// 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',
277274
text: image.url
278275
});
279276

280277
commentDecl.raws.left = `${ONE_SPACE}${COMMENT_TOKEN_PREFIX}`;
281278
image.token = commentDecl.toString();
282279

283-
decl.replaceWith(commentDecl);
280+
decl.remove();
284281
}
285282
}
286283
}
@@ -452,25 +449,17 @@ export function updateRule(rule, token, image) {
452449
const sizeX = spriteWidth / ratio;
453450
const sizeY = spriteHeight / ratio;
454451

455-
const backgroundImageDecl = postcss.decl({
452+
token.cloneAfter({
453+
type: 'decl',
456454
prop: 'background-image',
457455
value: `url(${spriteUrl})`
458-
});
459-
460-
const backgroundPositionDecl = postcss.decl({
456+
}).cloneAfter({
461457
prop: 'background-position',
462458
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({
469460
prop: 'background-size',
470461
value: `${sizeX}px ${sizeY}px`
471462
});
472-
473-
rule.insertAfter(backgroundPositionDecl, backgroundSizeDecl);
474463
}
475464

476465
/////////////////////////

0 commit comments

Comments
 (0)