Skip to content

fix(openai): use new Function trick for node: dynamic imports#33

Merged
OwenMcGirr merged 1 commit intomainfrom
fix/32-openai-node-dynamic-import-turbopack
Apr 8, 2026
Merged

fix(openai): use new Function trick for node: dynamic imports#33
OwenMcGirr merged 1 commit intomainfrom
fix/32-openai-node-dynamic-import-turbopack

Conversation

@OwenMcGirr
Copy link
Copy Markdown
Collaborator

Problem

The template literal import(\node:${name}`)intextToSpeechandtextToSpeechStreaminginsideopenai.tscauses **build failures** in Turbopack (Next.js 16) and Vite. Both bundlers attempt to statically resolve dynamic imports and cannot handle anode:` prefix with a dynamic suffix.

Reproduces as:

Module not found: Can't resolve 'node:' <dynamic no slash>
  const importNodeBuiltin = async (name) => import(`node:${name}`);

Fix

Replace with the new Function pattern already used in abstract-tts.ts, audio-converter.ts, and playht.ts:

// Before
const importNodeBuiltin = async (name: string) => import(`node:${name}`);

// After  
const importNodeBuiltin = (name: string) => new Function('m', 'return import(m)')(name);

Fixes #32

The template literal `import(\`node:\${name}\`)` in textToSpeech and
textToSpeechStreaming fails at build time in Turbopack and Vite, which
try to statically analyze the import and cannot resolve 'node:' with a
dynamic suffix.

Replace with the same `new Function('m', 'return import(m)')` pattern
already used in abstract-tts.ts, audio-converter.ts, and playht.ts —
this hides the dynamic import from static analysis while preserving
identical runtime behaviour.

Fixes #32
@OwenMcGirr OwenMcGirr merged commit 56e9294 into main Apr 8, 2026
6 checks passed
@OwenMcGirr OwenMcGirr deleted the fix/32-openai-node-dynamic-import-turbopack branch April 8, 2026 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Browser bundle: dynamic node: imports break Turbopack/Vite builds

1 participant