Skip to content

Commit

Permalink
Remove default filter from WellKnownMap (#2180)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie authored Sep 23, 2024
2 parents 7d9cdef + 4cc6d36 commit fbb1b31
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
7 changes: 7 additions & 0 deletions .changeset/quiet-items-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"graphile-export": patch
---

Fix bug in graphile-export handing modules where default export
(`import mod from 'mod'`) differed from wildcard export
(`import * as mod from 'mod'`).
29 changes: 15 additions & 14 deletions utils/graphile-export/src/wellKnown.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import crypto from "crypto";
// eslint-disable-next-line import/no-duplicates
import * as _crypto from "crypto";
import * as cryptoStar from "crypto";
import * as grafastStar from "grafast";
import * as graphqlStar from "grafast/graphql";
import util, * as utilStar from "util";
import * as utilStar from "util";

import type { ExportOptions } from "./interfaces.js";

Expand All @@ -13,33 +11,39 @@ interface $$Export {
}

function makeWellKnownFromOptions(options: ExportOptions) {
const namespaces = Object.create(null);
const wellKnownMap = new Map<unknown, $$Export>();

function exportAll(
obj: Record<string, any>,
moduleStar: Record<string, any>,
moduleName: string,
preferViaDefault = false,
) {
for (const exportName of Object.keys(obj)) {
if (exportName !== "default" && !wellKnownMap.has(obj[exportName])) {
namespaces[moduleName] = moduleStar;
for (const exportName of Object.keys(moduleStar)) {
if (!wellKnownMap.has(moduleStar[exportName])) {
/**
* ESM is still a bit flaky, so though `import { foo } from 'bar';` may
* work in some contexts, in raw Node it's often required to do
* `import bar from 'bar'; const foo = bar.foo;`. This code determines
* if this latter approach is desired.
*/
const viaDefault =
preferViaDefault && obj[exportName] === obj["default"]?.[exportName];
wellKnownMap.set(obj[exportName], {
preferViaDefault &&
exportName !== "default" &&
moduleStar[exportName] === moduleStar["default"]?.[exportName];
wellKnownMap.set(moduleStar[exportName], {
moduleName,
exportName: viaDefault ? ["default", exportName] : exportName,
});
}
}
if (!wellKnownMap.has(moduleStar)) {
wellKnownMap.set(moduleStar, { moduleName, exportName: "*" });
}
}

wellKnownMap.set(crypto, { moduleName: "crypto", exportName: "default" });
wellKnownMap.set(util, { moduleName: "util", exportName: "default" });
exportAll(cryptoStar, "crypto");
exportAll(grafastStar, "grafast");
exportAll(graphqlStar, "graphql");
exportAll(utilStar, "util");
Expand All @@ -60,13 +64,10 @@ function makeWellKnownFromOptions(options: ExportOptions) {
}
}

const namespaces = Object.assign(Object.create(null), { crypto: _crypto });

// Now process options
if (options.modules) {
for (const [moduleName, moduleStar] of Object.entries(options.modules)) {
exportAll(moduleStar, moduleName, true);
namespaces[moduleName] = moduleStar;
}
}

Expand Down

0 comments on commit fbb1b31

Please sign in to comment.