forked from MadLittleMods/gulp-css-spriter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform-file-with-sprite-sheet-data.js
More file actions
77 lines (58 loc) · 2.39 KB
/
transform-file-with-sprite-sheet-data.js
File metadata and controls
77 lines (58 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
var path = require('path');
var extend = require('extend');
var css = require('css');
var spriterUtil = require('./spriter-util');
var mapOverStylesAndTransformBackgroundImageDeclarations = require('./map-over-styles-and-transform-background-image-declarations');
var backgroundURLMatchAllRegex = new RegExp(spriterUtil.backgroundURLRegex.source, "gi");
// Replace all the paths that need replacing
function transformFileWithSpriteSheetData(vinylFile, coordinateMap, pathToSpriteSheetFromCSS, /*optional*/includeMode, /*optional*/isSilent, /*optional*/outputIndent) {
includeMode = includeMode ? includeMode : 'implicit';
isSilent = (isSilent !== undefined) ? isSilent : false;
outputIndent = outputIndent ? outputIndent : '\t';
// Clone the declartion to keep it immutable
var resultantFile = vinylFile.clone();
if(resultantFile) {
var styles = css.parse(String(resultantFile.contents), {
'silent': isSilent,
'source': vinylFile.path
});
styles = mapOverStylesAndTransformBackgroundImageDeclarations(styles, includeMode, function(declaration) {
var coordList = [];
declaration.value = spriterUtil.matchBackgroundImages(declaration.value, function(imagePath) {
var coords = coordinateMap[path.join(path.dirname(resultantFile.path), imagePath)];
//console.log('coords', coords);
// Make sure there are coords for this image in the sprite sheet, otherwise we won't include it
if(coords) {
coordList.push("-" + coords.x + "px -" + coords.y + "px");
// If there are coords in the spritemap for this image, lets use the spritemap
return pathToSpriteSheetFromCSS;
}
return imagePath;
});
return {
'value': declaration,
/* */
// Add the appropriate background position according to the spritemap
'insertElements': (function() {
if(coordList.length > 0) {
return {
type: 'declaration',
property: 'background-position',
value: coordList.join(', ')
};
}
})()
/* */
};
});
//console.log(styles.stylesheet.rules[0].declarations);
// Put it back into string form
var resultantContents = css.stringify(styles, {
indent: outputIndent
});
//console.log(resultantContents);
resultantFile.contents = new Buffer(resultantContents);
}
return resultantFile;
}
module.exports = transformFileWithSpriteSheetData;