|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const { |
| 4 | + getKnownExportNames |
| 5 | +} = require("../lib/runtime/MakeDeferredNamespaceObjectRuntime"); |
| 6 | + |
| 7 | +/** |
| 8 | + * @typedef {object} FakeExportInfo |
| 9 | + * @property {string} name export name |
| 10 | + * @property {boolean | null | undefined} provided whether the module provides it |
| 11 | + * @property {(string | false)=} usedName the name it is emitted under |
| 12 | + */ |
| 13 | + |
| 14 | +/** |
| 15 | + * A module graph whose only deferred module reports the given exports. |
| 16 | + * @param {boolean | null | undefined} otherProvided `otherExportsInfo.provided` |
| 17 | + * @param {FakeExportInfo[]} exports the module's exports |
| 18 | + * @returns {import("../lib/ModuleGraph")} the stub module graph |
| 19 | + */ |
| 20 | +const moduleGraphWith = (otherProvided, exports) => |
| 21 | + /** @type {import("../lib/ModuleGraph")} */ ( |
| 22 | + /** @type {unknown} */ ({ |
| 23 | + getExportsInfo: () => ({ |
| 24 | + otherExportsInfo: { provided: otherProvided }, |
| 25 | + orderedExports: exports.map((exportInfo) => ({ |
| 26 | + name: exportInfo.name, |
| 27 | + provided: exportInfo.provided, |
| 28 | + getUsedName: () => |
| 29 | + exportInfo.usedName === undefined |
| 30 | + ? exportInfo.name |
| 31 | + : exportInfo.usedName |
| 32 | + })) |
| 33 | + }) |
| 34 | + }) |
| 35 | + ); |
| 36 | + |
| 37 | +const module_ = /** @type {import("../lib/Module")} */ ( |
| 38 | + /** @type {unknown} */ ({}) |
| 39 | +); |
| 40 | + |
| 41 | +describe("getKnownExportNames", () => { |
| 42 | + it("sorts the provided export names", () => { |
| 43 | + const moduleGraph = moduleGraphWith(false, [ |
| 44 | + { name: "beta", provided: true }, |
| 45 | + { name: "alpha", provided: true } |
| 46 | + ]); |
| 47 | + expect(getKnownExportNames(moduleGraph, module_, undefined)).toEqual([ |
| 48 | + "alpha", |
| 49 | + "beta" |
| 50 | + ]); |
| 51 | + }); |
| 52 | + |
| 53 | + it("bails out when the module may have exports the graph does not know", () => { |
| 54 | + for (const otherProvided of [true, null, undefined]) { |
| 55 | + const moduleGraph = moduleGraphWith(otherProvided, [ |
| 56 | + { name: "alpha", provided: true } |
| 57 | + ]); |
| 58 | + expect(getKnownExportNames(moduleGraph, module_, undefined)).toBeNull(); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + it("skips an export the module definitively does not provide", () => { |
| 63 | + const moduleGraph = moduleGraphWith(false, [ |
| 64 | + { name: "alpha", provided: true }, |
| 65 | + { name: "__esModule", provided: false } |
| 66 | + ]); |
| 67 | + expect(getKnownExportNames(moduleGraph, module_, undefined)).toEqual([ |
| 68 | + "alpha" |
| 69 | + ]); |
| 70 | + }); |
| 71 | + |
| 72 | + it("keeps `__esModule` when the module really exports it", () => { |
| 73 | + const moduleGraph = moduleGraphWith(false, [ |
| 74 | + { name: "alpha", provided: true }, |
| 75 | + { name: "__esModule", provided: true } |
| 76 | + ]); |
| 77 | + expect(getKnownExportNames(moduleGraph, module_, undefined)).toEqual([ |
| 78 | + "__esModule", |
| 79 | + "alpha" |
| 80 | + ]); |
| 81 | + }); |
| 82 | + |
| 83 | + it("bails out on an export whose presence is unknown", () => { |
| 84 | + for (const provided of [null, undefined]) { |
| 85 | + const moduleGraph = moduleGraphWith(false, [{ name: "alpha", provided }]); |
| 86 | + expect(getKnownExportNames(moduleGraph, module_, undefined)).toBeNull(); |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + it("never exposes `then`", () => { |
| 91 | + const moduleGraph = moduleGraphWith(false, [ |
| 92 | + { name: "alpha", provided: true }, |
| 93 | + { name: "then", provided: true } |
| 94 | + ]); |
| 95 | + expect(getKnownExportNames(moduleGraph, module_, undefined)).toEqual([ |
| 96 | + "alpha" |
| 97 | + ]); |
| 98 | + }); |
| 99 | + |
| 100 | + it("bails out when a name is emitted mangled", () => { |
| 101 | + const moduleGraph = moduleGraphWith(false, [ |
| 102 | + { name: "alpha", provided: true, usedName: "a" } |
| 103 | + ]); |
| 104 | + expect(getKnownExportNames(moduleGraph, module_, undefined)).toBeNull(); |
| 105 | + }); |
| 106 | + |
| 107 | + it("keeps a name the graph reports as unused", () => { |
| 108 | + const moduleGraph = moduleGraphWith(false, [ |
| 109 | + { name: "alpha", provided: true, usedName: false } |
| 110 | + ]); |
| 111 | + expect(getKnownExportNames(moduleGraph, module_, undefined)).toEqual([ |
| 112 | + "alpha" |
| 113 | + ]); |
| 114 | + }); |
| 115 | +}); |
0 commit comments