/
loader.js
61 lines (46 loc) · 1.71 KB
/
loader.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
/*
* @license MIT http://www.opensource.org/licenses/mit-license.php
* @author Hovhannes Babayan <bhovhannes at gmail dot com>
*/
var loaderUtils = require('loader-utils');
var REGEX_STYLE = /<style[\s\S]*?>[\s\S]*?<\/style>/i
var REGEX_DECLARATION = /^\s*<\?xml [^>]*>\s*/i
var REGEX_DOUBLE_QUOTE = /"/g
var REGEX_MULTIPLE_SPACES = /\s+/g
var REGEX_UNSAFE_CHARS = /[{}\|\\\^~\[\]`"<>#%]/g
module.exports = function(content) {
this.cacheable && this.cacheable();
var query = loaderUtils.getOptions(this) || {};
query.encoding = query.encoding || "none";
var limit = query.limit ? parseInt(query.limit, 10) : 0;
if (limit <= 0 || content.length < limit) {
var newContent = content.toString('utf8');
var hasStyleElement = REGEX_STYLE.test(newContent)
if (query.stripdeclarations) {
newContent = newContent.replace(REGEX_DECLARATION, "");
}
var data;
if (query.encoding === "base64") {
if (typeof newContent === "string") {
newContent = new Buffer(newContent);
}
data = "data:image/svg+xml;base64," + newContent.toString("base64");
} else {
newContent = newContent.replace(REGEX_DOUBLE_QUOTE, "'");
newContent = newContent.replace(REGEX_MULTIPLE_SPACES, " ");
newContent = newContent.replace(REGEX_UNSAFE_CHARS, function(match) {
return '%'+match[0].charCodeAt(0).toString(16).toUpperCase();
});
data = 'data:image/svg+xml,' + newContent.trim();
}
if (!(query.iesafe && hasStyleElement && data.length > 4096)) {
// if (query.encoding === "none" && !query.noquotes) {
// data = '"'+data+'"';
// }
return 'module.exports = ' + JSON.stringify(data);
}
}
var fileLoader = require('file-loader');
return fileLoader.call(this, content);
};
module.exports.raw = true;