From 7071c8ea7325dbde075334c83461ea6bc2f67b84 Mon Sep 17 00:00:00 2001 From: Seweryn Kras Date: Tue, 12 Mar 2024 14:13:17 +0100 Subject: [PATCH] feat: enable dual exports (commonjs and esm) of the package --- .github/workflows/ci.yml | 1 + .github/workflows/release.yml | 1 + package.json | 11 ++++++++++- tests/import/import.test.mjs | 6 ++++++ tests/import/jest.config.js | 9 +++++++++ tests/import/require.test.cjs | 6 ++++++ 6 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 tests/import/import.test.mjs create mode 100644 tests/import/jest.config.js create mode 100644 tests/import/require.test.cjs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 03e26c0..f33e387 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -44,3 +44,4 @@ jobs: npm run build npm install --prefix examples npm run --prefix examples lint:ts + npm run test:import diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 3086f69..540e762 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -71,6 +71,7 @@ jobs: npm run build npm install --prefix examples npm run --prefix examples lint:ts + npm run test:import - name: Upload unit test reports uses: actions/upload-artifact@v4 if: always() diff --git a/package.json b/package.json index 1c5c95e..3baafb8 100644 --- a/package.json +++ b/package.json @@ -23,9 +23,17 @@ "workspaces": [ "examples" ], + "type": "commonjs", "main": "dist/task-executor.cjs.js", + "exports": { + ".": { + "types": "./dist/index.d.ts", + "import": "./dist/task-executor.mjs", + "require": "./dist/task-executor.cjs.js" + } + }, "browser": "dist/task-executor.min.js", - "module": "dist/task-executor.es.js", + "module": "dist/task-executor.mjs", "types": "dist/index.d.ts", "jsdelivr": "dist/task-executor.min.js", "unpkg": "dist/task-executor.min.js", @@ -37,6 +45,7 @@ "test:e2e": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --config tests/e2e/jest.config.json tests/e2e/**.spec.ts --runInBand --forceExit", "test:cypress": "cypress run", "test:examples": "tsx tests/examples/examples.test.ts", + "test:import": "node --experimental-vm-modules node_modules/jest/bin/jest.js --config tests/import/jest.config.js", "lint": "npm run lint:ts && npm run lint:ts:tests && npm run lint:eslint", "lint:ts": "tsc --project tsconfig.json --noEmit", "lint:ts:tests": "tsc --project tests/tsconfig.json --noEmit", diff --git a/tests/import/import.test.mjs b/tests/import/import.test.mjs new file mode 100644 index 0000000..1d2e313 --- /dev/null +++ b/tests/import/import.test.mjs @@ -0,0 +1,6 @@ +describe("ESM Import", () => { + test("Import @golem-sdk/task-executor in ESM environment", async () => { + const { TaskExecutor } = await import("@golem-sdk/task-executor"); + expect(typeof TaskExecutor).toBe("function"); + }); +}); diff --git a/tests/import/jest.config.js b/tests/import/jest.config.js new file mode 100644 index 0000000..ad23c83 --- /dev/null +++ b/tests/import/jest.config.js @@ -0,0 +1,9 @@ +module.exports = { + verbose: true, + testEnvironment: "node", + // disable transforming source files because we want to execute the + // es modules directly + transform: {}, + moduleFileExtensions: ["js", "cjs", "mjs"], + testMatch: ["**/?(*.)+(spec|test).mjs", "**/?(*.)+(spec|test).cjs"], +}; diff --git a/tests/import/require.test.cjs b/tests/import/require.test.cjs new file mode 100644 index 0000000..51f8e39 --- /dev/null +++ b/tests/import/require.test.cjs @@ -0,0 +1,6 @@ +describe("CommonJS import", () => { + test("Require @golem-sdk/task-executor in CJS environment", async () => { + const { TaskExecutor } = require("@golem-sdk/task-executor"); + expect(typeof TaskExecutor).toBe("function"); + }); +});