source: trip-planner-front/node_modules/webpack/lib/dependencies/HarmonyExportImportedSpecifierDependency.js@ ceaed42

Last change on this file since ceaed42 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 33.1 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const Dependency = require("../Dependency");
9const { UsageState } = require("../ExportsInfo");
10const HarmonyLinkingError = require("../HarmonyLinkingError");
11const InitFragment = require("../InitFragment");
12const RuntimeGlobals = require("../RuntimeGlobals");
13const Template = require("../Template");
14const { countIterable } = require("../util/IterableHelpers");
15const { first, combine } = require("../util/SetHelpers");
16const makeSerializable = require("../util/makeSerializable");
17const propertyAccess = require("../util/propertyAccess");
18const HarmonyExportInitFragment = require("./HarmonyExportInitFragment");
19const HarmonyImportDependency = require("./HarmonyImportDependency");
20const processExportInfo = require("./processExportInfo");
21
22/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
23/** @typedef {import("../ChunkGraph")} ChunkGraph */
24/** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
25/** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
26/** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
27/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
28/** @typedef {import("../ExportsInfo")} ExportsInfo */
29/** @typedef {import("../ExportsInfo").ExportInfo} ExportInfo */
30/** @typedef {import("../Module")} Module */
31/** @typedef {import("../ModuleGraph")} ModuleGraph */
32/** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
33/** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
34/** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
35/** @typedef {import("../WebpackError")} WebpackError */
36/** @typedef {import("../util/Hash")} Hash */
37/** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
38
39/** @typedef {"missing"|"unused"|"empty-star"|"reexport-dynamic-default"|"reexport-named-default"|"reexport-namespace-object"|"reexport-fake-namespace-object"|"reexport-undefined"|"normal-reexport"|"dynamic-reexport"} ExportModeType */
40
41const idsSymbol = Symbol("HarmonyExportImportedSpecifierDependency.ids");
42
43class NormalReexportItem {
44 /**
45 * @param {string} name export name
46 * @param {string[]} ids reexported ids from other module
47 * @param {ExportInfo} exportInfo export info from other module
48 * @param {boolean} checked true, if it should be checked at runtime if this export exists
49 * @param {boolean} hidden true, if it is hidden behind another active export in the same module
50 */
51 constructor(name, ids, exportInfo, checked, hidden) {
52 this.name = name;
53 this.ids = ids;
54 this.exportInfo = exportInfo;
55 this.checked = checked;
56 this.hidden = hidden;
57 }
58}
59
60class ExportMode {
61 /**
62 * @param {ExportModeType} type type of the mode
63 */
64 constructor(type) {
65 /** @type {ExportModeType} */
66 this.type = type;
67
68 // for "normal-reexport":
69 /** @type {NormalReexportItem[] | null} */
70 this.items = null;
71
72 // for "reexport-named-default" | "reexport-fake-namespace-object" | "reexport-namespace-object"
73 /** @type {string|null} */
74 this.name = null;
75 /** @type {ExportInfo | null} */
76 this.partialNamespaceExportInfo = null;
77
78 // for "dynamic-reexport":
79 /** @type {Set<string> | null} */
80 this.ignored = null;
81
82 // for "dynamic-reexport" | "empty-star":
83 /** @type {Set<string> | null} */
84 this.hidden = null;
85
86 // for "missing":
87 /** @type {string | null} */
88 this.userRequest = null;
89
90 // for "reexport-fake-namespace-object":
91 /** @type {number} */
92 this.fakeType = 0;
93 }
94}
95
96const determineExportAssignments = (
97 moduleGraph,
98 dependencies,
99 additionalDependency
100) => {
101 const names = new Set();
102 const dependencyIndices = [];
103
104 if (additionalDependency) {
105 dependencies = dependencies.concat(additionalDependency);
106 }
107
108 for (const dep of dependencies) {
109 const i = dependencyIndices.length;
110 dependencyIndices[i] = names.size;
111 const otherImportedModule = moduleGraph.getModule(dep);
112 if (otherImportedModule) {
113 const exportsInfo = moduleGraph.getExportsInfo(otherImportedModule);
114 for (const exportInfo of exportsInfo.exports) {
115 if (
116 exportInfo.provided === true &&
117 exportInfo.name !== "default" &&
118 !names.has(exportInfo.name)
119 ) {
120 names.add(exportInfo.name);
121 dependencyIndices[i] = names.size;
122 }
123 }
124 }
125 }
126 dependencyIndices.push(names.size);
127
128 return { names: Array.from(names), dependencyIndices };
129};
130
131const findDependencyForName = (
132 { names, dependencyIndices },
133 name,
134 dependencies
135) => {
136 const dependenciesIt = dependencies[Symbol.iterator]();
137 const dependencyIndicesIt = dependencyIndices[Symbol.iterator]();
138 let dependenciesItResult = dependenciesIt.next();
139 let dependencyIndicesItResult = dependencyIndicesIt.next();
140 if (dependencyIndicesItResult.done) return;
141 for (let i = 0; i < names.length; i++) {
142 while (i >= dependencyIndicesItResult.value) {
143 dependenciesItResult = dependenciesIt.next();
144 dependencyIndicesItResult = dependencyIndicesIt.next();
145 if (dependencyIndicesItResult.done) return;
146 }
147 if (names[i] === name) return dependenciesItResult.value;
148 }
149 return undefined;
150};
151
152class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
153 /**
154 * @param {string} request the request string
155 * @param {number} sourceOrder the order in the original source file
156 * @param {string[]} ids the requested export name of the imported module
157 * @param {string | null} name the export name of for this module
158 * @param {Set<string>} activeExports other named exports in the module
159 * @param {ReadonlyArray<HarmonyExportImportedSpecifierDependency> | Iterable<HarmonyExportImportedSpecifierDependency>} otherStarExports other star exports in the module before this import
160 * @param {boolean} strictExportPresence when true, missing exports in the imported module lead to errors instead of warnings
161 * @param {HarmonyStarExportsList} allStarExports all star exports in the module
162 * @param {Record<string, any>=} assertions import assertions
163 */
164 constructor(
165 request,
166 sourceOrder,
167 ids,
168 name,
169 activeExports,
170 otherStarExports,
171 strictExportPresence,
172 allStarExports,
173 assertions
174 ) {
175 super(request, sourceOrder, assertions);
176
177 this.ids = ids;
178 this.name = name;
179 this.activeExports = activeExports;
180 this.otherStarExports = otherStarExports;
181 this.strictExportPresence = strictExportPresence;
182 this.allStarExports = allStarExports;
183 this._getMode = this._getMode.bind(this);
184 }
185
186 // TODO webpack 6 remove
187 get id() {
188 throw new Error("id was renamed to ids and type changed to string[]");
189 }
190
191 // TODO webpack 6 remove
192 getId() {
193 throw new Error("id was renamed to ids and type changed to string[]");
194 }
195
196 // TODO webpack 6 remove
197 setId() {
198 throw new Error("id was renamed to ids and type changed to string[]");
199 }
200
201 get type() {
202 return "harmony export imported specifier";
203 }
204
205 /**
206 * @param {ModuleGraph} moduleGraph the module graph
207 * @returns {string[]} the imported id
208 */
209 getIds(moduleGraph) {
210 return moduleGraph.getMeta(this)[idsSymbol] || this.ids;
211 }
212
213 /**
214 * @param {ModuleGraph} moduleGraph the module graph
215 * @param {string[]} ids the imported ids
216 * @returns {void}
217 */
218 setIds(moduleGraph, ids) {
219 moduleGraph.getMeta(this)[idsSymbol] = ids;
220 }
221
222 /**
223 * @param {ModuleGraph} moduleGraph the module graph
224 * @param {RuntimeSpec} runtime the runtime
225 * @returns {ExportMode} the export mode
226 */
227 getMode(moduleGraph, runtime) {
228 return moduleGraph.cached(this._getMode, runtime);
229 }
230
231 /**
232 * @param {ModuleGraph} moduleGraph the module graph
233 * @param {RuntimeSpec} runtime the runtime
234 * @returns {ExportMode} the export mode
235 */
236 _getMode(moduleGraph, runtime) {
237 const name = this.name;
238 const ids = this.getIds(moduleGraph);
239 const parentModule = moduleGraph.getParentModule(this);
240 const importedModule = moduleGraph.getModule(this);
241 const exportsInfo = moduleGraph.getExportsInfo(parentModule);
242
243 if (!importedModule) {
244 const mode = new ExportMode("missing");
245
246 mode.userRequest = this.userRequest;
247
248 return mode;
249 }
250
251 if (
252 name
253 ? exportsInfo.getUsed(name, runtime) === UsageState.Unused
254 : exportsInfo.isUsed(runtime) === false
255 ) {
256 const mode = new ExportMode("unused");
257
258 mode.name = name || "*";
259
260 return mode;
261 }
262
263 const importedExportsType = importedModule.getExportsType(
264 moduleGraph,
265 parentModule.buildMeta.strictHarmonyModule
266 );
267
268 // Special handling for reexporting the default export
269 // from non-namespace modules
270 if (name && ids.length > 0 && ids[0] === "default") {
271 switch (importedExportsType) {
272 case "dynamic": {
273 const mode = new ExportMode("reexport-dynamic-default");
274
275 mode.name = name;
276
277 return mode;
278 }
279 case "default-only":
280 case "default-with-named": {
281 const exportInfo = exportsInfo.getReadOnlyExportInfo(name);
282 const mode = new ExportMode("reexport-named-default");
283
284 mode.name = name;
285 mode.partialNamespaceExportInfo = exportInfo;
286
287 return mode;
288 }
289 }
290 }
291
292 // reexporting with a fixed name
293 if (name) {
294 let mode;
295 const exportInfo = exportsInfo.getReadOnlyExportInfo(name);
296
297 if (ids.length > 0) {
298 // export { name as name }
299 switch (importedExportsType) {
300 case "default-only":
301 mode = new ExportMode("reexport-undefined");
302 mode.name = name;
303 break;
304 default:
305 mode = new ExportMode("normal-reexport");
306 mode.items = [
307 new NormalReexportItem(name, ids, exportInfo, false, false)
308 ];
309 break;
310 }
311 } else {
312 // export * as name
313 switch (importedExportsType) {
314 case "default-only":
315 mode = new ExportMode("reexport-fake-namespace-object");
316 mode.name = name;
317 mode.partialNamespaceExportInfo = exportInfo;
318 mode.fakeType = 0;
319 break;
320 case "default-with-named":
321 mode = new ExportMode("reexport-fake-namespace-object");
322 mode.name = name;
323 mode.partialNamespaceExportInfo = exportInfo;
324 mode.fakeType = 2;
325 break;
326 case "dynamic":
327 default:
328 mode = new ExportMode("reexport-namespace-object");
329 mode.name = name;
330 mode.partialNamespaceExportInfo = exportInfo;
331 }
332 }
333
334 return mode;
335 }
336
337 // Star reexporting
338
339 const { ignoredExports, exports, checked, hidden } = this.getStarReexports(
340 moduleGraph,
341 runtime,
342 exportsInfo,
343 importedModule
344 );
345 if (!exports) {
346 // We have too few info about the modules
347 // Delegate the logic to the runtime code
348
349 const mode = new ExportMode("dynamic-reexport");
350 mode.ignored = ignoredExports;
351 mode.hidden = hidden;
352
353 return mode;
354 }
355
356 if (exports.size === 0) {
357 const mode = new ExportMode("empty-star");
358 mode.hidden = hidden;
359
360 return mode;
361 }
362
363 const mode = new ExportMode("normal-reexport");
364
365 mode.items = Array.from(
366 exports,
367 exportName =>
368 new NormalReexportItem(
369 exportName,
370 [exportName],
371 exportsInfo.getReadOnlyExportInfo(exportName),
372 checked.has(exportName),
373 false
374 )
375 );
376 if (hidden !== undefined) {
377 for (const exportName of hidden) {
378 mode.items.push(
379 new NormalReexportItem(
380 exportName,
381 [exportName],
382 exportsInfo.getReadOnlyExportInfo(exportName),
383 false,
384 true
385 )
386 );
387 }
388 }
389
390 return mode;
391 }
392
393 /**
394 * @param {ModuleGraph} moduleGraph the module graph
395 * @param {RuntimeSpec} runtime the runtime
396 * @param {ExportsInfo} exportsInfo exports info about the current module (optional)
397 * @param {Module} importedModule the imported module (optional)
398 * @returns {{exports?: Set<string>, checked?: Set<string>, ignoredExports: Set<string>, hidden?: Set<string>}} information
399 */
400 getStarReexports(
401 moduleGraph,
402 runtime,
403 exportsInfo = moduleGraph.getExportsInfo(moduleGraph.getParentModule(this)),
404 importedModule = moduleGraph.getModule(this)
405 ) {
406 const importedExportsInfo = moduleGraph.getExportsInfo(importedModule);
407
408 const noExtraExports =
409 importedExportsInfo.otherExportsInfo.provided === false;
410 const noExtraImports =
411 exportsInfo.otherExportsInfo.getUsed(runtime) === UsageState.Unused;
412
413 const ignoredExports = new Set(["default", ...this.activeExports]);
414
415 let hiddenExports = undefined;
416 const otherStarExports =
417 this._discoverActiveExportsFromOtherStarExports(moduleGraph);
418 if (otherStarExports !== undefined) {
419 hiddenExports = new Set();
420 for (let i = 0; i < otherStarExports.namesSlice; i++) {
421 hiddenExports.add(otherStarExports.names[i]);
422 }
423 for (const e of ignoredExports) hiddenExports.delete(e);
424 }
425
426 if (!noExtraExports && !noExtraImports) {
427 return {
428 ignoredExports,
429 hidden: hiddenExports
430 };
431 }
432
433 /** @type {Set<string>} */
434 const exports = new Set();
435 /** @type {Set<string>} */
436 const checked = new Set();
437 /** @type {Set<string>} */
438 const hidden = hiddenExports !== undefined ? new Set() : undefined;
439
440 if (noExtraImports) {
441 for (const exportInfo of exportsInfo.orderedExports) {
442 const name = exportInfo.name;
443 if (ignoredExports.has(name)) continue;
444 if (exportInfo.getUsed(runtime) === UsageState.Unused) continue;
445 const importedExportInfo =
446 importedExportsInfo.getReadOnlyExportInfo(name);
447 if (importedExportInfo.provided === false) continue;
448 if (hiddenExports !== undefined && hiddenExports.has(name)) {
449 hidden.add(name);
450 continue;
451 }
452 exports.add(name);
453 if (importedExportInfo.provided === true) continue;
454 checked.add(name);
455 }
456 } else if (noExtraExports) {
457 for (const importedExportInfo of importedExportsInfo.orderedExports) {
458 const name = importedExportInfo.name;
459 if (ignoredExports.has(name)) continue;
460 if (importedExportInfo.provided === false) continue;
461 const exportInfo = exportsInfo.getReadOnlyExportInfo(name);
462 if (exportInfo.getUsed(runtime) === UsageState.Unused) continue;
463 if (hiddenExports !== undefined && hiddenExports.has(name)) {
464 hidden.add(name);
465 continue;
466 }
467 exports.add(name);
468 if (importedExportInfo.provided === true) continue;
469 checked.add(name);
470 }
471 }
472
473 return { ignoredExports, exports, checked, hidden };
474 }
475
476 /**
477 * @param {ModuleGraph} moduleGraph module graph
478 * @returns {null | false | function(ModuleGraphConnection, RuntimeSpec): ConnectionState} function to determine if the connection is active
479 */
480 getCondition(moduleGraph) {
481 return (connection, runtime) => {
482 const mode = this.getMode(moduleGraph, runtime);
483 return mode.type !== "unused" && mode.type !== "empty-star";
484 };
485 }
486
487 /**
488 * @param {ModuleGraph} moduleGraph the module graph
489 * @returns {ConnectionState} how this dependency connects the module to referencing modules
490 */
491 getModuleEvaluationSideEffectsState(moduleGraph) {
492 return false;
493 }
494
495 /**
496 * Returns list of exports referenced by this dependency
497 * @param {ModuleGraph} moduleGraph module graph
498 * @param {RuntimeSpec} runtime the runtime for which the module is analysed
499 * @returns {(string[] | ReferencedExport)[]} referenced exports
500 */
501 getReferencedExports(moduleGraph, runtime) {
502 const mode = this.getMode(moduleGraph, runtime);
503
504 switch (mode.type) {
505 case "missing":
506 case "unused":
507 case "empty-star":
508 case "reexport-undefined":
509 return Dependency.NO_EXPORTS_REFERENCED;
510
511 case "reexport-dynamic-default":
512 return Dependency.EXPORTS_OBJECT_REFERENCED;
513
514 case "reexport-named-default": {
515 if (!mode.partialNamespaceExportInfo)
516 return Dependency.EXPORTS_OBJECT_REFERENCED;
517 /** @type {string[][]} */
518 const referencedExports = [];
519 processExportInfo(
520 runtime,
521 referencedExports,
522 [],
523 /** @type {ExportInfo} */ (mode.partialNamespaceExportInfo)
524 );
525 return referencedExports;
526 }
527
528 case "reexport-namespace-object":
529 case "reexport-fake-namespace-object": {
530 if (!mode.partialNamespaceExportInfo)
531 return Dependency.EXPORTS_OBJECT_REFERENCED;
532 /** @type {string[][]} */
533 const referencedExports = [];
534 processExportInfo(
535 runtime,
536 referencedExports,
537 [],
538 /** @type {ExportInfo} */ (mode.partialNamespaceExportInfo),
539 mode.type === "reexport-fake-namespace-object"
540 );
541 return referencedExports;
542 }
543
544 case "dynamic-reexport":
545 return Dependency.EXPORTS_OBJECT_REFERENCED;
546
547 case "normal-reexport": {
548 const referencedExports = [];
549 for (const { ids, exportInfo, hidden } of mode.items) {
550 if (hidden) continue;
551 processExportInfo(runtime, referencedExports, ids, exportInfo, false);
552 }
553 return referencedExports;
554 }
555
556 default:
557 throw new Error(`Unknown mode ${mode.type}`);
558 }
559 }
560
561 /**
562 * @param {ModuleGraph} moduleGraph the module graph
563 * @returns {{ names: string[], namesSlice: number, dependencyIndices: number[], dependencyIndex: number } | undefined} exported names and their origin dependency
564 */
565 _discoverActiveExportsFromOtherStarExports(moduleGraph) {
566 if (!this.otherStarExports) return undefined;
567
568 const i =
569 "length" in this.otherStarExports
570 ? this.otherStarExports.length
571 : countIterable(this.otherStarExports);
572 if (i === 0) return undefined;
573
574 if (this.allStarExports) {
575 const { names, dependencyIndices } = moduleGraph.cached(
576 determineExportAssignments,
577 this.allStarExports.dependencies
578 );
579
580 return {
581 names,
582 namesSlice: dependencyIndices[i - 1],
583 dependencyIndices,
584 dependencyIndex: i
585 };
586 }
587
588 const { names, dependencyIndices } = moduleGraph.cached(
589 determineExportAssignments,
590 this.otherStarExports,
591 this
592 );
593
594 return {
595 names,
596 namesSlice: dependencyIndices[i - 1],
597 dependencyIndices,
598 dependencyIndex: i
599 };
600 }
601
602 /**
603 * Returns the exported names
604 * @param {ModuleGraph} moduleGraph module graph
605 * @returns {ExportsSpec | undefined} export names
606 */
607 getExports(moduleGraph) {
608 const mode = this.getMode(moduleGraph, undefined);
609
610 switch (mode.type) {
611 case "missing":
612 return undefined;
613 case "dynamic-reexport": {
614 const from = moduleGraph.getConnection(this);
615 return {
616 exports: true,
617 from,
618 canMangle: false,
619 excludeExports: mode.hidden
620 ? combine(mode.ignored, mode.hidden)
621 : mode.ignored,
622 hideExports: mode.hidden,
623 dependencies: [from.module]
624 };
625 }
626 case "empty-star":
627 return {
628 exports: [],
629 hideExports: mode.hidden,
630 dependencies: [moduleGraph.getModule(this)]
631 };
632 // falls through
633 case "normal-reexport": {
634 const from = moduleGraph.getConnection(this);
635 return {
636 exports: Array.from(mode.items, item => ({
637 name: item.name,
638 from,
639 export: item.ids,
640 hidden: item.hidden
641 })),
642 priority: 1,
643 dependencies: [from.module]
644 };
645 }
646 case "reexport-dynamic-default": {
647 {
648 const from = moduleGraph.getConnection(this);
649 return {
650 exports: [
651 {
652 name: mode.name,
653 from,
654 export: ["default"]
655 }
656 ],
657 priority: 1,
658 dependencies: [from.module]
659 };
660 }
661 }
662 case "reexport-undefined":
663 return {
664 exports: [mode.name],
665 dependencies: [moduleGraph.getModule(this)]
666 };
667 case "reexport-fake-namespace-object": {
668 const from = moduleGraph.getConnection(this);
669 return {
670 exports: [
671 {
672 name: mode.name,
673 from,
674 export: null,
675 exports: [
676 {
677 name: "default",
678 canMangle: false,
679 from,
680 export: null
681 }
682 ]
683 }
684 ],
685 priority: 1,
686 dependencies: [from.module]
687 };
688 }
689 case "reexport-namespace-object": {
690 const from = moduleGraph.getConnection(this);
691 return {
692 exports: [
693 {
694 name: mode.name,
695 from,
696 export: null
697 }
698 ],
699 priority: 1,
700 dependencies: [from.module]
701 };
702 }
703 case "reexport-named-default": {
704 const from = moduleGraph.getConnection(this);
705 return {
706 exports: [
707 {
708 name: mode.name,
709 from,
710 export: ["default"]
711 }
712 ],
713 priority: 1,
714 dependencies: [from.module]
715 };
716 }
717 default:
718 throw new Error(`Unknown mode ${mode.type}`);
719 }
720 }
721
722 /**
723 * Returns warnings
724 * @param {ModuleGraph} moduleGraph module graph
725 * @returns {WebpackError[]} warnings
726 */
727 getWarnings(moduleGraph) {
728 if (
729 this.strictExportPresence ||
730 moduleGraph.getParentModule(this).buildMeta.strictHarmonyModule
731 ) {
732 return null;
733 }
734
735 return this._getErrors(moduleGraph);
736 }
737
738 /**
739 * Returns errors
740 * @param {ModuleGraph} moduleGraph module graph
741 * @returns {WebpackError[]} errors
742 */
743 getErrors(moduleGraph) {
744 if (
745 this.strictExportPresence ||
746 moduleGraph.getParentModule(this).buildMeta.strictHarmonyModule
747 ) {
748 return this._getErrors(moduleGraph);
749 }
750
751 return null;
752 }
753
754 /**
755 * @param {ModuleGraph} moduleGraph module graph
756 * @returns {WebpackError[] | undefined} errors
757 */
758 _getErrors(moduleGraph) {
759 const ids = this.getIds(moduleGraph);
760 let errors = this.getLinkingErrors(
761 moduleGraph,
762 ids,
763 `(reexported as '${this.name}')`
764 );
765 if (ids.length === 0 && this.name === null) {
766 const potentialConflicts =
767 this._discoverActiveExportsFromOtherStarExports(moduleGraph);
768 if (potentialConflicts && potentialConflicts.namesSlice > 0) {
769 const ownNames = new Set(
770 potentialConflicts.names.slice(
771 potentialConflicts.namesSlice,
772 potentialConflicts.dependencyIndices[
773 potentialConflicts.dependencyIndex
774 ]
775 )
776 );
777 const importedModule = moduleGraph.getModule(this);
778 if (importedModule) {
779 const exportsInfo = moduleGraph.getExportsInfo(importedModule);
780 const conflicts = new Map();
781 for (const exportInfo of exportsInfo.orderedExports) {
782 if (exportInfo.provided !== true) continue;
783 if (exportInfo.name === "default") continue;
784 if (this.activeExports.has(exportInfo.name)) continue;
785 if (ownNames.has(exportInfo.name)) continue;
786 const conflictingDependency = findDependencyForName(
787 potentialConflicts,
788 exportInfo.name,
789 this.allStarExports
790 ? this.allStarExports.dependencies
791 : [...this.otherStarExports, this]
792 );
793 if (!conflictingDependency) continue;
794 const target = exportInfo.getTerminalBinding(moduleGraph);
795 if (!target) continue;
796 const conflictingModule = moduleGraph.getModule(
797 conflictingDependency
798 );
799 if (conflictingModule === importedModule) continue;
800 const conflictingExportInfo = moduleGraph.getExportInfo(
801 conflictingModule,
802 exportInfo.name
803 );
804 const conflictingTarget =
805 conflictingExportInfo.getTerminalBinding(moduleGraph);
806 if (!conflictingTarget) continue;
807 if (target === conflictingTarget) continue;
808 const list = conflicts.get(conflictingDependency.request);
809 if (list === undefined) {
810 conflicts.set(conflictingDependency.request, [exportInfo.name]);
811 } else {
812 list.push(exportInfo.name);
813 }
814 }
815 for (const [request, exports] of conflicts) {
816 if (!errors) errors = [];
817 errors.push(
818 new HarmonyLinkingError(
819 `The requested module '${
820 this.request
821 }' contains conflicting star exports for the ${
822 exports.length > 1 ? "names" : "name"
823 } ${exports
824 .map(e => `'${e}'`)
825 .join(", ")} with the previous requested module '${request}'`
826 )
827 );
828 }
829 }
830 }
831 }
832 return errors;
833 }
834
835 serialize(context) {
836 const { write, setCircularReference } = context;
837
838 setCircularReference(this);
839 write(this.ids);
840 write(this.name);
841 write(this.activeExports);
842 write(this.otherStarExports);
843 write(this.strictExportPresence);
844 write(this.allStarExports);
845
846 super.serialize(context);
847 }
848
849 deserialize(context) {
850 const { read, setCircularReference } = context;
851
852 setCircularReference(this);
853 this.ids = read();
854 this.name = read();
855 this.activeExports = read();
856 this.otherStarExports = read();
857 this.strictExportPresence = read();
858 this.allStarExports = read();
859
860 super.deserialize(context);
861 }
862}
863
864makeSerializable(
865 HarmonyExportImportedSpecifierDependency,
866 "webpack/lib/dependencies/HarmonyExportImportedSpecifierDependency"
867);
868
869module.exports = HarmonyExportImportedSpecifierDependency;
870
871HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedSpecifierDependencyTemplate extends (
872 HarmonyImportDependency.Template
873) {
874 /**
875 * @param {Dependency} dependency the dependency for which the template should be applied
876 * @param {ReplaceSource} source the current replace source which can be modified
877 * @param {DependencyTemplateContext} templateContext the context object
878 * @returns {void}
879 */
880 apply(dependency, source, templateContext) {
881 const { moduleGraph, runtime, concatenationScope } = templateContext;
882
883 const dep = /** @type {HarmonyExportImportedSpecifierDependency} */ (
884 dependency
885 );
886
887 const mode = dep.getMode(moduleGraph, runtime);
888
889 if (concatenationScope) {
890 switch (mode.type) {
891 case "reexport-undefined":
892 concatenationScope.registerRawExport(
893 mode.name,
894 "/* reexport non-default export from non-harmony */ undefined"
895 );
896 }
897 return;
898 }
899
900 if (mode.type !== "unused" && mode.type !== "empty-star") {
901 super.apply(dependency, source, templateContext);
902
903 this._addExportFragments(
904 templateContext.initFragments,
905 dep,
906 mode,
907 templateContext.module,
908 moduleGraph,
909 runtime,
910 templateContext.runtimeTemplate,
911 templateContext.runtimeRequirements
912 );
913 }
914 }
915
916 /**
917 * @param {InitFragment[]} initFragments target array for init fragments
918 * @param {HarmonyExportImportedSpecifierDependency} dep dependency
919 * @param {ExportMode} mode the export mode
920 * @param {Module} module the current module
921 * @param {ModuleGraph} moduleGraph the module graph
922 * @param {RuntimeSpec} runtime the runtime
923 * @param {RuntimeTemplate} runtimeTemplate the runtime template
924 * @param {Set<string>} runtimeRequirements runtime requirements
925 * @returns {void}
926 */
927 _addExportFragments(
928 initFragments,
929 dep,
930 mode,
931 module,
932 moduleGraph,
933 runtime,
934 runtimeTemplate,
935 runtimeRequirements
936 ) {
937 const importedModule = moduleGraph.getModule(dep);
938 const importVar = dep.getImportVar(moduleGraph);
939
940 switch (mode.type) {
941 case "missing":
942 case "empty-star":
943 initFragments.push(
944 new InitFragment(
945 "/* empty/unused harmony star reexport */\n",
946 InitFragment.STAGE_HARMONY_EXPORTS,
947 1
948 )
949 );
950 break;
951
952 case "unused":
953 initFragments.push(
954 new InitFragment(
955 `${Template.toNormalComment(
956 `unused harmony reexport ${mode.name}`
957 )}\n`,
958 InitFragment.STAGE_HARMONY_EXPORTS,
959 1
960 )
961 );
962 break;
963
964 case "reexport-dynamic-default":
965 initFragments.push(
966 this.getReexportFragment(
967 module,
968 "reexport default from dynamic",
969 moduleGraph.getExportsInfo(module).getUsedName(mode.name, runtime),
970 importVar,
971 null,
972 runtimeRequirements
973 )
974 );
975 break;
976
977 case "reexport-fake-namespace-object":
978 initFragments.push(
979 ...this.getReexportFakeNamespaceObjectFragments(
980 module,
981 moduleGraph.getExportsInfo(module).getUsedName(mode.name, runtime),
982 importVar,
983 mode.fakeType,
984 runtimeRequirements
985 )
986 );
987 break;
988
989 case "reexport-undefined":
990 initFragments.push(
991 this.getReexportFragment(
992 module,
993 "reexport non-default export from non-harmony",
994 moduleGraph.getExportsInfo(module).getUsedName(mode.name, runtime),
995 "undefined",
996 "",
997 runtimeRequirements
998 )
999 );
1000 break;
1001
1002 case "reexport-named-default":
1003 initFragments.push(
1004 this.getReexportFragment(
1005 module,
1006 "reexport default export from named module",
1007 moduleGraph.getExportsInfo(module).getUsedName(mode.name, runtime),
1008 importVar,
1009 "",
1010 runtimeRequirements
1011 )
1012 );
1013 break;
1014
1015 case "reexport-namespace-object":
1016 initFragments.push(
1017 this.getReexportFragment(
1018 module,
1019 "reexport module object",
1020 moduleGraph.getExportsInfo(module).getUsedName(mode.name, runtime),
1021 importVar,
1022 "",
1023 runtimeRequirements
1024 )
1025 );
1026 break;
1027
1028 case "normal-reexport":
1029 for (const { name, ids, checked, hidden } of mode.items) {
1030 if (hidden) continue;
1031 if (checked) {
1032 initFragments.push(
1033 new InitFragment(
1034 "/* harmony reexport (checked) */ " +
1035 this.getConditionalReexportStatement(
1036 module,
1037 name,
1038 importVar,
1039 ids,
1040 runtimeRequirements
1041 ),
1042 InitFragment.STAGE_HARMONY_IMPORTS,
1043 dep.sourceOrder
1044 )
1045 );
1046 } else {
1047 initFragments.push(
1048 this.getReexportFragment(
1049 module,
1050 "reexport safe",
1051 moduleGraph.getExportsInfo(module).getUsedName(name, runtime),
1052 importVar,
1053 moduleGraph
1054 .getExportsInfo(importedModule)
1055 .getUsedName(ids, runtime),
1056 runtimeRequirements
1057 )
1058 );
1059 }
1060 }
1061 break;
1062
1063 case "dynamic-reexport": {
1064 const ignored = mode.hidden
1065 ? combine(mode.ignored, mode.hidden)
1066 : mode.ignored;
1067 const modern =
1068 runtimeTemplate.supportsConst() &&
1069 runtimeTemplate.supportsArrowFunction();
1070 let content =
1071 "/* harmony reexport (unknown) */ var __WEBPACK_REEXPORT_OBJECT__ = {};\n" +
1072 `/* harmony reexport (unknown) */ for(${
1073 modern ? "const" : "var"
1074 } __WEBPACK_IMPORT_KEY__ in ${importVar}) `;
1075
1076 // Filter out exports which are defined by other exports
1077 // and filter out default export because it cannot be reexported with *
1078 if (ignored.size > 1) {
1079 content +=
1080 "if(" +
1081 JSON.stringify(Array.from(ignored)) +
1082 ".indexOf(__WEBPACK_IMPORT_KEY__) < 0) ";
1083 } else if (ignored.size === 1) {
1084 content += `if(__WEBPACK_IMPORT_KEY__ !== ${JSON.stringify(
1085 first(ignored)
1086 )}) `;
1087 }
1088
1089 content += `__WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = `;
1090 if (modern) {
1091 content += `() => ${importVar}[__WEBPACK_IMPORT_KEY__]`;
1092 } else {
1093 content += `function(key) { return ${importVar}[key]; }.bind(0, __WEBPACK_IMPORT_KEY__)`;
1094 }
1095
1096 runtimeRequirements.add(RuntimeGlobals.exports);
1097 runtimeRequirements.add(RuntimeGlobals.definePropertyGetters);
1098
1099 const exportsName = module.exportsArgument;
1100 initFragments.push(
1101 new InitFragment(
1102 `${content}\n/* harmony reexport (unknown) */ ${RuntimeGlobals.definePropertyGetters}(${exportsName}, __WEBPACK_REEXPORT_OBJECT__);\n`,
1103 InitFragment.STAGE_HARMONY_IMPORTS,
1104 dep.sourceOrder
1105 )
1106 );
1107 break;
1108 }
1109
1110 default:
1111 throw new Error(`Unknown mode ${mode.type}`);
1112 }
1113 }
1114
1115 getReexportFragment(
1116 module,
1117 comment,
1118 key,
1119 name,
1120 valueKey,
1121 runtimeRequirements
1122 ) {
1123 const returnValue = this.getReturnValue(name, valueKey);
1124
1125 runtimeRequirements.add(RuntimeGlobals.exports);
1126 runtimeRequirements.add(RuntimeGlobals.definePropertyGetters);
1127
1128 const map = new Map();
1129 map.set(key, `/* ${comment} */ ${returnValue}`);
1130
1131 return new HarmonyExportInitFragment(module.exportsArgument, map);
1132 }
1133
1134 getReexportFakeNamespaceObjectFragments(
1135 module,
1136 key,
1137 name,
1138 fakeType,
1139 runtimeRequirements
1140 ) {
1141 runtimeRequirements.add(RuntimeGlobals.exports);
1142 runtimeRequirements.add(RuntimeGlobals.definePropertyGetters);
1143 runtimeRequirements.add(RuntimeGlobals.createFakeNamespaceObject);
1144
1145 const map = new Map();
1146 map.set(
1147 key,
1148 `/* reexport fake namespace object from non-harmony */ ${name}_namespace_cache || (${name}_namespace_cache = ${
1149 RuntimeGlobals.createFakeNamespaceObject
1150 }(${name}${fakeType ? `, ${fakeType}` : ""}))`
1151 );
1152
1153 return [
1154 new InitFragment(
1155 `var ${name}_namespace_cache;\n`,
1156 InitFragment.STAGE_CONSTANTS,
1157 -1,
1158 `${name}_namespace_cache`
1159 ),
1160 new HarmonyExportInitFragment(module.exportsArgument, map)
1161 ];
1162 }
1163
1164 getConditionalReexportStatement(
1165 module,
1166 key,
1167 name,
1168 valueKey,
1169 runtimeRequirements
1170 ) {
1171 if (valueKey === false) {
1172 return "/* unused export */\n";
1173 }
1174
1175 const exportsName = module.exportsArgument;
1176 const returnValue = this.getReturnValue(name, valueKey);
1177
1178 runtimeRequirements.add(RuntimeGlobals.exports);
1179 runtimeRequirements.add(RuntimeGlobals.definePropertyGetters);
1180 runtimeRequirements.add(RuntimeGlobals.hasOwnProperty);
1181
1182 return `if(${RuntimeGlobals.hasOwnProperty}(${name}, ${JSON.stringify(
1183 valueKey[0]
1184 )})) ${
1185 RuntimeGlobals.definePropertyGetters
1186 }(${exportsName}, { ${JSON.stringify(
1187 key
1188 )}: function() { return ${returnValue}; } });\n`;
1189 }
1190
1191 getReturnValue(name, valueKey) {
1192 if (valueKey === null) {
1193 return `${name}_default.a`;
1194 }
1195
1196 if (valueKey === "") {
1197 return name;
1198 }
1199
1200 if (valueKey === false) {
1201 return "/* unused export */ undefined";
1202 }
1203
1204 return `${name}${propertyAccess(valueKey)}`;
1205 }
1206};
1207
1208class HarmonyStarExportsList {
1209 constructor() {
1210 /** @type {HarmonyExportImportedSpecifierDependency[]} */
1211 this.dependencies = [];
1212 }
1213
1214 /**
1215 * @param {HarmonyExportImportedSpecifierDependency} dep dependency
1216 * @returns {void}
1217 */
1218 push(dep) {
1219 this.dependencies.push(dep);
1220 }
1221
1222 slice() {
1223 return this.dependencies.slice();
1224 }
1225
1226 serialize({ write, setCircularReference }) {
1227 setCircularReference(this);
1228 write(this.dependencies);
1229 }
1230
1231 deserialize({ read, setCircularReference }) {
1232 setCircularReference(this);
1233 this.dependencies = read();
1234 }
1235}
1236
1237makeSerializable(
1238 HarmonyStarExportsList,
1239 "webpack/lib/dependencies/HarmonyExportImportedSpecifierDependency",
1240 "HarmonyStarExportsList"
1241);
1242
1243module.exports.HarmonyStarExportsList = HarmonyStarExportsList;
Note: See TracBrowser for help on using the repository browser.