Commit 5362cf9
committed
feat: Universal runtime compatibility for Node.js SDK v8 - fixes import resolution across all target runtimes (#1301)
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.
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
**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 | 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 |
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
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
```bash
node -e "console.log('CJS:', require('./lib/cjs/index.cjs').WorkOS.name)"
node -e "import('./lib/esm/index.js').then(m => console.log('ESM:', m.WorkOS.name))"
deno eval "import('./lib/esm/index.js').then(m => console.log('Deno:', m.WorkOS.name))"
bun -e "console.log('Bun:', require('./lib/cjs/index.cjs').WorkOS.name)"
pnpm check:runtimes
```
- ✅ **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
- 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
```
[ ] Yes
```
This change improves internal module resolution and doesn't affect the
public API, so no documentation updates are required.1 parent e9c6d94 commit 5362cf9
File tree
7 files changed
+839
-74
lines changed- .github/workflows
- scripts
- src
- sso/interfaces
7 files changed
+839
-74
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 | + | |
0 commit comments