-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
36 lines (33 loc) · 1.14 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
var fs = require('fs');
var path = require('path');
var sass = require('node-sass');
var mime = require('mime');
var replaceall = require('replaceall');
var inlineImage = function(filepath, replace, done) {
var mimeType = mime.lookup(filepath);
if (mimeType !== 'image/svg+xml') {
throw new Error('File ' + filepath + ' is not of type image/svg+xml.');
} else {
var data = fs.readFileSync(filepath);
if (!data || !data.length) {
throw new Error('File ' + filepath + ' is empty or cannot be read')
}
var result = data.toString('utf8');
if (replace.getLength()) {
for (var i = 0; i < replace.getLength(); i++) {
result = replaceall(replace.getKey(i).getValue(), replace.getValue(i).getValue(), result);
}
}
done(result);
}
}
module.exports = function() {
return {
'inline-svg($filename, $replace: ())': function(filename, replace, done) {
inlineImage(filename.getValue(), replace, function(dataUrl) {
var encodedUrl = encodeURIComponent(dataUrl);
done(new sass.types.String('url(\'data:image/svg+xml;charset=utf-8,' + encodedUrl + '\')'));
});
}
}
}