From 69eea145bf5bd4f08a0976431ddf006a76fd8648 Mon Sep 17 00:00:00 2001 From: Ken Collins Date: Tue, 27 Aug 2024 12:42:51 -0400 Subject: [PATCH] Dual Package --- .github/workflows/publish.yml | 2 -- bin/build | 1 + build.js | 27 +++++++++++++++++---------- package.json | 7 +------ 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 84bd10f..f7b0759 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -18,8 +18,6 @@ jobs: run: npm install - name: Build run: ./bin/build - - name: Remove Type - run: npm pkg delete type - name: Publish uses: JS-DevTools/npm-publish@v3 with: diff --git a/bin/build b/bin/build index 6427456..d5d6b86 100755 --- a/bin/build +++ b/bin/build @@ -2,3 +2,4 @@ set -e node build.js +npm pkg delete type diff --git a/build.js b/build.js index ebbcddb..e4088e9 100644 --- a/build.js +++ b/build.js @@ -2,6 +2,7 @@ import * as esbuild from "esbuild"; import * as glob from "glob"; import { promises as fs } from "fs"; import { createRequire } from "module"; +import path from "path"; const require = createRequire(import.meta.url); const pkg = require("./package.json"); @@ -36,24 +37,30 @@ async function build() { target: "node18", bundle: true, outdir: "dist", - external, // This line excludes all packages listed in dependencies and devDependencies + external, }; - // Build ESM version - await esbuild.build({ - ...commonConfig, - format: "esm", - outExtension: { ".js": ".js" }, - }); - - // Build CommonJS version + // Build CommonJS version only await esbuild.build({ ...commonConfig, format: "cjs", - outExtension: { ".js": ".cjs" }, + outExtension: { ".js": ".js" }, // Output .js files for CommonJS }); console.log("Build complete"); + + // Modify the generated files to use module.exports + const distFiles = glob.sync("dist/**/*.js"); + for (const file of distFiles) { + let content = await fs.readFile(file, "utf8"); + content = content.replace( + /export {([^}]+)};/g, + (_, exports) => `module.exports = { ${exports.trim()} };` + ); + await fs.writeFile(file, content); + } + + console.log("CommonJS conversion complete"); } build().catch(console.error); diff --git a/package.json b/package.json index a93e845..40d55b9 100644 --- a/package.json +++ b/package.json @@ -3,12 +3,6 @@ "version": "1.5.5", "description": "An opinionated panel of experts implementation using OpenAI's Assistants API", "type": "module", - "main": "./dist/index.cjs", - "module": "./dist/index.js", - "exports": { - "import": "./dist/index.js", - "require": "./dist/index.cjs" - }, "scripts": { "setup": "./bin/setup", "test": "./bin/test", @@ -21,6 +15,7 @@ "src", "test", "CHANGELOG.md", + "jest.config.mjs", "LICENSE.md", "package.json", "README.md"