Skip to content

Commit d91d5e6

Browse files
committed
fix: handle @babel/traverse CJS-ESM interop in .mts files
@babel/traverse is a CJS package that exports { default: fn }. In Node ESM (.mts), `import traverse from '@babel/traverse'` resolves to the module object, not the function, causing "traverse is not a function" at runtime. Use `_traverse.default ?? _traverse` to unwrap correctly.
1 parent 0e600f7 commit d91d5e6

4 files changed

Lines changed: 16 additions & 2 deletions

File tree

.config/vitest.config.isolated.mts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ const isCoverageEnabled =
3030
export default defineConfig({
3131
cacheDir: './.cache/vitest',
3232
test: {
33+
deps: {
34+
interopDefault: false,
35+
},
3336
globals: false,
3437
environment: 'node',
3538
// Only include tests that need isolation

.config/vitest.config.mts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ if (isCoverageEnabled) {
2525
export default defineConfig({
2626
cacheDir: './.cache/vitest',
2727
test: {
28+
deps: {
29+
interopDefault: false,
30+
},
2831
globals: false,
2932
environment: 'node',
3033
include: ['test/**/*.test.{js,ts,mjs,mts,cjs}'],

scripts/generate-sdk.mts

Lines changed: 5 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 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,10 @@ 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 interop: @babel/traverse exports { default: fn } from CJS.
30+
// @ts-expect-error -- runtime CJS has .default, types don't expose it.
31+
const traverse: typeof _traverse = _traverse.default
32+
2933
const OPENAPI_URL = 'https://api.socket.dev/v0/openapi'
3034

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

test/unit/bundle-validation.test.mts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ 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 interop: @babel/traverse exports { default: fn } from CJS.
15+
// @ts-expect-error -- runtime CJS has .default, types don't expose it.
16+
const traverse: typeof _traverse = _traverse.default
17+
1418
const __dirname = path.dirname(fileURLToPath(import.meta.url))
1519
const packagePath = path.resolve(__dirname, '../..')
1620
const distPath = path.join(packagePath, 'dist')

0 commit comments

Comments
 (0)