-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (36 loc) · 1.47 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
let postcss = require('postcss');
var SVG = require('./lib/svg');
module.exports = postcss.plugin('postcss-embed-svg', (opts = {}) => {
SVG.init(opts);
return (root, result) => {
root.walkRules( (rule) => {
rule.walkDecls(/^background|^filter|^content|image$/, (decl) => {
var matches = null;
var replace = null;
var datauri = null;
var svg = null;
var funcName = opts.func || 'svg';
var SVGRegExp = new RegExp(`${funcName}\\(\"([^\"]+)\"(,\\s*\"([^\"]+)\")?\\)`);
var replaceRegExp = new RegExp(`${funcName}\\((\"[^\"]+\"|\'[^\']+\')(,\\s*(\"[^\"]+\"|\'[^\']+\'))?\\)`);
if( ! decl.value ) {
return;
}
while( matches = SVGRegExp.exec(decl.value.replace(/'/g, '"')) )
{
[___, name, ...params] = matches;
try {
svg = SVG.get(name, params[1]);
} catch (error) {
decl.warn(result, `postcss-embed-svg: ${error}`);
}
if( !svg ) {
return;
}
replace = replaceRegExp.exec(decl.value)[0];
datauri = `url("${svg}")`;
decl.value = decl.value.replace(replace, datauri);
}
});
});
};
});