| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const { UsageState } = require("../ExportsInfo");
|
|---|
| 9 |
|
|---|
| 10 | /** @typedef {import("../Dependency").RawReferencedExports} RawReferencedExports */
|
|---|
| 11 | /** @typedef {import("../ExportsInfo").ExportInfo} ExportInfo */
|
|---|
| 12 | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
|---|
| 13 |
|
|---|
| 14 | /**
|
|---|
| 15 | * Process export info.
|
|---|
| 16 | * @param {RuntimeSpec} runtime the runtime
|
|---|
| 17 | * @param {RawReferencedExports} referencedExports list of referenced exports, will be added to
|
|---|
| 18 | * @param {string[]} prefix export prefix
|
|---|
| 19 | * @param {ExportInfo=} exportInfo the export info
|
|---|
| 20 | * @param {boolean} defaultPointsToSelf when true, using default will reference itself
|
|---|
| 21 | * @param {Set<ExportInfo>} alreadyVisited already visited export info (to handle circular reexports)
|
|---|
| 22 | */
|
|---|
| 23 | const processExportInfo = (
|
|---|
| 24 | runtime,
|
|---|
| 25 | referencedExports,
|
|---|
| 26 | prefix,
|
|---|
| 27 | exportInfo,
|
|---|
| 28 | defaultPointsToSelf = false,
|
|---|
| 29 | alreadyVisited = new Set()
|
|---|
| 30 | ) => {
|
|---|
| 31 | if (!exportInfo) {
|
|---|
| 32 | referencedExports.push(prefix);
|
|---|
| 33 | return;
|
|---|
| 34 | }
|
|---|
| 35 | const used = exportInfo.getUsed(runtime);
|
|---|
| 36 | if (used === UsageState.Unused) return;
|
|---|
| 37 | if (alreadyVisited.has(exportInfo)) {
|
|---|
| 38 | referencedExports.push(prefix);
|
|---|
| 39 | return;
|
|---|
| 40 | }
|
|---|
| 41 | alreadyVisited.add(exportInfo);
|
|---|
| 42 | if (
|
|---|
| 43 | used !== UsageState.OnlyPropertiesUsed ||
|
|---|
| 44 | !exportInfo.exportsInfo ||
|
|---|
| 45 | exportInfo.exportsInfo.otherExportsInfo.getUsed(runtime) !==
|
|---|
| 46 | UsageState.Unused
|
|---|
| 47 | ) {
|
|---|
| 48 | alreadyVisited.delete(exportInfo);
|
|---|
| 49 | referencedExports.push(prefix);
|
|---|
| 50 | return;
|
|---|
| 51 | }
|
|---|
| 52 | const exportsInfo = exportInfo.exportsInfo;
|
|---|
| 53 | for (const exportInfo of exportsInfo.orderedExports) {
|
|---|
| 54 | processExportInfo(
|
|---|
| 55 | runtime,
|
|---|
| 56 | referencedExports,
|
|---|
| 57 | defaultPointsToSelf && exportInfo.name === "default"
|
|---|
| 58 | ? prefix
|
|---|
| 59 | : [...prefix, exportInfo.name],
|
|---|
| 60 | exportInfo,
|
|---|
| 61 | false,
|
|---|
| 62 | alreadyVisited
|
|---|
| 63 | );
|
|---|
| 64 | }
|
|---|
| 65 | alreadyVisited.delete(exportInfo);
|
|---|
| 66 | };
|
|---|
| 67 |
|
|---|
| 68 | module.exports = processExportInfo;
|
|---|