forked from webpack/html-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
142 lines (132 loc) · 4.03 KB
/
index.js
File metadata and controls
142 lines (132 loc) · 4.03 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var htmlMinifier = require("html-minifier");
var attrParse = require("./lib/attributesParser");
var loaderUtils = require("loader-utils");
var url = require("url");
var assign = require("object-assign");
var compile = require("es6-templates").compile;
function randomIdent() {
return "xxxHTMLLINKxxx" + Math.random() + Math.random() + "xxx";
}
function getLoaderConfig(context) {
var query = loaderUtils.parseQuery(context.query);
var configKey = query.config || 'htmlLoader';
var config = context.options && context.options.hasOwnProperty(configKey) ? context.options[configKey] : {};
delete query.config;
return assign(query, config);
}
module.exports = function(content) {
this.cacheable && this.cacheable();
var config = getLoaderConfig(this);
var attributes = ["img:src", "img:srcset"];
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 rawLinks = attrParse(content, function(tag, attr) {
return attributes.indexOf(tag + ":" + attr) >= 0;
});
var links = [];
rawLinks.forEach(function (link) {
var length = link.length;
var start = link.start;
var valueList = link.value.split(",");
valueList.forEach(function (newLink) {
var trimmed = newLink.trim();
var cLength = newLink.length;
var spacePos = trimmed.indexOf(" ");
var spaceStart = newLink.indexOf(trimmed);
var len = cLength+ spaceStart;
if (-1 != spacePos) {
len = spacePos + spaceStart;
trimmed = trimmed.substring(0,spacePos);
}
links.push({start: start, length: len , value: trimmed});
start += cLength+1;
});
});
links.reverse();
var data = {};
content = [content];
links.forEach(function(link) {
var newValue1 = link.value.split(",");
var newValue = newValue1.map(function (value) {
var valueArray = value.trim().split(" ");
var obj = {
value: valueArray.shift(),
additional: valueArray,
};
if(!loaderUtils.isUrlRequest(obj.value, root)) return;
var uri = url.parse(obj.value);
if (uri.hash !== null && uri.hash !== undefined) {
obj.hash = uri.hash;
uri.hash = null;
obj.value = uri.format();
}
return obj;
}).filter(function (value) {
return value !== undefined;
});
if (newValue.length > 0)
{
do {
var ident = randomIdent();
} while(data[ident]);
data[ident] = newValue;
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("");
if(typeof config.minimize === "boolean" ? config.minimize : this.minimize) {
var minimizeOptions = assign({}, config);
[
"removeComments",
"removeCommentsFromCDATA",
"removeCDATASectionsFromCDATA",
"collapseWhitespace",
"conservativeCollapse",
"removeAttributeQuotes",
"useShortDoctype",
"keepClosingSlash",
"minifyJS",
"minifyCSS",
"removeScriptTypeAttributes",
"removeStyleTypeAttributes",
].forEach(function(name) {
if(typeof minimizeOptions[name] === "undefined") {
minimizeOptions[name] = true;
}
});
content = htmlMinifier.minify(content, minimizeOptions);
}
if(config.interpolate) {
content = compile('`' + content + '`').code;
} else {
content = JSON.stringify(content);
}
return "module.exports = " + content.replace(/xxxHTMLLINKxxx[0-9\.]+xxx/g, function(match) {
if(!data[match]) return match;
return data[match].reduce(function (pV,cV, index, array) {
var hash = cV.hash || "";
var additional = cV.additional.length != 0 ? " " + cV.additional.join(" ") : "";
if (index != array.length -1) {
additional += ",";
}
return pV + '" + require(' + JSON.stringify(loaderUtils.urlToRequest(cV.value, root)) + ') + "' + hash + additional;
},"");
}) + ";";
}