Skip to content

Commit 198125d

Browse files
author
Jed Mao
committed
Return early
1 parent 62c78e2 commit 198125d

File tree

1 file changed

+37
-32
lines changed

1 file changed

+37
-32
lines changed

src/core.js

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -246,41 +246,46 @@ export function setTokens(root, opts, images) {
246246
const ruleStr = rule.toString();
247247
let url, image, color, backgroundColorDecl, commentDecl;
248248

249+
if (!hasImageInRule(ruleStr)) {
250+
return;
251+
}
252+
249253
// Manipulate only rules with image in them
250-
if (hasImageInRule(ruleStr)) {
251-
url = getImageUrl(ruleStr)[1];
252-
image = _.find(images, { url });
253-
254-
if (image) {
255-
// Remove all necessary background declarations
256-
rule.walkDecls(/^background-(repeat|size|position)$/, decl => decl.remove());
257-
258-
// Extract color to background-color property
259-
if (decl.prop === BACKGROUND) {
260-
color = getColor(decl.value);
261-
262-
if (color) {
263-
decl.cloneAfter({
264-
prop: 'background-color',
265-
value: color
266-
}).raws.before = ONE_SPACE;
267-
}
268-
}
269-
270-
// Replace with comment token
271-
if (_.includes([BACKGROUND, BACKGROUND_IMAGE], decl.prop)) {
272-
commentDecl = decl.cloneAfter({
273-
type: 'comment',
274-
text: image.url
275-
});
276-
277-
commentDecl.raws.left = `${ONE_SPACE}${COMMENT_TOKEN_PREFIX}`;
278-
image.token = commentDecl.toString();
279-
280-
decl.remove();
281-
}
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;
282274
}
283275
}
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+
}
284289
});
285290

286291
resolve([root, opts, images]);

0 commit comments

Comments
 (0)