From 08a684c7fefdc51fb48baabd6f33d41dc07636ea Mon Sep 17 00:00:00 2001 From: Nick Sweet Date: Fri, 20 Sep 2024 13:15:02 -0500 Subject: [PATCH 1/4] Remove default filter from WellKnownMap --- utils/graphile-export/src/wellKnown.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/graphile-export/src/wellKnown.ts b/utils/graphile-export/src/wellKnown.ts index c854680f65..dd57c9b80e 100644 --- a/utils/graphile-export/src/wellKnown.ts +++ b/utils/graphile-export/src/wellKnown.ts @@ -21,7 +21,7 @@ function makeWellKnownFromOptions(options: ExportOptions) { preferViaDefault = false, ) { for (const exportName of Object.keys(obj)) { - if (exportName !== "default" && !wellKnownMap.has(obj[exportName])) { + if (!wellKnownMap.has(obj[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 From 094956322731be845bd907091b82843acd9787de Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Mon, 23 Sep 2024 11:27:21 +0100 Subject: [PATCH 2/4] Overhaul graphile-export's wellKnown --- utils/graphile-export/src/wellKnown.ts | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/utils/graphile-export/src/wellKnown.ts b/utils/graphile-export/src/wellKnown.ts index dd57c9b80e..3c4527d5ad 100644 --- a/utils/graphile-export/src/wellKnown.ts +++ b/utils/graphile-export/src/wellKnown.ts @@ -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"; @@ -16,12 +14,12 @@ function makeWellKnownFromOptions(options: ExportOptions) { const wellKnownMap = new Map(); function exportAll( - obj: Record, + moduleStar: Record, moduleName: string, preferViaDefault = false, ) { - for (const exportName of Object.keys(obj)) { - if (!wellKnownMap.has(obj[exportName])) { + 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 @@ -29,17 +27,21 @@ function makeWellKnownFromOptions(options: ExportOptions) { * 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"); From 4a4daf5ec4c7002a561f4b6516257fc63e491cce Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Mon, 23 Sep 2024 11:39:20 +0100 Subject: [PATCH 3/4] docs(changeset): Fix bug in graphile-export handing modules where default export (`import mod from 'mod'`) differed from wildcard export (`import * as mod from 'mod'`). --- .changeset/quiet-items-divide.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .changeset/quiet-items-divide.md diff --git a/.changeset/quiet-items-divide.md b/.changeset/quiet-items-divide.md new file mode 100644 index 0000000000..393f180c49 --- /dev/null +++ b/.changeset/quiet-items-divide.md @@ -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'`). From 4cc6d369a9086dd5e2045c4ea45361555b8ea03c Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Mon, 23 Sep 2024 11:47:37 +0100 Subject: [PATCH 4/4] Overhaul wellKnown namespaces --- utils/graphile-export/src/wellKnown.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/utils/graphile-export/src/wellKnown.ts b/utils/graphile-export/src/wellKnown.ts index 3c4527d5ad..02f711bd60 100644 --- a/utils/graphile-export/src/wellKnown.ts +++ b/utils/graphile-export/src/wellKnown.ts @@ -11,6 +11,7 @@ interface $$Export { } function makeWellKnownFromOptions(options: ExportOptions) { + const namespaces = Object.create(null); const wellKnownMap = new Map(); function exportAll( @@ -18,6 +19,7 @@ function makeWellKnownFromOptions(options: ExportOptions) { moduleName: string, preferViaDefault = false, ) { + namespaces[moduleName] = moduleStar; for (const exportName of Object.keys(moduleStar)) { if (!wellKnownMap.has(moduleStar[exportName])) { /** @@ -62,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; } }