-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (59 loc) · 1.97 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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Original Author Tobias Koppers @sokra
Modified by aztack
*/
var attrParse = require("./lib/attributesParser");
var loaderUtils = require("loader-utils");
var assign = require("object-assign");
var url = require("url");
function randomIdent() {
return 'xxxJADELINKxxx' + Math.random() + Math.random() + "xxx";
}
module.exports = function(content) {
this.cacheable && this.cacheable();
var config = loaderUtils.getOptions(this) || {};
var attributes = ["img:src", "script:src", "link:href"];
if(config.attrs !== undefined) {
if(typeof config.attrs === "string")
attributes = config.attrs.split(" ");
else if(Array.isArray(config.attrs))
attributes = config.attrs;
else if(config.attrs === false)
attributes = [];
else
throw new Error("Invalid value to config parameter attrs");
}
var root = config.root;
var links = attrParse(content, function(tag, attr) {
return attributes.indexOf(tag + ":" + attr) >= 0;
});
links.reverse();
var data = {};
content = [content];
links.forEach(function(link) {
var uri = url.parse(link.value);
if (uri.hash !== null && uri.hash !== undefined) {
link.value = decodeURIComponent(uri.format());
uri.hash = null;
link.length = link.value.length;
}
do {
var ident = randomIdent();
} while(data[ident]);
data[ident] = link.value;
var x = content.pop();
content.push(x.substr(link.start + link.length));
content.push(ident);
content.push(x.substr(0, link.start));
});
content.reverse();
content = content.join("");
content = JSON.stringify(content);
var getEmitedFilePath = config.getEmitedFilePath || function (v) { return v;}
var res = "module.exports = " + content.replace(/xxxJADELINKxxx[0-9\.]+xxx/g, function(match) {
if(!data[match]) return '';
return '" + ' + JSON.stringify(getEmitedFilePath(data[match]), root) + ' + "';
}) + ";";
return res
}