Skip to content

Commit 2945541

Browse files
committed
Improve: abstract token replacement in standalone function
1 parent 06af1b3 commit 2945541

File tree

1 file changed

+65
-36
lines changed

1 file changed

+65
-36
lines changed

index.js

Lines changed: 65 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ function plugin(opts) {
7979
.all([getImages(css, options), options])
8080
.spread(applyFilterBy)
8181
.spread(applyGroupBy)
82+
.spread(function(images, opts){
83+
return setTokens(images, opts, css);
84+
})
8285
.spread(runSpriteSmith)
8386
.spread(saveSprites)
8487
.spread(mapSpritesProperties)
@@ -123,49 +126,13 @@ function getImages(css, opts) {
123126
token : '',
124127
selector: null
125128
};
126-
var color;
127129

128130
// Manipulate only rules with background image
129131
// in them.
130132
if (hasImageInRule(rule.toString())) {
131133
image.url = getImageUrl(rule.toString());
132134

133135
if (isImageSupported(image.url)) {
134-
rule.eachDecl(/^background/, function(decl) {
135-
// We remove these declarations since
136-
// our plugin will insert them when
137-
// they are necessary.
138-
if (decl.prop === BACKGROUND_REPEAT || decl.prop === BACKGROUND_SIZE) {
139-
decl.removeSelf();
140-
}
141-
142-
if (decl.prop === BACKGROUND) {
143-
color = getColor(decl);
144-
145-
// Extract color to background-color propery
146-
if (color && color.length === 1) {
147-
rule.append({
148-
prop: 'background-color',
149-
value: color[0],
150-
before: ' '
151-
});
152-
}
153-
}
154-
155-
if (decl.prop === BACKGROUND || decl.prop === BACKGROUND_IMAGE) {
156-
image.token = postcss.comment({
157-
text: image.url,
158-
before: ' ',
159-
left: '@replace|',
160-
right: ''
161-
});
162-
163-
// Replace the declaration with a comment token
164-
// which will be used later for reference.
165-
decl.replaceWith(image.token);
166-
}
167-
});
168-
169136
// Perform search for retina
170137
// images if option is allowed.
171138
if (opts.retina && isRetinaImage(image.url)) {
@@ -307,6 +274,68 @@ function applyGroupBy(images, opts) {
307274
});
308275
}
309276

277+
/**
278+
* Replace declarations with comment tokens.
279+
*
280+
* @param {Array} images
281+
* @param {Object} opts
282+
* @param {Object} css
283+
* @return {Promise}
284+
*/
285+
function setTokens(images, opts, css) {
286+
return Q.Promise(function(resolve, reject) {
287+
css.eachRule(function(rule) {
288+
var url, image, color;
289+
290+
// Manipulate only rules with background image
291+
// in them.
292+
if (hasImageInRule(rule.toString())) {
293+
url = getImageUrl(rule.toString());
294+
image = lodash.find(images, { url: url });
295+
296+
if (image) {
297+
rule.eachDecl(/^background/, function(decl) {
298+
// We remove these declarations since
299+
// our plugin will insert them when
300+
// they are necessary.
301+
if (decl.prop === BACKGROUND_REPEAT || decl.prop === BACKGROUND_SIZE) {
302+
decl.removeSelf();
303+
}
304+
305+
if (decl.prop === BACKGROUND) {
306+
color = getColor(decl);
307+
308+
// Extract color to background-color propery
309+
if (color && color.length === 1) {
310+
rule.append({
311+
prop: 'background-color',
312+
value: color[0],
313+
before: ' '
314+
});
315+
}
316+
}
317+
318+
if (decl.prop === BACKGROUND || decl.prop === BACKGROUND_IMAGE) {
319+
image.token = postcss.comment({
320+
text: image.url,
321+
before: ' ',
322+
left: '@replace|',
323+
right: ''
324+
});
325+
326+
// Replace the declaration with a comment token
327+
// which will be used later for reference.
328+
decl.replaceWith(image.token);
329+
}
330+
});
331+
}
332+
}
333+
});
334+
335+
resolve([images, opts]);
336+
});
337+
}
338+
310339
/**
311340
* Process the images with spritesmith module.
312341
*

0 commit comments

Comments
 (0)