From aaa1ea7419c36bd67e818c973b93f410e8ceb93e Mon Sep 17 00:00:00 2001 From: ubinatus <51177379+ubinatus@users.noreply.github.com> Date: Tue, 17 Mar 2026 15:55:28 -0500 Subject: [PATCH] Fix ESM dist export packaging Mark the built ESM and CJS outputs with package manifests and verify both entrypoints expose named exports. This keeps Node from treating the import target as CommonJS and catches packaging regressions during build. --- package.json | 5 +++-- scripts/verify-dist-exports.cjs | 26 ++++++++++++++++++++++++++ scripts/write-dist-package-jsons.cjs | 16 ++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 scripts/verify-dist-exports.cjs create mode 100644 scripts/write-dist-package-jsons.cjs diff --git a/package.json b/package.json index 7aa963c..718c6ac 100644 --- a/package.json +++ b/package.json @@ -13,8 +13,9 @@ "node": ">=14.17.0" }, "scripts": { - "build": "rimraf dist && tsc -p tsconfig.esm.json & tsc -p tsconfig.cjs.json", - "prepare": "rimraf dist && tsc -p tsconfig.esm.json & tsc -p tsconfig.cjs.json", + "build": "rimraf dist && tsc -p tsconfig.esm.json && tsc -p tsconfig.cjs.json && node scripts/write-dist-package-jsons.cjs && npm run verify:dist", + "prepare": "npm run build", + "verify:dist": "node scripts/verify-dist-exports.cjs", "lint": "eslint **/*.ts --ignore-path .eslintignore", "lint:fix": "eslint **/*.ts --ignore-path .eslintignore --fix" }, diff --git a/scripts/verify-dist-exports.cjs b/scripts/verify-dist-exports.cjs new file mode 100644 index 0000000..2e89318 --- /dev/null +++ b/scripts/verify-dist-exports.cjs @@ -0,0 +1,26 @@ +const assert = require('node:assert/strict'); +const path = require('node:path'); +const { pathToFileURL } = require('node:url'); + +async function main() { + const rootDir = path.resolve(__dirname, '..'); + const cjsEntry = path.join(rootDir, 'dist', 'cjs', 'index.js'); + const esmEntry = pathToFileURL( + path.join(rootDir, 'dist', 'esm', 'index.js') + ).href; + + const cjsSdk = require(cjsEntry); + const esmSdk = await import(esmEntry); + + assert.equal(cjsSdk.OrderStatus.Created, 'CREATED'); + assert.equal(cjsSdk.CaptureStatus.Completed, 'COMPLETED'); + assert.equal(esmSdk.OrderStatus.Created, 'CREATED'); + assert.equal(esmSdk.CaptureStatus.Completed, 'COMPLETED'); + + console.log('Verified ESM and CJS dist exports'); +} + +main().catch((error) => { + console.error(error); + process.exitCode = 1; +}); diff --git a/scripts/write-dist-package-jsons.cjs b/scripts/write-dist-package-jsons.cjs new file mode 100644 index 0000000..828b33c --- /dev/null +++ b/scripts/write-dist-package-jsons.cjs @@ -0,0 +1,16 @@ +const fs = require('fs'); +const path = require('path'); + +const rootDir = path.resolve(__dirname, '..'); +const distPackages = [ + { dir: path.join(rootDir, 'dist', 'esm'), type: 'module' }, + { dir: path.join(rootDir, 'dist', 'cjs'), type: 'commonjs' }, +]; + +for (const { dir, type } of distPackages) { + fs.mkdirSync(dir, { recursive: true }); + fs.writeFileSync( + path.join(dir, 'package.json'), + JSON.stringify({ type }, null, 2) + '\n' + ); +}