Skip to content

Commit 8c898a8

Browse files
committed
[v8] Add ESM / CJS dual build (#1287)
1 parent 3240f60 commit 8c898a8

File tree

11 files changed

+113
-29
lines changed

11 files changed

+113
-29
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-
yarn add @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: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515
"engines": {
1616
"node": ">=18"
1717
},
18-
"typings": "lib/index.d.ts",
18+
"type": "module",
19+
"main": "./lib/index.cjs",
20+
"module": "./lib/index.js",
21+
"types": "./lib/index.d.ts",
1922
"files": [
20-
"lib/"
23+
"lib/",
24+
"package.json"
2125
],
2226
"repository": {
2327
"type": "git",
@@ -28,14 +32,15 @@
2832
},
2933
"scripts": {
3034
"clean": "rm -rf lib",
31-
"build": "tsc -p .",
35+
"build": "tsup",
36+
"build:watch": "tsup --watch",
3237
"lint": "eslint",
3338
"test": "jest",
3439
"test:watch": "jest --watch",
3540
"test:worker": "jest src/worker.spec.ts",
3641
"prettier": "prettier \"src/**/*.{js,ts,tsx}\" --check",
3742
"format": "prettier \"src/**/*.{js,ts,tsx}\" --write",
38-
"prepublishOnly": "yarn run build"
43+
"prepublishOnly": "npm run build"
3944
},
4045
"dependencies": {
4146
"iron-session": "^8.0.4",
@@ -49,7 +54,6 @@
4954
"@babel/preset-env": "^7.26.9",
5055
"@babel/preset-typescript": "^7.27.0",
5156
"@eslint/js": "^9.21.0",
52-
"@peculiar/webcrypto": "^1.4.5",
5357
"@types/cookie": "^0.6.0",
5458
"@types/jest": "^29.5.14",
5559
"@types/node": "~18",
@@ -66,22 +70,31 @@
6670
"prettier": "^3.5.3",
6771
"supertest": "7.1.0",
6872
"ts-jest": "29.3.1",
73+
"tsup": "^8.5.0",
6974
"typescript": "5.8.2",
7075
"typescript-eslint": "^8.25.0"
7176
},
7277
"exports": {
73-
"types": "./lib/index.d.ts",
74-
"workerd": {
75-
"import": "./lib/index.worker.js",
76-
"default": "./lib/index.worker.js"
78+
".": {
79+
"types": "./lib/index.d.ts",
80+
"workerd": {
81+
"import": "./lib/index.worker.js",
82+
"require": "./lib/index.worker.cjs"
83+
},
84+
"edge-light": {
85+
"import": "./lib/index.worker.js",
86+
"require": "./lib/index.worker.cjs"
87+
},
88+
"import": "./lib/index.js",
89+
"require": "./lib/index.cjs",
90+
"default": "./lib/index.js"
7791
},
78-
"edge-light": {
92+
"./worker": {
93+
"types": "./lib/index.worker.d.ts",
7994
"import": "./lib/index.worker.js",
95+
"require": "./lib/index.worker.cjs",
8096
"default": "./lib/index.worker.js"
8197
},
82-
"default": {
83-
"import": "./lib/index.js",
84-
"default": "./lib/index.js"
85-
}
98+
"./package.json": "./package.json"
8699
}
87100
}

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
@@ -1031,6 +1041,16 @@ describe('UserManagement', () => {
10311041
});
10321042

10331043
describe('getSessionFromCookie', () => {
1044+
const OLD_ENV = process.env;
1045+
1046+
beforeEach(() => {
1047+
process.env = { ...OLD_ENV };
1048+
delete process.env.WORKOS_COOKIE_PASSWORD;
1049+
});
1050+
1051+
afterEach(() => {
1052+
process.env = OLD_ENV;
1053+
});
10341054
it('throws an error when the cookie password is undefined', async () => {
10351055
await expect(
10361056
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)