Commit 522b0b8
authored
feat: Universal runtime compatibility for Node.js SDK v8 - fixes import resolution across all target runtimes (#1301)
## Description
This PR implements comprehensive universal runtime compatibility for the
WorkOS Node.js SDK v8, addressing critical module resolution issues that
prevented the package from working correctly across different JavaScript
runtimes.
### Problem Statement
The current v8 build configuration with `tsup` and `bundle: false`
created incompatible import patterns:
- **CJS files** attempted to `require()` ESM files (`.js` extension)
- **ESM files** imported without explicit file extensions, breaking
strict runtimes like Deno
- **Standalone Node.js** execution failed due to cross-format imports
- **Edge Runtime** and **Cloudflare Workers** compatibility was broken
### Solution Overview
**Phase 1: Import Path Rewriting** ✅
- Implemented custom `fixImportsPlugin` to rewrite import paths during
build
- ESM builds now use explicit `.js` extensions for all relative imports
- CJS builds use `.cjs` extensions for internal module references
- Maintains bundler compatibility while fixing standalone runtime
execution
**Phase 2: Enhanced Export Mapping** ✅
- Added runtime-specific export conditions for optimal module resolution
- Separate TypeScript type paths for CJS (`.d.cts`) and ESM (`.d.ts`)
- Dedicated worker builds for edge runtimes (`workerd`, `edge-light`)
- Performance-ordered conditions for faster resolution
**Phase 3: Comprehensive Testing Infrastructure** ✅
- Created ecosystem compatibility test suite covering 6 runtime
scenarios
- Manual validation commands for immediate testing
- Automated CI/CD workflow with matrix strategy testing Node.js 18/20,
Deno, and Bun
- Fail-fast smoke tests for quick compatibility validation
### Runtime Compatibility Matrix
| Runtime | CJS Build | ESM Build | Status | Notes |
|---------|-----------|-----------|---------|-------|
| **Next.js/webpack** | ✅ | ✅ | Working | Bundler handles resolution |
| **Node.js standalone** | ✅ | ✅ | **FIXED** | Import rewriting resolved
cross-format issues |
| **Deno** | N/A | ✅ | **FIXED** | Explicit .js extensions now provided
|
| **Bun** | ✅ | ✅ | Working | Enhanced with runtime-specific exports |
| **Edge Runtime** | ✅ | ✅ | **FIXED** | Dedicated worker builds |
| **Cloudflare Workers** | ✅ | ✅ | **FIXED** | Optimized for workerd
environment |
### Key Technical Changes
1. **Custom Import Rewriter Plugin** (`scripts/fix-imports-plugin.ts`)
- Transforms relative imports to include appropriate file extensions
- Format-aware: `.js` for ESM, `.cjs` for CommonJS
- Preserves external module imports unchanged
2. **Enhanced Package.json Exports**
```json
{
"exports": {
".": {
"types": {
"require": "./lib/cjs/index.d.cts",
"import": "./lib/esm/index.d.ts"
},
"workerd": "./lib/esm/index.worker.js",
"edge-light": "./lib/esm/index.worker.js",
"deno": "./lib/esm/index.js",
"bun": {
"import": "./lib/esm/index.js",
"require": "./lib/cjs/index.cjs"
},
"node": {
"import": "./lib/esm/index.js",
"require": "./lib/cjs/index.cjs"
},
"import": "./lib/esm/index.js",
"require": "./lib/cjs/index.cjs",
"default": "./lib/esm/index.js"
}
}
}
```
3. **CI/CD Integration** (`.github/workflows/runtime-tests.yml`)
- Matrix strategy testing across Node.js versions and alternative
runtimes
- Automated validation on every PR and push
- Quick smoke tests for immediate feedback
### Testing Results
All 6 runtime scenarios now pass:
- ✅ Node.js CJS: `WorkOSNode`
- ✅ Node.js ESM: `WorkOSNode`
- ✅ Deno: `WorkOSNode`
- ✅ Bun CJS: `WorkOSNode`
- ✅ Bun ESM: `WorkOSNode`
- ✅ Worker: Module resolution successful
### Verification Commands
```bash
# Node.js CJS
node -e "console.log('CJS:', require('./lib/cjs/index.cjs').WorkOS.name)"
# Node.js ESM
node -e "import('./lib/esm/index.js').then(m => console.log('ESM:', m.WorkOS.name))"
# Deno
deno eval "import('./lib/esm/index.js').then(m => console.log('Deno:', m.WorkOS.name))"
# Bun
bun -e "console.log('Bun:', require('./lib/cjs/index.cjs').WorkOS.name)"
# Comprehensive test suite
pnpm check:runtimes
```
### Backward Compatibility
- ✅ **No breaking changes** to existing API surface
- ✅ **Bundler compatibility** maintained (Next.js, webpack, Vite, etc.)
- ✅ **Tree-shaking** still works properly
- ✅ **Package size** unchanged (unbundled builds)
- ✅ **TypeScript support** enhanced with improved type resolution
### Risk Mitigation
- Extensive testing across all target runtimes before merge
- Automated CI validation prevents regressions
- Rollback plan available (revert to `bundle: true` if needed)
- Industry-standard patterns based on OpenAI SDK, Drizzle ORM practices
## Documentation
```
[ ] Yes
```
This change improves internal module resolution and doesn't affect the
public API, so no documentation updates are required.1 parent bee7e85 commit 522b0b8
File tree
7 files changed
+278
-60
lines changed- .github/workflows
- scripts
- src
- sso/interfaces
7 files changed
+278
-60
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
20 | | - | |
21 | | - | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | | - | |
| 40 | + | |
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
| |||
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
| 57 | + | |
57 | 58 | | |
58 | 59 | | |
59 | 60 | | |
60 | 61 | | |
61 | 62 | | |
| 63 | + | |
62 | 64 | | |
63 | 65 | | |
64 | 66 | | |
| 67 | + | |
65 | 68 | | |
66 | 69 | | |
67 | 70 | | |
| 71 | + | |
68 | 72 | | |
69 | 73 | | |
70 | 74 | | |
71 | 75 | | |
72 | 76 | | |
| 77 | + | |
73 | 78 | | |
74 | 79 | | |
75 | 80 | | |
76 | 81 | | |
77 | 82 | | |
78 | | - | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
79 | 87 | | |
80 | | - | |
81 | | - | |
| 88 | + | |
| 89 | + | |
82 | 90 | | |
83 | 91 | | |
84 | | - | |
85 | | - | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
86 | 99 | | |
87 | | - | |
88 | | - | |
89 | | - | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
90 | 107 | | |
91 | 108 | | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
96 | 116 | | |
97 | 117 | | |
98 | 118 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
16 | 16 | | |
This file was deleted.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
69 | | - | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
70 | 73 | | |
71 | 74 | | |
72 | 75 | | |
| |||
0 commit comments