Skip to content

Commit 67e9b7a

Browse files
authored
Merge pull request #90 from jedmao/iss89
Preserve PostCSS declaration sources
2 parents bf83fb9 + 198125d commit 67e9b7a

File tree

1 file changed

+41
-47
lines changed

1 file changed

+41
-47
lines changed

src/core.js

+41-47
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';
@@ -247,43 +246,46 @@ export function setTokens(root, opts, images) {
247246
const ruleStr = rule.toString();
248247
let url, image, color, backgroundColorDecl, commentDecl;
249248

249+
if (!hasImageInRule(ruleStr)) {
250+
return;
251+
}
252+
250253
// 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;
285274
}
286275
}
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+
}
287289
});
288290

289291
resolve([root, opts, images]);
@@ -452,25 +454,17 @@ export function updateRule(rule, token, image) {
452454
const sizeX = spriteWidth / ratio;
453455
const sizeY = spriteHeight / ratio;
454456

455-
const backgroundImageDecl = postcss.decl({
457+
token.cloneAfter({
458+
type: 'decl',
456459
prop: 'background-image',
457460
value: `url(${spriteUrl})`
458-
});
459-
460-
const backgroundPositionDecl = postcss.decl({
461+
}).cloneAfter({
461462
prop: 'background-position',
462463
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({
469465
prop: 'background-size',
470466
value: `${sizeX}px ${sizeY}px`
471467
});
472-
473-
rule.insertAfter(backgroundPositionDecl, backgroundSizeDecl);
474468
}
475469

476470
/////////////////////////

0 commit comments

Comments
 (0)