Skip to content

Commit 340f01e

Browse files
committed
[v8] Add ESM / CJS dual build (#1287)
1 parent a78d761 commit 340f01e

File tree

11 files changed

+112
-28
lines changed

11 files changed

+112
-28
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ See the [API Reference](https://workos.com/docs/reference/client-libraries) for
1111

1212
## Requirements
1313

14-
Node 16 or higher.
14+
Node 18 or higher.
1515

1616
## Installation
1717

1818
Install the package with:
1919

2020
```
21-
npm i @workos-inc/node
21+
npm install @workos-inc/node
2222
```
2323

2424
## Configuration

eslint.config.mjs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ export default tseslint.config(
3333
'n/no-unsupported-features/node-builtins': 'off',
3434
},
3535
},
36+
{
37+
files: ['**/*.cjs'],
38+
languageOptions: {
39+
globals: {
40+
require: 'readonly',
41+
module: 'readonly',
42+
__dirname: 'readonly',
43+
__filename: 'readonly',
44+
exports: 'readonly',
45+
process: 'readonly',
46+
},
47+
},
48+
},
3649
{
3750
files: tests,
3851
extends: [jestPlugin.configs['flat/recommended']],
@@ -43,6 +56,7 @@ export default tseslint.config(
4356
'jest/no-disabled-tests': 'off',
4457
'jest/no-conditional-expect': 'off',
4558
'jest/valid-expect': 'off',
59+
'jest/expect-expect': 'off',
4660
},
4761
},
4862
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-env node */
12
const babelJest = require('babel-jest');
23

34
module.exports = babelJest.createTransformer({
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-env node */
12
module.exports = {
23
clearMocks: true,
34
resetMocks: true,
@@ -9,12 +10,9 @@ module.exports = {
910
setupFilesAfterEnv: ['./setup-jest.ts'],
1011
transform: {
1112
'^.+\\.ts?$': 'ts-jest',
12-
'^.+\\.m?js$': '<rootDir>/jest-transform-esm.js',
13+
'^.+\\.m?js$': '<rootDir>/jest-transform-esm.cjs',
1314
},
1415
transformIgnorePatterns: [
15-
'/node_modules/(?!(iron-session|uncrypto|cookie-es)/).+\\.m?js$',
16+
'node_modules/(?!(iron-session|uncrypto|cookie-es|@noble|@scure|jose)/)',
1617
],
17-
moduleNameMapper: {
18-
'^jose': require.resolve('jose'),
19-
},
2018
};

package.json

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111
"engines": {
1212
"node": ">=18"
1313
},
14-
"typings": "lib/index.d.ts",
14+
"type": "module",
15+
"main": "./lib/index.cjs",
16+
"module": "./lib/index.js",
17+
"types": "./lib/index.d.ts",
1518
"files": [
16-
"lib/"
19+
"lib/",
20+
"package.json"
1721
],
1822
"repository": {
1923
"type": "git",
@@ -24,7 +28,8 @@
2428
},
2529
"scripts": {
2630
"clean": "rm -rf lib",
27-
"build": "tsc -p .",
31+
"build": "tsup",
32+
"build:watch": "tsup --watch",
2833
"lint": "eslint",
2934
"test": "jest",
3035
"test:watch": "jest --watch",
@@ -45,7 +50,6 @@
4550
"@babel/preset-env": "^7.26.9",
4651
"@babel/preset-typescript": "^7.27.0",
4752
"@eslint/js": "^9.21.0",
48-
"@peculiar/webcrypto": "^1.4.5",
4953
"@types/cookie": "^0.6.0",
5054
"@types/jest": "^29.5.14",
5155
"@types/node": "~18",
@@ -62,22 +66,31 @@
6266
"prettier": "^3.5.3",
6367
"supertest": "7.1.0",
6468
"ts-jest": "29.3.1",
69+
"tsup": "^8.5.0",
6570
"typescript": "5.8.2",
6671
"typescript-eslint": "^8.25.0"
6772
},
6873
"exports": {
69-
"types": "./lib/index.d.ts",
70-
"workerd": {
71-
"import": "./lib/index.worker.js",
72-
"default": "./lib/index.worker.js"
74+
".": {
75+
"types": "./lib/index.d.ts",
76+
"workerd": {
77+
"import": "./lib/index.worker.js",
78+
"require": "./lib/index.worker.cjs"
79+
},
80+
"edge-light": {
81+
"import": "./lib/index.worker.js",
82+
"require": "./lib/index.worker.cjs"
83+
},
84+
"import": "./lib/index.js",
85+
"require": "./lib/index.cjs",
86+
"default": "./lib/index.js"
7387
},
74-
"edge-light": {
88+
"./worker": {
89+
"types": "./lib/index.worker.d.ts",
7590
"import": "./lib/index.worker.js",
91+
"require": "./lib/index.worker.cjs",
7692
"default": "./lib/index.worker.js"
7793
},
78-
"default": {
79-
"import": "./lib/index.js",
80-
"default": "./lib/index.js"
81-
}
94+
"./package.json": "./package.json"
8295
}
8396
}

setup-jest.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { enableFetchMocks } from 'jest-fetch-mock';
2-
import { Crypto } from '@peculiar/webcrypto';
2+
import { webcrypto } from 'crypto';
33

44
enableFetchMocks();
55

6-
// Assign Node's Crypto to global.crypto if it is not already present
6+
// Make Node's crypto.webcrypto available as global.crypto for tests
77
if (!global.crypto) {
8-
global.crypto = new Crypto();
8+
global.crypto = webcrypto as unknown as Crypto;
99
}

src/user-management/user-management.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,16 @@ describe('UserManagement', () => {
873873
});
874874

875875
describe('authenticateWithSessionCookie', () => {
876+
const OLD_ENV = process.env;
877+
878+
beforeEach(() => {
879+
process.env = { ...OLD_ENV };
880+
delete process.env.WORKOS_COOKIE_PASSWORD;
881+
});
882+
883+
afterEach(() => {
884+
process.env = OLD_ENV;
885+
});
876886
beforeEach(() => {
877887
// Mock createRemoteJWKSet
878888
jest
@@ -1074,6 +1084,16 @@ describe('UserManagement', () => {
10741084
});
10751085

10761086
describe('getSessionFromCookie', () => {
1087+
const OLD_ENV = process.env;
1088+
1089+
beforeEach(() => {
1090+
process.env = { ...OLD_ENV };
1091+
delete process.env.WORKOS_COOKIE_PASSWORD;
1092+
});
1093+
1094+
afterEach(() => {
1095+
process.env = OLD_ENV;
1096+
});
10771097
it('throws an error when the cookie password is undefined', async () => {
10781098
await expect(
10791099
workos.userManagement.getSessionFromCookie({

src/worker.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* @jest-environment miniflare
33
*/
44

5-
import { WorkOS } from './index.worker';
6-
7-
test.skip('WorkOS is initialized without errors', () => {
8-
expect(() => new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU')).not.toThrow();
5+
describe('Worker', () => {
6+
test.skip('WorkOS is initialized without errors', () => {
7+
// Skipped test - miniflare environment has issues with ESM modules
8+
});
99
});

src/workos.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ describe('WorkOS', () => {
3232

3333
describe('when no API key is provided', () => {
3434
it('throws a NoApiKeyFoundException error', async () => {
35+
delete process.env.WORKOS_API_KEY;
3536
expect(() => new WorkOS()).toThrowError(NoApiKeyProvidedException);
3637
});
3738
});

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919
"types": ["jest", "jest-environment-miniflare/globals"]
2020
},
2121
"include": ["src"]
22-
}
22+
}

0 commit comments

Comments
 (0)