-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
187 lines (155 loc) · 4.5 KB
/
index.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
'use strict';
var path = require('path');
var fs = require('fs');
var crypto = require('crypto');
var postcss = require('postcss');
var async = require('async');
var when = require('when');
var phantomjs = require('phantomjs');
var childProcess = require('child_process');
var phantomjsScript = path.resolve(__dirname, './phantomjs-script.js');
var backgroundImageRegex = /url\(('|")?(([^\1]+\.svg)|(data:image\/svg\+xml[;,][^\1]+))\1\)/;
var backgroundSizeRegex = /^(\d+)px( (\d+)px)?$/;
function hash(input) {
return crypto.createHash('md5').update(input).digest('hex');
}
module.exports = postcss.plugin('postcss-svg-fallback', function(options) {
var fallbackSelector;
var disableConvert;
options = options || {};
fallbackSelector = options.fallbackSelector || '.no-svg';
disableConvert = options.disableConvert || false;
return function (css, result) {
var images = [];
var rulesMethodName = !!css.walkRules ? 'walkRules' : 'eachRule';
var declsMethodName = !!css.walkRules ? 'walkDecls' : 'eachDecl';
css[rulesMethodName](function(rule) {
var inlineBackground;
var backgroundImage;
var backgroundSize;
var newImage;
var newRule;
var newDecl;
var matchedBackgroundImageDecl;
var suffix;
var newSelectors;
// skip our added rules
if (rule.selector.indexOf(fallbackSelector) !== -1) {
return;
}
rule[declsMethodName](function(decl) {
var backgroundImageMatch;
var backgroundSizeMatch;
if (decl.prop.match(/^background(-image)?$/)) {
backgroundImageMatch = backgroundImageRegex.exec(decl.value);
if (backgroundImageMatch) {
matchedBackgroundImageDecl = decl;
if (backgroundImageMatch[3]) {
inlineBackground = false;
backgroundImage = backgroundImageMatch[3];
} else {
inlineBackground = true;
backgroundImage = backgroundImageMatch[4];
}
}
}
if (decl.prop === 'background-size') {
backgroundSizeMatch = backgroundSizeRegex.exec(decl.value);
if (backgroundSizeMatch) {
backgroundSize = {
width: parseInt(backgroundSizeMatch[1]),
height: parseInt(backgroundSizeMatch[3] || backgroundSizeMatch[1]),
};
}
}
});
if (backgroundImage && backgroundSize) {
suffix = '-' + backgroundSize.width + 'x' + backgroundSize.height + '.png';
if (inlineBackground) {
newImage = hash(backgroundImage) + suffix;
} else {
newImage = backgroundImage.replace(/\.svg$/, suffix);
}
images.push({
inline: inlineBackground,
postcssResult: result,
postcssRule: rule,
image: backgroundImage,
newImage: newImage,
size: backgroundSize,
});
newSelectors = rule.selectors.map(function(selector) {
return fallbackSelector + ' ' + selector;
});
newRule = postcss.rule({ selectors: newSelectors });
newRule.source = rule.source;
newDecl = postcss.decl({
prop: 'background-image',
value: 'url(' + newImage + ')',
});
newDecl.source = matchedBackgroundImageDecl.source;
newRule.append(newDecl);
rule.parent.insertAfter(rule, newRule);
}
});
if (disableConvert) {
return when.resolve();
}
return when.promise(function(resolve, reject) {
async.eachSeries(images, processImage.bind(null, options), function(err) {
if (err) {
reject(err);
} else {
resolve();
}
});
});
};
});
function processImage(options, image, cb) {
var source = path.join(options.basePath || '', image.image);
var dest = path.join(options.dest || '', image.newImage);
var args = [
phantomjsScript,
image.inline ? image.image : source,
image.size.width,
image.size.height,
dest,
];
if (image.inline) {
statDest('inline', dest, args, cb);
} else {
fs.stat(source, function(sourceErr, sourceStat) {
if (sourceStat) {
statDest(sourceStat, dest, args, cb);
} else {
image.postcssResult.warn(
'Could not find "' + image.image + '" at "' + source + '"',
{ node: image.postcssRule });
cb();
}
});
}
}
function statDest(sourceStat, dest, args, cb) {
fs.stat(dest, function(destErr, destStat) {
if (!destStat || !sourceStat || sourceStat !== 'inline' || sourceStat.mtime > destStat.mtime) {
runPhantomJs(args, cb);
} else {
cb();
}
});
}
function runPhantomJs(args, cb) {
childProcess.execFile(phantomjs.path, args, function(err, stdout, stderr) {
if (err) {
cb(err);
} else if (stdout.length) {
cb(stdout.toString().trim());
} else if (stderr.length) {
cb(stderr.toString().trim());
} else {
cb();
}
});
}