1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.c0ControlCodesExclude = c0ControlCodesExclude;
|
7 | exports.defaultMinimizerOptions = void 0;
|
8 | exports.getExportCode = getExportCode;
|
9 | exports.getFilter = getFilter;
|
10 | exports.getImportCode = getImportCode;
|
11 | exports.getModuleCode = getModuleCode;
|
12 | exports.isUrlRequestable = isUrlRequestable;
|
13 | exports.normalizeOptions = normalizeOptions;
|
14 | exports.parseSrc = parseSrc;
|
15 | exports.parseSrcset = parseSrcset;
|
16 | exports.pluginRunner = pluginRunner;
|
17 | exports.requestify = requestify;
|
18 | exports.srcType = srcType;
|
19 | exports.srcsetType = srcsetType;
|
20 | exports.traverse = traverse;
|
21 | exports.webpackIgnoreCommentRegexp = void 0;
|
22 |
|
23 | var _path = _interopRequireDefault(require("path"));
|
24 |
|
25 | var _HtmlSourceError = _interopRequireDefault(require("./HtmlSourceError"));
|
26 |
|
27 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
28 |
|
29 | function isASCIIWhitespace(character) {
|
30 | return (
|
31 | character === "\u0009" ||
|
32 | character === "\u000A" ||
|
33 | character === "\u000C" ||
|
34 | character === "\u000D" ||
|
35 | character === "\u0020"
|
36 | );
|
37 | }
|
38 |
|
39 |
|
40 |
|
41 | const regexLeadingSpaces = /^[ \t\n\r\u000c]+/;
|
42 |
|
43 | const regexLeadingCommasOrSpaces = /^[, \t\n\r\u000c]+/;
|
44 |
|
45 | const regexLeadingNotSpaces = /^[^ \t\n\r\u000c]+/;
|
46 | const regexTrailingCommas = /[,]+$/;
|
47 | const regexNonNegativeInteger = /^\d+$/;
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | const regexFloatingPoint = /^-?(?:[0-9]+|[0-9]*\.[0-9]+)(?:[eE][+-]?[0-9]+)?$/;
|
54 |
|
55 | function parseSrcset(input) {
|
56 |
|
57 | const inputLength = input.length;
|
58 | let url;
|
59 | let descriptors;
|
60 | let currentDescriptor;
|
61 | let state;
|
62 | let c;
|
63 |
|
64 |
|
65 | let position = 0;
|
66 | let startOffset;
|
67 |
|
68 | function collectCharacters(regEx) {
|
69 | let chars;
|
70 | const match = regEx.exec(input.substring(position));
|
71 |
|
72 | if (match) {
|
73 | [chars] = match;
|
74 | position += chars.length;
|
75 | return chars;
|
76 | }
|
77 | }
|
78 |
|
79 |
|
80 | const candidates = [];
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | while (true) {
|
86 | collectCharacters(regexLeadingCommasOrSpaces);
|
87 |
|
88 | if (position >= inputLength) {
|
89 | if (candidates.length === 0) {
|
90 | throw new Error("Must contain one or more image candidate strings");
|
91 | }
|
92 |
|
93 |
|
94 | return candidates;
|
95 | }
|
96 |
|
97 |
|
98 |
|
99 | startOffset = position;
|
100 | url = collectCharacters(regexLeadingNotSpaces);
|
101 |
|
102 | descriptors = [];
|
103 |
|
104 |
|
105 |
|
106 | if (url.slice(-1) === ",") {
|
107 | url = url.replace(regexTrailingCommas, "");
|
108 |
|
109 | parseDescriptors();
|
110 | }
|
111 | else {
|
112 | tokenize();
|
113 | }
|
114 |
|
115 | }
|
116 | |
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | function tokenize() {
|
123 |
|
124 | collectCharacters(regexLeadingSpaces);
|
125 |
|
126 | currentDescriptor = "";
|
127 |
|
128 | state = "in descriptor";
|
129 |
|
130 | while (true) {
|
131 |
|
132 | c = input.charAt(position);
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | if (state === "in descriptor") {
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | if (isASCIIWhitespace(c)) {
|
144 | if (currentDescriptor) {
|
145 | descriptors.push(currentDescriptor);
|
146 | currentDescriptor = "";
|
147 | state = "after descriptor";
|
148 | }
|
149 | }
|
150 |
|
151 |
|
152 |
|
153 | else if (c === ",") {
|
154 | position += 1;
|
155 |
|
156 | if (currentDescriptor) {
|
157 | descriptors.push(currentDescriptor);
|
158 | }
|
159 |
|
160 | parseDescriptors();
|
161 | return;
|
162 | }
|
163 |
|
164 | else if (c === "\u0028") {
|
165 | currentDescriptor += c;
|
166 | state = "in parens";
|
167 | }
|
168 |
|
169 |
|
170 | else if (c === "") {
|
171 | if (currentDescriptor) {
|
172 | descriptors.push(currentDescriptor);
|
173 | }
|
174 |
|
175 | parseDescriptors();
|
176 | return;
|
177 |
|
178 | } else {
|
179 | currentDescriptor += c;
|
180 | }
|
181 | }
|
182 | else if (state === "in parens") {
|
183 |
|
184 |
|
185 | if (c === ")") {
|
186 | currentDescriptor += c;
|
187 | state = "in descriptor";
|
188 | }
|
189 |
|
190 |
|
191 | else if (c === "") {
|
192 | descriptors.push(currentDescriptor);
|
193 | parseDescriptors();
|
194 | return;
|
195 | }
|
196 |
|
197 | else {
|
198 | currentDescriptor += c;
|
199 | }
|
200 | }
|
201 | else if (state === "after descriptor") {
|
202 |
|
203 | if (isASCIIWhitespace(c)) {
|
204 | }
|
205 | else if (c === "") {
|
206 | parseDescriptors();
|
207 | return;
|
208 | }
|
209 |
|
210 | else {
|
211 | state = "in descriptor";
|
212 | position -= 1;
|
213 | }
|
214 | }
|
215 |
|
216 |
|
217 | position += 1;
|
218 | }
|
219 | }
|
220 | |
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 | function parseDescriptors() {
|
228 |
|
229 | let pError = false;
|
230 |
|
231 |
|
232 |
|
233 | let w;
|
234 | let d;
|
235 | let h;
|
236 | let i;
|
237 | const candidate = {};
|
238 | let desc;
|
239 | let lastChar;
|
240 | let value;
|
241 | let intVal;
|
242 | let floatVal;
|
243 |
|
244 |
|
245 | for (i = 0; i < descriptors.length; i++) {
|
246 | desc = descriptors[i];
|
247 | lastChar = desc[desc.length - 1];
|
248 | value = desc.substring(0, desc.length - 1);
|
249 | intVal = parseInt(value, 10);
|
250 | floatVal = parseFloat(value);
|
251 |
|
252 |
|
253 | if (regexNonNegativeInteger.test(value) && lastChar === "w") {
|
254 |
|
255 | if (w || d) {
|
256 | pError = true;
|
257 | }
|
258 |
|
259 |
|
260 |
|
261 |
|
262 | if (intVal === 0) {
|
263 | pError = true;
|
264 | } else {
|
265 | w = intVal;
|
266 | }
|
267 | }
|
268 |
|
269 | else if (regexFloatingPoint.test(value) && lastChar === "x") {
|
270 |
|
271 |
|
272 | if (w || d || h) {
|
273 | pError = true;
|
274 | }
|
275 |
|
276 |
|
277 |
|
278 |
|
279 | if (floatVal < 0) {
|
280 | pError = true;
|
281 | } else {
|
282 | d = floatVal;
|
283 | }
|
284 | }
|
285 |
|
286 | else if (regexNonNegativeInteger.test(value) && lastChar === "h") {
|
287 |
|
288 | if (h || d) {
|
289 | pError = true;
|
290 | }
|
291 |
|
292 |
|
293 |
|
294 |
|
295 | if (intVal === 0) {
|
296 | pError = true;
|
297 | } else {
|
298 | h = intVal;
|
299 | }
|
300 |
|
301 | } else {
|
302 | pError = true;
|
303 | }
|
304 | }
|
305 |
|
306 |
|
307 |
|
308 |
|
309 | if (!pError) {
|
310 | candidate.source = {
|
311 | value: url,
|
312 | startOffset
|
313 | };
|
314 |
|
315 | if (w) {
|
316 | candidate.width = {
|
317 | value: w
|
318 | };
|
319 | }
|
320 |
|
321 | if (d) {
|
322 | candidate.density = {
|
323 | value: d
|
324 | };
|
325 | }
|
326 |
|
327 | if (h) {
|
328 | candidate.height = {
|
329 | value: h
|
330 | };
|
331 | }
|
332 |
|
333 | candidates.push(candidate);
|
334 | } else {
|
335 | throw new Error(`Invalid srcset descriptor found in '${input}' at '${desc}'`);
|
336 | }
|
337 | }
|
338 | }
|
339 |
|
340 | function parseSrc(input) {
|
341 | if (!input) {
|
342 | throw new Error("Must be non-empty");
|
343 | }
|
344 |
|
345 | let startOffset = 0;
|
346 | let value = input;
|
347 |
|
348 | while (isASCIIWhitespace(value.substring(0, 1))) {
|
349 | startOffset += 1;
|
350 | value = value.substring(1, value.length);
|
351 | }
|
352 |
|
353 | while (isASCIIWhitespace(value.substring(value.length - 1, value.length))) {
|
354 | value = value.substring(0, value.length - 1);
|
355 | }
|
356 |
|
357 | if (!value) {
|
358 | throw new Error("Must be non-empty");
|
359 | }
|
360 |
|
361 | return {
|
362 | value,
|
363 | startOffset
|
364 | };
|
365 | }
|
366 |
|
367 | const WINDOWS_ABS_PATH_REGEXP = /^[a-zA-Z]:[\\/]|^\\\\/;
|
368 |
|
369 | function isUrlRequestable(url) {
|
370 |
|
371 | if (/^\/\//.test(url)) {
|
372 | return false;
|
373 | }
|
374 |
|
375 |
|
376 | if (/^file:/i.test(url)) {
|
377 | return true;
|
378 | }
|
379 |
|
380 |
|
381 | if (/^[a-z][a-z0-9+.-]*:/i.test(url) && !WINDOWS_ABS_PATH_REGEXP.test(url)) {
|
382 | return false;
|
383 | }
|
384 |
|
385 |
|
386 | if (/^[{}[\]#*;,'§$%&(=?`´^°<>]/.test(url)) {
|
387 | return false;
|
388 | }
|
389 |
|
390 | return true;
|
391 | }
|
392 |
|
393 | const WINDOWS_PATH_SEPARATOR_REGEXP = /\\/g;
|
394 | const RELATIVE_PATH_REGEXP = /^\.\.?[/\\]/;
|
395 |
|
396 | const absoluteToRequest = (context, maybeAbsolutePath) => {
|
397 | if (maybeAbsolutePath[0] === "/") {
|
398 | if (maybeAbsolutePath.length > 1 && maybeAbsolutePath[maybeAbsolutePath.length - 1] === "/") {
|
399 |
|
400 |
|
401 | return maybeAbsolutePath;
|
402 | }
|
403 |
|
404 | const querySplitPos = maybeAbsolutePath.indexOf("?");
|
405 | let resource = querySplitPos === -1 ? maybeAbsolutePath : maybeAbsolutePath.slice(0, querySplitPos);
|
406 | resource = _path.default.posix.relative(context, resource);
|
407 |
|
408 | if (!resource.startsWith("../")) {
|
409 | resource = `./${resource}`;
|
410 | }
|
411 |
|
412 | return querySplitPos === -1 ? resource : resource + maybeAbsolutePath.slice(querySplitPos);
|
413 | }
|
414 |
|
415 | if (WINDOWS_ABS_PATH_REGEXP.test(maybeAbsolutePath)) {
|
416 | const querySplitPos = maybeAbsolutePath.indexOf("?");
|
417 | let resource = querySplitPos === -1 ? maybeAbsolutePath : maybeAbsolutePath.slice(0, querySplitPos);
|
418 | resource = _path.default.win32.relative(context, resource);
|
419 |
|
420 | if (!WINDOWS_ABS_PATH_REGEXP.test(resource)) {
|
421 | resource = resource.replace(WINDOWS_PATH_SEPARATOR_REGEXP, "/");
|
422 |
|
423 | if (!resource.startsWith("../")) {
|
424 | resource = `./${resource}`;
|
425 | }
|
426 | }
|
427 |
|
428 | return querySplitPos === -1 ? resource : resource + maybeAbsolutePath.slice(querySplitPos);
|
429 | }
|
430 |
|
431 | if (!RELATIVE_PATH_REGEXP.test(maybeAbsolutePath)) {
|
432 | return `./${maybeAbsolutePath.replace(WINDOWS_PATH_SEPARATOR_REGEXP, "/")}`;
|
433 | }
|
434 |
|
435 |
|
436 | return maybeAbsolutePath;
|
437 | };
|
438 |
|
439 | const contextify = (context, request) => request.split("!").map(r => absoluteToRequest(context, r)).join("!");
|
440 |
|
441 | const MODULE_REQUEST_REGEXP = /^[^?]*~/;
|
442 |
|
443 | function requestify(context, request) {
|
444 | const isWindowsAbsolutePath = WINDOWS_ABS_PATH_REGEXP.test(request);
|
445 | const newRequest = isWindowsAbsolutePath ? decodeURI(request).replace(/[\t\n\r]/g, "") : decodeURI(request).replace(/[\t\n\r]/g, "").replace(/\\/g, "/");
|
446 |
|
447 | if (isWindowsAbsolutePath || newRequest[0] === "/") {
|
448 | return newRequest;
|
449 | }
|
450 |
|
451 | if (/^file:/i.test(newRequest)) {
|
452 | return newRequest;
|
453 | }
|
454 |
|
455 |
|
456 | if (MODULE_REQUEST_REGEXP.test(newRequest)) {
|
457 | return newRequest.replace(MODULE_REQUEST_REGEXP, "");
|
458 | }
|
459 |
|
460 |
|
461 | return contextify(context, newRequest);
|
462 | }
|
463 |
|
464 | function isProductionMode(loaderContext) {
|
465 | return loaderContext.mode === "production" || !loaderContext.mode;
|
466 | }
|
467 |
|
468 | const defaultMinimizerOptions = {
|
469 | caseSensitive: true,
|
470 |
|
471 | collapseWhitespace: true,
|
472 | conservativeCollapse: true,
|
473 | keepClosingSlash: true,
|
474 |
|
475 | minifyCSS: true,
|
476 | minifyJS: true,
|
477 |
|
478 |
|
479 | removeComments: true,
|
480 |
|
481 |
|
482 | removeScriptTypeAttributes: true,
|
483 | removeStyleLinkTypeAttributes: true
|
484 |
|
485 | };
|
486 | exports.defaultMinimizerOptions = defaultMinimizerOptions;
|
487 |
|
488 | function getMinimizeOption(rawOptions, loaderContext) {
|
489 | if (typeof rawOptions.minimize === "undefined") {
|
490 | return isProductionMode(loaderContext) ? defaultMinimizerOptions : false;
|
491 | }
|
492 |
|
493 | if (typeof rawOptions.minimize === "boolean") {
|
494 | return rawOptions.minimize === true ? defaultMinimizerOptions : false;
|
495 | }
|
496 |
|
497 | return rawOptions.minimize;
|
498 | }
|
499 |
|
500 | function getAttributeValue(attributes, name) {
|
501 | const [result] = attributes.filter(i => i.name.toLowerCase() === name);
|
502 | return typeof result === "undefined" ? result : result.value;
|
503 | }
|
504 |
|
505 | function scriptSrcFilter(tag, attribute, attributes) {
|
506 | let type = getAttributeValue(attributes, "type");
|
507 |
|
508 | if (!type) {
|
509 | return true;
|
510 | }
|
511 |
|
512 | type = type.trim();
|
513 |
|
514 | if (!type) {
|
515 | return false;
|
516 | }
|
517 |
|
518 | if (type !== "module" && type !== "text/javascript" && type !== "application/javascript") {
|
519 | return false;
|
520 | }
|
521 |
|
522 | return true;
|
523 | }
|
524 |
|
525 | function linkHrefFilter(tag, attribute, attributes) {
|
526 | let rel = getAttributeValue(attributes, "rel");
|
527 |
|
528 | if (!rel) {
|
529 | return false;
|
530 | }
|
531 |
|
532 | rel = rel.trim();
|
533 |
|
534 | if (!rel) {
|
535 | return false;
|
536 | }
|
537 |
|
538 | rel = rel.toLowerCase();
|
539 | const usedRels = rel.split(" ").filter(value => value);
|
540 | const allowedRels = ["stylesheet", "icon", "mask-icon", "apple-touch-icon", "apple-touch-icon-precomposed", "apple-touch-startup-image", "manifest", "prefetch", "preload"];
|
541 | return allowedRels.filter(value => usedRels.includes(value)).length > 0;
|
542 | }
|
543 |
|
544 | const META = new Map([["name", new Set([
|
545 | "msapplication-tileimage", "msapplication-square70x70logo", "msapplication-square150x150logo", "msapplication-wide310x150logo", "msapplication-square310x310logo", "msapplication-config", "msapplication-task", "twitter:image"])], ["property", new Set(["og:image", "og:image:url", "og:image:secure_url", "og:audio", "og:audio:secure_url", "og:video", "og:video:secure_url", "vk:image"])], ["itemprop", new Set(["image", "logo", "screenshot", "thumbnailurl", "contenturl", "downloadurl", "duringmedia", "embedurl", "installurl", "layoutimage"])]]);
|
546 |
|
547 | function linkItempropFilter(tag, attribute, attributes) {
|
548 | let name = getAttributeValue(attributes, "itemprop");
|
549 |
|
550 | if (name) {
|
551 | name = name.trim();
|
552 |
|
553 | if (!name) {
|
554 | return false;
|
555 | }
|
556 |
|
557 | name = name.toLowerCase();
|
558 | return META.get("itemprop").has(name);
|
559 | }
|
560 |
|
561 | return false;
|
562 | }
|
563 |
|
564 | function linkUnionFilter(tag, attribute, attributes) {
|
565 | return linkHrefFilter(tag, attribute, attributes) || linkItempropFilter(tag, attribute, attributes);
|
566 | }
|
567 |
|
568 | function metaContentFilter(tag, attribute, attributes) {
|
569 | for (const item of META) {
|
570 | const [key, allowedNames] = item;
|
571 | let name = getAttributeValue(attributes, key);
|
572 |
|
573 | if (name) {
|
574 | name = name.trim();
|
575 |
|
576 | if (!name) {
|
577 |
|
578 | continue;
|
579 | }
|
580 |
|
581 | name = name.toLowerCase();
|
582 | return allowedNames.has(name);
|
583 | }
|
584 | }
|
585 |
|
586 | return false;
|
587 | }
|
588 |
|
589 | function srcType(options) {
|
590 | let source;
|
591 |
|
592 | try {
|
593 | source = parseSrc(options.value);
|
594 | } catch (error) {
|
595 | throw new _HtmlSourceError.default(`Bad value for attribute "${options.attribute}" on element "${options.tag}": ${error.message}`, options.attributeStartOffset, options.attributeEndOffset, options.html);
|
596 | }
|
597 |
|
598 | source = c0ControlCodesExclude(source);
|
599 |
|
600 | if (!isUrlRequestable(source.value)) {
|
601 | return [];
|
602 | }
|
603 |
|
604 | const startOffset = options.valueStartOffset + source.startOffset;
|
605 | const endOffset = startOffset + source.value.length;
|
606 | return [{
|
607 | value: source.value,
|
608 | startOffset,
|
609 | endOffset
|
610 | }];
|
611 | }
|
612 |
|
613 | function srcsetType(options) {
|
614 | let sourceSet;
|
615 |
|
616 | try {
|
617 | sourceSet = parseSrcset(options.value);
|
618 | } catch (error) {
|
619 | throw new _HtmlSourceError.default(`Bad value for attribute "${options.attribute}" on element "${options.tag}": ${error.message}`, options.attributeStartOffset, options.attributeEndOffset, options.html);
|
620 | }
|
621 |
|
622 | const result = [];
|
623 | sourceSet.forEach(sourceItem => {
|
624 | let {
|
625 | source
|
626 | } = sourceItem;
|
627 | source = c0ControlCodesExclude(source);
|
628 |
|
629 | if (!isUrlRequestable(source.value)) {
|
630 | return false;
|
631 | }
|
632 |
|
633 | const startOffset = options.valueStartOffset + source.startOffset;
|
634 | const endOffset = startOffset + source.value.length;
|
635 | result.push({
|
636 | value: source.value,
|
637 | startOffset,
|
638 | endOffset
|
639 | });
|
640 | return false;
|
641 | });
|
642 | return result;
|
643 | }
|
644 |
|
645 | function metaContentType(options) {
|
646 | const isMsapplicationTask = options.attributes.find(i => i.name.toLowerCase() === "name" && i.value.toLowerCase() === "msapplication-task");
|
647 |
|
648 | if (isMsapplicationTask) {
|
649 | let startOffset = options.valueStartOffset;
|
650 | let endOffset = options.valueStartOffset;
|
651 | let value;
|
652 | const parts = options.value.split(";");
|
653 |
|
654 | for (const [index, part] of parts.entries()) {
|
655 | const isLastIteration = index === parts.length - 1;
|
656 |
|
657 | if (/^icon-uri/i.test(part.trim())) {
|
658 | const [name, src] = part.split("=");
|
659 | startOffset += name.length + 1;
|
660 | let source;
|
661 |
|
662 | try {
|
663 | source = parseSrc(src);
|
664 | } catch (error) {
|
665 | throw new _HtmlSourceError.default(`Bad value for attribute "icon-uri" on element "${options.tag}": ${error.message}`, options.attributeStartOffset, options.attributeEndOffset, options.html);
|
666 | }
|
667 |
|
668 | source = c0ControlCodesExclude(source);
|
669 | ({
|
670 | value
|
671 | } = source);
|
672 | startOffset += source.startOffset;
|
673 | endOffset = startOffset + value.length;
|
674 | break;
|
675 | }
|
676 |
|
677 |
|
678 | startOffset += part.length + (isLastIteration ? 0 : 1);
|
679 | }
|
680 |
|
681 | if (!value) {
|
682 | return [];
|
683 | }
|
684 |
|
685 | return [{
|
686 | startOffset,
|
687 | endOffset,
|
688 | value
|
689 | }];
|
690 | }
|
691 |
|
692 | return srcType(options);
|
693 | }
|
694 |
|
695 |
|
696 |
|
697 |
|
698 |
|
699 |
|
700 |
|
701 |
|
702 |
|
703 |
|
704 |
|
705 |
|
706 |
|
707 |
|
708 |
|
709 |
|
710 |
|
711 |
|
712 |
|
713 |
|
714 |
|
715 |
|
716 |
|
717 |
|
718 |
|
719 |
|
720 |
|
721 |
|
722 |
|
723 |
|
724 |
|
725 |
|
726 |
|
727 |
|
728 |
|
729 |
|
730 |
|
731 |
|
732 | const defaultSources = new Map([["audio", new Map([["src", {
|
733 | type: srcType
|
734 | }]])], ["embed", new Map([["src", {
|
735 | type: srcType
|
736 | }]])], ["img", new Map([["src", {
|
737 | type: srcType
|
738 | }], ["srcset", {
|
739 | type: srcsetType
|
740 | }]])], ["input", new Map([["src", {
|
741 | type: srcType
|
742 | }]])], ["link", new Map([["href", {
|
743 | type: srcType,
|
744 | filter: linkUnionFilter
|
745 | }], ["imagesrcset", {
|
746 | type: srcsetType,
|
747 | filter: linkHrefFilter
|
748 | }]])], ["meta", new Map([["content", {
|
749 | type: metaContentType,
|
750 | filter: metaContentFilter
|
751 | }]])], ["object", new Map([["data", {
|
752 | type: srcType
|
753 | }]])], ["script", new Map([["src", {
|
754 | type: srcType,
|
755 | filter: scriptSrcFilter
|
756 | }],
|
757 | ["href", {
|
758 | type: srcType,
|
759 | filter: scriptSrcFilter
|
760 | }], ["xlink:href", {
|
761 | type: srcType,
|
762 | filter: scriptSrcFilter
|
763 | }]])], ["source", new Map([["src", {
|
764 | type: srcType
|
765 | }], ["srcset", {
|
766 | type: srcsetType
|
767 | }]])], ["track", new Map([["src", {
|
768 | type: srcType
|
769 | }]])], ["video", new Map([["poster", {
|
770 | type: srcType
|
771 | }], ["src", {
|
772 | type: srcType
|
773 | }]])],
|
774 | ["image", new Map([["xlink:href", {
|
775 | type: srcType
|
776 | }], ["href", {
|
777 | type: srcType
|
778 | }]])], ["use", new Map([["xlink:href", {
|
779 | type: srcType
|
780 | }], ["href", {
|
781 | type: srcType
|
782 | }]])]
|
783 |
|
784 |
|
785 |
|
786 |
|
787 |
|
788 |
|
789 |
|
790 |
|
791 |
|
792 |
|
793 | ]);
|
794 |
|
795 | function normalizeSourcesList(sources) {
|
796 | if (typeof sources === "undefined") {
|
797 | return defaultSources;
|
798 | }
|
799 |
|
800 | const result = new Map();
|
801 |
|
802 | for (const source of sources) {
|
803 | if (source === "...") {
|
804 | for (const [tag, attributes] of defaultSources.entries()) {
|
805 | let newAttributes;
|
806 | const existingAttributes = result.get(tag);
|
807 |
|
808 | if (existingAttributes) {
|
809 | newAttributes = new Map([...existingAttributes, ...attributes]);
|
810 | } else {
|
811 | newAttributes = new Map(attributes);
|
812 | }
|
813 |
|
814 | result.set(tag, newAttributes);
|
815 | }
|
816 |
|
817 |
|
818 | continue;
|
819 | }
|
820 |
|
821 | let {
|
822 | tag = "*",
|
823 | attribute = "*"
|
824 | } = source;
|
825 | tag = tag.toLowerCase();
|
826 | attribute = attribute.toLowerCase();
|
827 |
|
828 | if (!result.has(tag)) {
|
829 | result.set(tag, new Map());
|
830 | }
|
831 |
|
832 | let typeFn;
|
833 |
|
834 | switch (source.type) {
|
835 | case "src":
|
836 | typeFn = srcType;
|
837 | break;
|
838 |
|
839 | case "srcset":
|
840 | typeFn = srcsetType;
|
841 | break;
|
842 | }
|
843 |
|
844 | result.get(tag).set(attribute, {
|
845 | type: typeFn,
|
846 | filter: source.filter
|
847 | });
|
848 | }
|
849 |
|
850 | return result;
|
851 | }
|
852 |
|
853 | function getSourcesOption(rawOptions) {
|
854 | if (typeof rawOptions.sources === "undefined") {
|
855 | return {
|
856 | list: normalizeSourcesList()
|
857 | };
|
858 | }
|
859 |
|
860 | if (typeof rawOptions.sources === "boolean") {
|
861 | return rawOptions.sources === true ? {
|
862 | list: normalizeSourcesList()
|
863 | } : false;
|
864 | }
|
865 |
|
866 | const sources = normalizeSourcesList(rawOptions.sources.list);
|
867 | return {
|
868 | list: sources,
|
869 | urlFilter: rawOptions.sources.urlFilter
|
870 | };
|
871 | }
|
872 |
|
873 | function normalizeOptions(rawOptions, loaderContext) {
|
874 | return {
|
875 | preprocessor: rawOptions.preprocessor,
|
876 | sources: getSourcesOption(rawOptions),
|
877 | minimize: getMinimizeOption(rawOptions, loaderContext),
|
878 | esModule: typeof rawOptions.esModule === "undefined" ? true : rawOptions.esModule
|
879 | };
|
880 | }
|
881 |
|
882 | function pluginRunner(plugins) {
|
883 | return {
|
884 | async process(content) {
|
885 | const result = {};
|
886 |
|
887 | for (const plugin of plugins) {
|
888 |
|
889 | content = await plugin(content, result);
|
890 | }
|
891 |
|
892 | result.html = content;
|
893 | return result;
|
894 | }
|
895 |
|
896 | };
|
897 | }
|
898 |
|
899 | function getFilter(filter) {
|
900 | return (attribute, value, resourcePath) => {
|
901 | if (typeof filter === "function") {
|
902 | return filter(attribute, value, resourcePath);
|
903 | }
|
904 |
|
905 | return true;
|
906 | };
|
907 | }
|
908 |
|
909 | const GET_SOURCE_FROM_IMPORT_NAME = "___HTML_LOADER_GET_SOURCE_FROM_IMPORT___";
|
910 |
|
911 | function getImportCode(html, loaderContext, imports, options) {
|
912 | if (imports.length === 0) {
|
913 | return "";
|
914 | }
|
915 |
|
916 |
|
917 | const getURLRuntime = require.resolve("./runtime/getUrl.js");
|
918 |
|
919 | const context = loaderContext.context || loaderContext.rootContext;
|
920 | const fileURLToHelper = typeof loaderContext.utils !== "undefined" && typeof loaderContext.utils.contextify === "function" ? loaderContext.utils.contextify(context, getURLRuntime) : contextify(context, getURLRuntime);
|
921 | let code = options.esModule ? `import ${GET_SOURCE_FROM_IMPORT_NAME} from "${fileURLToHelper}";\n` : `var ${GET_SOURCE_FROM_IMPORT_NAME} = require("${fileURLToHelper}");\n`;
|
922 |
|
923 | for (const item of imports) {
|
924 | const {
|
925 | format,
|
926 | importName,
|
927 | request
|
928 | } = item;
|
929 |
|
930 | switch (format) {
|
931 | case "import":
|
932 | code += options.esModule ? `import ${importName} from ${JSON.stringify(request)};\n` : `var ${importName} = require(${JSON.stringify(request)});\n`;
|
933 | break;
|
934 |
|
935 | case "url":
|
936 | default:
|
937 | code += options.esModule ? `var ${importName} = new URL(${JSON.stringify(request)}, import.meta.url);\n` : `var ${importName} = require(${JSON.stringify(request)});\n`;
|
938 | }
|
939 | }
|
940 |
|
941 | return `// Imports\n${code}`;
|
942 | }
|
943 |
|
944 | function getModuleCode(html, replacements) {
|
945 | let code = JSON.stringify(html)
|
946 | .replace(/[\u2028\u2029]/g, str => str === "\u2029" ? "\\u2029" : "\\u2028");
|
947 | let replacersCode = "";
|
948 |
|
949 | for (const item of replacements) {
|
950 | const {
|
951 | runtime,
|
952 | importName,
|
953 | replacementName,
|
954 | isValueQuoted,
|
955 | hash
|
956 | } = item;
|
957 |
|
958 | if (typeof runtime === "undefined" || runtime === true) {
|
959 | const getUrlOptions = [].concat(hash ? [`hash: ${JSON.stringify(hash)}`] : []).concat(isValueQuoted ? [] : "maybeNeedQuotes: true");
|
960 | const preparedOptions = getUrlOptions.length > 0 ? `, { ${getUrlOptions.join(", ")} }` : "";
|
961 | replacersCode += `var ${replacementName} = ${GET_SOURCE_FROM_IMPORT_NAME}(${importName}${preparedOptions});\n`;
|
962 | code = code.replace(new RegExp(replacementName, "g"), () => `" + ${replacementName} + "`);
|
963 | } else {
|
964 | code = code.replace(new RegExp(replacementName, "g"), () => `" + ${importName} + "`);
|
965 | }
|
966 | }
|
967 |
|
968 |
|
969 | code = code.replace(/<(\/?script)/g, (_, s) => `<" + "${s}`);
|
970 | return `// Module\n${replacersCode}var code = ${code};\n`;
|
971 | }
|
972 |
|
973 | function getExportCode(html, options) {
|
974 | if (options.esModule) {
|
975 | return `// Exports\nexport default code;`;
|
976 | }
|
977 |
|
978 | return `// Exports\nmodule.exports = code;`;
|
979 | }
|
980 |
|
981 | function isASCIIC0group(character) {
|
982 |
|
983 |
|
984 | return /^[\u0001-\u0019\u00a0]/.test(character);
|
985 | }
|
986 |
|
987 | function c0ControlCodesExclude(source) {
|
988 | let {
|
989 | value,
|
990 | startOffset
|
991 | } = source;
|
992 |
|
993 | if (!value) {
|
994 | throw new Error("Must be non-empty");
|
995 | }
|
996 |
|
997 | while (isASCIIC0group(value.substring(0, 1))) {
|
998 | startOffset += 1;
|
999 | value = value.substring(1, value.length);
|
1000 | }
|
1001 |
|
1002 | while (isASCIIC0group(value.substring(value.length - 1, value.length))) {
|
1003 | value = value.substring(0, value.length - 1);
|
1004 | }
|
1005 |
|
1006 | if (!value) {
|
1007 | throw new Error("Must be non-empty");
|
1008 | }
|
1009 |
|
1010 | return {
|
1011 | value,
|
1012 | startOffset
|
1013 | };
|
1014 | }
|
1015 |
|
1016 | function traverse(root, callback) {
|
1017 | const visit = (node, parent) => {
|
1018 | let res;
|
1019 |
|
1020 | if (callback) {
|
1021 | res = callback(node, parent);
|
1022 | }
|
1023 |
|
1024 | let {
|
1025 | childNodes
|
1026 | } = node;
|
1027 |
|
1028 | if (node.content && Array.isArray(node.content.childNodes)) {
|
1029 | ({
|
1030 | childNodes
|
1031 | } = node.content);
|
1032 | }
|
1033 |
|
1034 | if (res !== false && Array.isArray(childNodes) && childNodes.length >= 0) {
|
1035 | childNodes.forEach(child => {
|
1036 | visit(child, node);
|
1037 | });
|
1038 | }
|
1039 | };
|
1040 |
|
1041 | visit(root, null);
|
1042 | }
|
1043 |
|
1044 | const webpackIgnoreCommentRegexp = /webpackIgnore:(\s+)?(true|false)/;
|
1045 | exports.webpackIgnoreCommentRegexp = webpackIgnoreCommentRegexp; |
\ | No newline at end of file |