Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove default filter from WellKnownMap #2180

Merged
merged 4 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading