From d1b1878543e3ce8e70b9d72051242b343150b340 Mon Sep 17 00:00:00 2001 From: Scott Trinh Date: Thu, 25 Apr 2024 13:07:51 -0600 Subject: [PATCH] Export default module interface types directly (#979) We were using an obscure CommonJS syntax for what was essentially a type alias for the default modules and then exporting them in a large object-like export. Instead, just `export type` directly in the aliasing and explicitly reexport nested namespaces. --- .../src/edgeql-js/generateInterfaces.ts | 36 +++++++++---------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/packages/generate/src/edgeql-js/generateInterfaces.ts b/packages/generate/src/edgeql-js/generateInterfaces.ts index c870390e0..6c7cd2207 100644 --- a/packages/generate/src/edgeql-js/generateInterfaces.ts +++ b/packages/generate/src/edgeql-js/generateInterfaces.ts @@ -197,28 +197,26 @@ export const generateInterfaces = (params: GenerateInterfacesParams) => { plainTypesCode.addExport(module.internalName, { modes: ["js"] }); if (module.isRoot && module.name === "default") { - const typeRefs = [ - ...module.types.values(), - ...[...module.nestedModules.values()].map( - (nestedMod) => nestedMod.fullInternalName - ), - ]; - for (const typeRef of typeRefs) { + const sliceTo = module.internalName.length + 1; + for (const typeRef of module.types.values()) { + const aliased = typeRef.slice(sliceTo); + plainTypesCode.writeln([`export type ${aliased} = ${typeRef};\n`]); + } + + for (const nestedMod of module.nestedModules.values()) { plainTypesCode.writeln([ - `import ${typeRef.slice( - module.internalName.length + 1 - )} = ${typeRef};`, + `export namespace ${nestedMod.internalName} {`, ]); + plainTypesCode.increaseIndent(); + for (const typeRef of nestedMod.types.values()) { + const aliased = typeRef.slice( + sliceTo + nestedMod.internalName.length + 1 + ); + plainTypesCode.writeln([`export type ${aliased} = ${typeRef};`]); + } + plainTypesCode.decreaseIndent(); + plainTypesCode.writeln([`}`]); } - plainTypesCode.writeln([t`export type {`]); - plainTypesCode.increaseIndent(); - plainTypesCode.writeln([ - typeRefs - .map((typeRef) => typeRef.slice(module.internalName.length + 1)) - .join(",\n"), - ]); - plainTypesCode.decreaseIndent(); - plainTypesCode.writeln([t`};`]); } };