-
Notifications
You must be signed in to change notification settings - Fork 0
/
html-loader.js
136 lines (120 loc) · 3.77 KB
/
html-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
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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
const attrParse = require('./lib/attributesParser');
const loaderUtils = require('loader-utils');
const url = require('url');
const assign = require('object-assign');
function randomIdent() {
return `xxxHTMLLINKxxx${Math.random()}${Math.random()}xxx`;
}
function getLoaderConfig(context) {
const query = loaderUtils.getOptions(context) || {};
const configKey = query.config || 'htmlLoader';
const 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();
const config = getLoaderConfig(this);
let attributes = ['img:src'];
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');
}
}
const root = config.root;
const links = attrParse(content, (tag, attr) => {
const res = attributes.find(a => {
if (a.charAt(0) === ':') {
return attr === a.slice(1);
} else {
return (`${tag}:${attr}`) === a;
}
});
return !!res;
});
links.reverse();
const data = {};
content = [content];
links.forEach(link => {
if (!loaderUtils.isUrlRequest(link.value, root)) return;
if (link.value.indexOf('mailto:') > -1) return;
const uri = url.parse(link.value);
if (uri.hash !== null && uri.hash !== undefined) {
uri.hash = null;
link.value = uri.format();
link.length = link.value.length;
}
let ident;
do {
ident = randomIdent();
} while (data[ident]);
data[ident] = link.value;
const 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 (config.interpolate === 'require') {
const reg = /\$\{require\([^)]*\)\}/g;
let result;
const reqList = [];
while (result = reg.exec(content)) {
reqList.push({
length: result[0].length,
start: result.index,
value: result[0],
});
}
reqList.reverse();
content = [content];
reqList.forEach(link => {
const x = content.pop();
let ident;
do {
ident = randomIdent();
} while (data[ident]);
data[ident] = link.value.substring(11, link.length - 3);
content.push(x.substr(link.start + link.length));
content.push(ident);
content.push(x.substr(0, link.start));
});
content.reverse();
content = content.join('');
}
if (config.interpolate && config.interpolate !== 'require') {
// Double escape quotes so that they are not unescaped completely in the template string
content = content.replace(/\\"/g, '\\\\"');
content = content.replace(/\\'/g, '\\\\\'');
} else {
content = JSON.stringify(content);
}
let exportsString = 'module.exports = ';
if (config.exportAsDefault) {
exportsString = 'exports.default = ';
} else if (config.exportAsEs6Default) {
exportsString = 'export default ';
}
const contentVar = content.replace(/xxxHTMLLINKxxx[0-9\.]+xxx/g, match => {
if (!data[match]) return match;
let urlToRequest;
if (config.interpolate === 'require') {
urlToRequest = data[match];
} else {
urlToRequest = loaderUtils.urlToRequest(data[match], root);
}
return `" + require(${JSON.stringify(urlToRequest)}) + "`;
});
return `${exportsString + contentVar};`;
};