-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
21 lines (19 loc) · 776 Bytes
/
index.js
File metadata and controls
21 lines (19 loc) · 776 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
var postcss = require('postcss'),
mime = require('mime'),
fs = require('fs');
module.exports = postcss.plugin('postcss-data-uri', function (opts) {
return function (css) {
css.walkDecls(/background(\-image)?/i, function (decl) {
var rePattern = /data-uri\(\s*['|"]?(.*?)['|"]?\s*\)/i,
path;
if (rePattern.test(decl.value)) {
path = decl.value.match(rePattern)[1];
if (fs.existsSync(path)) {
decl.value = decl.value.replace(rePattern, 'url(data:' + mime.lookup(path) + ';base64,' + fs.readFileSync(path, 'base64') + ')');
} else {
console.log('Image ',path,' not found.')
}
}
});
};
});