Skip to content

Commit 9d885fa

Browse files
authored
fix: resolve @babel/traverse CJS/ESM interop for native .mts execution (#590)
* fix: resolve @babel/traverse CJS/ESM interop for native .mts execution Node's ESM loader wraps CJS modules so the default export is a namespace object, not the function itself. Extract `.default` at runtime with a cast to satisfy TypeScript. Also bump .node-version to 25.9.0. * fix: use consistent @babel/traverse CJS/ESM interop pattern Apply the same runtime-safe interop resolution in both generate-sdk.mts and bundle-validation.test.mts. The typeof check handles both Vitest (where the import may already be the function) and native Node ESM (where it's wrapped under .default). * refactor: simplify traverse interop to nullish coalescing
1 parent 7939e74 commit 9d885fa

3 files changed

Lines changed: 9 additions & 3 deletions

File tree

.node-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
25.8.2
1+
25.9.0

scripts/generate-sdk.mts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import path from 'node:path'
1515
import process from 'node:process'
1616

1717
import { parse } from '@babel/parser'
18-
import { default as traverse } from '@babel/traverse'
18+
import _traverse from '@babel/traverse'
1919
import * as t from '@babel/types'
2020
import MagicString from 'magic-string'
2121

@@ -26,6 +26,9 @@ import { spawn } from '@socketsecurity/lib/spawn'
2626
import { getRootPath } from './utils/path-helpers.mts'
2727
import { runCommand } from './utils/run-command.mts'
2828

29+
// CJS/ESM interop: @babel/traverse wraps the function under .default in ESM
30+
const traverse = ((_traverse as any).default ?? _traverse) as typeof _traverse
31+
2932
const OPENAPI_URL = 'https://api.socket.dev/v0/openapi'
3033

3134
const rootPath = getRootPath(import.meta.url)

test/unit/bundle-validation.test.mts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ import path from 'node:path'
88
import { fileURLToPath } from 'node:url'
99

1010
import { parse } from '@babel/parser'
11-
import { default as traverse } from '@babel/traverse'
11+
import _traverse from '@babel/traverse'
1212
import { describe, expect, it } from 'vitest'
1313

14+
// CJS/ESM interop: @babel/traverse wraps the function under .default in ESM
15+
const traverse = ((_traverse as any).default ?? _traverse) as typeof _traverse
16+
1417
const __dirname = path.dirname(fileURLToPath(import.meta.url))
1518
const packagePath = path.resolve(__dirname, '../..')
1619
const distPath = path.join(packagePath, 'dist')

0 commit comments

Comments
 (0)