/
sources-plugin.js
197 lines (158 loc) · 5.11 KB
/
sources-plugin.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { parse } from "parse5";
import {
traverse,
getFilter,
requestify,
webpackIgnoreCommentRegexp,
} from "../utils";
export default (options) =>
function process(html) {
const sources = [];
const document = parse(html, {
sourceCodeLocationInfo: true,
scriptingEnabled: options.sources.scriptingEnabled,
});
let needIgnore = false;
traverse(document, (node) => {
const { tagName, attrs: attributes, sourceCodeLocation } = node;
if (node.nodeName === "#comment") {
const match = node.data.match(webpackIgnoreCommentRegexp);
if (match) {
needIgnore = match[2] === "true";
}
return;
}
if (!tagName) {
return;
}
if (needIgnore) {
needIgnore = false;
return;
}
attributes.forEach((attribute) => {
let { name } = attribute;
name = attribute.prefix ? `${attribute.prefix}:${name}` : name;
const handlers = new Map([
...(options.sources.list.get("*") || new Map()),
...(options.sources.list.get(tagName.toLowerCase()) || new Map()),
]);
if (handlers.size === 0) {
return;
}
const handler = handlers.get(name.toLowerCase());
if (!handler) {
return;
}
if (
handler.filter &&
!handler.filter(tagName, name, attributes, options.resourcePath)
) {
return;
}
const attributeAndValue = html.slice(
sourceCodeLocation.attrs[name].startOffset,
sourceCodeLocation.attrs[name].endOffset
);
const isValueQuoted =
attributeAndValue[attributeAndValue.length - 1] === '"' ||
attributeAndValue[attributeAndValue.length - 1] === "'";
const valueStartOffset =
sourceCodeLocation.attrs[name].startOffset +
attributeAndValue.indexOf(attribute.value);
const valueEndOffset =
sourceCodeLocation.attrs[name].endOffset - (isValueQuoted ? 1 : 0);
const optionsForTypeFn = {
tag: tagName,
startTag: {
startOffset: sourceCodeLocation.startTag.startOffset,
endOffset: sourceCodeLocation.startTag.endOffset,
},
endTag: sourceCodeLocation.endTag
? {
startOffset: sourceCodeLocation.endTag.startOffset,
endOffset: sourceCodeLocation.endTag.endOffset,
}
: // eslint-disable-next-line no-undefined
undefined,
attributes,
attribute: name,
attributePrefix: attribute.prefix,
attributeNamespace: attribute.namespace,
attributeStartOffset: sourceCodeLocation.attrs[name].startOffset,
attributeEndOffset: sourceCodeLocation.attrs[name].endOffset,
value: attribute.value,
isValueQuoted,
valueEndOffset,
valueStartOffset,
html,
};
let result;
try {
result = handler.type(optionsForTypeFn);
} catch (error) {
options.errors.push(error);
}
result = Array.isArray(result) ? result : [result];
for (const source of result) {
if (!source) {
// eslint-disable-next-line no-continue
continue;
}
sources.push({ ...source, name, isValueQuoted });
}
});
});
const urlFilter = getFilter(options.sources.urlFilter);
const imports = new Map();
const replacements = new Map();
let offset = 0;
for (const source of sources) {
const {
name,
value,
isValueQuoted,
format,
runtime,
startOffset,
endOffset,
} = source;
let request = value;
if (!urlFilter(name, value, options.resourcePath)) {
// eslint-disable-next-line no-continue
continue;
}
let hash;
const indexHash = request.lastIndexOf("#");
if (indexHash >= 0) {
hash = request.substring(indexHash);
request = request.substring(0, indexHash);
}
request = requestify(options.context, request);
let importName = imports.get(request);
if (!importName) {
importName = `___HTML_LOADER_IMPORT_${imports.size}___`;
imports.set(request, importName);
options.imports.push({ format, importName, request });
}
const replacementKey = JSON.stringify({ request, isValueQuoted, hash });
let replacementName = replacements.get(replacementKey);
if (!replacementName) {
replacementName = `___HTML_LOADER_REPLACEMENT_${replacements.size}___`;
replacements.set(replacementKey, replacementName);
options.replacements.push({
replacementName,
importName,
hash,
isValueQuoted,
runtime,
});
}
// eslint-disable-next-line no-param-reassign
html =
html.slice(0, startOffset + offset) +
replacementName +
html.slice(endOffset + offset);
offset += startOffset + replacementName.length - endOffset;
}
return html;
};