From 9eb7c6ea989d81d4ed5113e57ea15600150d29cd Mon Sep 17 00:00:00 2001 From: Eric Anderson Date: Tue, 30 Jul 2024 10:43:47 -0500 Subject: [PATCH] Restore CJS exports (#541) * Restore CJS exports * make code compile in cjs mode --- .changeset/weak-masks-suffer.md | 5 +++ .monorepolint.config.mjs | 3 +- ...imulate-internal-foundry-sdk-generator.cjs | 6 +++ .../src/e2e.run-foundry-sdk-generator.test.ts | 37 ++++++++++++++++--- packages/foundry-sdk-generator/package.json | 8 ++-- .../src/generate/generateBundles.ts | 9 +++-- packages/foundry-sdk-generator/tsup.config.js | 4 +- 7 files changed, 56 insertions(+), 16 deletions(-) create mode 100644 .changeset/weak-masks-suffer.md create mode 100644 packages/e2e.test.foundry-sdk-generator/bin/simulate-internal-foundry-sdk-generator.cjs diff --git a/.changeset/weak-masks-suffer.md b/.changeset/weak-masks-suffer.md new file mode 100644 index 000000000..ae0370380 --- /dev/null +++ b/.changeset/weak-masks-suffer.md @@ -0,0 +1,5 @@ +--- +"@osdk/foundry-sdk-generator": patch +--- + +Restore cjs exports diff --git a/.monorepolint.config.mjs b/.monorepolint.config.mjs index 80bdf1837..1113ae435 100644 --- a/.monorepolint.config.mjs +++ b/.monorepolint.config.mjs @@ -483,12 +483,13 @@ NOTE: DO NOT EDIT THIS README BY HAND. It is generated by monorepolint. tsVersion: LATEST_TYPESCRIPT_DEP, }), + // internal packages depend on the cjs nature of this package! Do not make esmOnly without + // fixing the internal packages first (and bumping major). ...standardPackageRules({ includePackages: ["@osdk/foundry-sdk-generator"], }, { legacy: false, tsVersion: LATEST_TYPESCRIPT_DEP, - esmOnly: true, customTsconfigExcludes: [ "./src/__e2e_tests__/**/**.test.ts", "./src/generatedNoCheck/**/*", diff --git a/packages/e2e.test.foundry-sdk-generator/bin/simulate-internal-foundry-sdk-generator.cjs b/packages/e2e.test.foundry-sdk-generator/bin/simulate-internal-foundry-sdk-generator.cjs new file mode 100644 index 000000000..66d6ea70b --- /dev/null +++ b/packages/e2e.test.foundry-sdk-generator/bin/simulate-internal-foundry-sdk-generator.cjs @@ -0,0 +1,6 @@ +#!/usr/bin/env node + +// Internally, there is code that invokes the CLI this way (not via normal npm execution) +// so we have this file to ensure we don't break them. +const cli = require("@osdk/foundry-sdk-generator"); +cli.cli(); diff --git a/packages/e2e.test.foundry-sdk-generator/src/e2e.run-foundry-sdk-generator.test.ts b/packages/e2e.test.foundry-sdk-generator/src/e2e.run-foundry-sdk-generator.test.ts index b15fe0a80..d1004eabb 100644 --- a/packages/e2e.test.foundry-sdk-generator/src/e2e.run-foundry-sdk-generator.test.ts +++ b/packages/e2e.test.foundry-sdk-generator/src/e2e.run-foundry-sdk-generator.test.ts @@ -14,16 +14,19 @@ * limitations under the License. */ -import { execa } from "execa"; +import { execa, execaNode } from "execa"; import { describe, expect, it } from "vitest"; describe("foundry-sdk-generator", () => { - it("should do a basic execution", async () => { - expect(true).toBe(true); + it( + "should do a basic execution via pnpm exec", + { timeout: 10_000 }, + async () => { + expect(true).toBe(true); - const { stdout } = await execa`pnpm exec foundry-sdk-generator --help`; + const { stdout } = await execa`pnpm exec foundry-sdk-generator --help`; - expect(stdout).toMatchInlineSnapshot(` + expect(stdout).toMatchInlineSnapshot(` "foundry-sdk-generator.cjs Commands: @@ -34,6 +37,28 @@ describe("foundry-sdk-generator", () => { --help Show help [boolean] --version Show version number [boolean]" `); + }, + ); + + it("should do a basic execution via simulated internal mechanism", { + timeout: 10_000, + }, async () => { + expect(true).toBe(true); + + const { stdout } = + await execaNode`${__dirname}/../bin/simulate-internal-foundry-sdk-generator.cjs --help`; + + expect(stdout).toMatchInlineSnapshot(` + "simulate-internal-foundry-sdk-generator.cjs + + Commands: + simulate-internal-foundry-sdk-generator. Generates a new npm package which + cjs generatePackage can be published + + Options: + --help Show help [boolean] + --version Show version number [boolean]" + `); }); -}, { timeout: 10_000 }); +}); // diff --git a/packages/foundry-sdk-generator/package.json b/packages/foundry-sdk-generator/package.json index 3b4cf6243..93cd9401c 100644 --- a/packages/foundry-sdk-generator/package.json +++ b/packages/foundry-sdk-generator/package.json @@ -10,16 +10,18 @@ }, "exports": { ".": { + "require": "./build/cjs/index.cjs", "browser": "./build/browser/index.js", "import": "./build/esm/index.js" }, "./*": { + "require": "./build/cjs/public/*.cjs", "browser": "./build/browser/public/*.js", "import": "./build/esm/public/*.js" } }, "scripts": { - "check-attw": "monorepo.tool.attw esm", + "check-attw": "monorepo.tool.attw both", "check-spelling": "cspell --quiet .", "clean": "rm -rf lib dist types build tsconfig.tsbuildinfo", "fix-lint": "eslint . --fix && dprint fmt --config $(find-up dprint.json)", @@ -28,7 +30,7 @@ "test:watch": "vitest", "transpile": "monorepo.tool.transpile", "transpileWatch": "tsup --watch", - "typecheck": "monorepo.tool.typecheck esm" + "typecheck": "monorepo.tool.typecheck both" }, "dependencies": { "@osdk/api": "workspace:~", @@ -74,6 +76,6 @@ ], "main": "./build/cjs/index.cjs", "module": "./build/esm/index.js", - "types": "./build/esm/index.d.ts", + "types": "./build/cjs/index.d.cts", "type": "module" } diff --git a/packages/foundry-sdk-generator/src/generate/generateBundles.ts b/packages/foundry-sdk-generator/src/generate/generateBundles.ts index 2aad8c07a..738b1caee 100644 --- a/packages/foundry-sdk-generator/src/generate/generateBundles.ts +++ b/packages/foundry-sdk-generator/src/generate/generateBundles.ts @@ -39,9 +39,12 @@ async function createRollupBuild( browser: true, modulePaths: [nodeModulesPath!], }), - (commonjs.default ?? (commonjs as any as typeof commonjs["default"]))({}), - (nodePolyfill.default - ?? (nodePolyfill as any as typeof nodePolyfill["default"]))(), + ("default" in commonjs + ? commonjs.default + : (commonjs as any))({}), + ("default" in nodePolyfill + ? nodePolyfill.default + : (nodePolyfill as any))(), ], onwarn: (warning, warn) => { // Ignore circular dependency warnings diff --git a/packages/foundry-sdk-generator/tsup.config.js b/packages/foundry-sdk-generator/tsup.config.js index cfc2b707b..7a77e08bc 100644 --- a/packages/foundry-sdk-generator/tsup.config.js +++ b/packages/foundry-sdk-generator/tsup.config.js @@ -17,7 +17,5 @@ import { defineConfig } from "tsup"; export default defineConfig(async (options) => - (await import("@osdk/monorepo.tsup")).default(options, { - esmOnly: true, - }) + (await import("@osdk/monorepo.tsup")).default(options, {}) );