Skip to content

Commit 970bf65

Browse files
committed
fix: preserve runtime imports under Turbopack
1 parent 2467771 commit 970bf65

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { createRequire } from 'node:module'
2-
import { pathToFileURL } from 'node:url'
1+
import { importServicePackage } from './services-install'
32

43
/**
54
* Resolve and import a package at runtime without adding it to a consumer's
@@ -9,6 +8,5 @@ import { pathToFileURL } from 'node:url'
98
* @internal
109
*/
1110
export async function importRuntimeModule<T = unknown>(specifier: string): Promise<T> {
12-
const resolved = createRequire(import.meta.url).resolve(specifier)
13-
return await import(/* webpackIgnore: true */ /* @vite-ignore */ /* turbopackIgnore: true */ pathToFileURL(resolved).href) as T
11+
return await importServicePackage(specifier, [import.meta.url]) as T
1412
}

tests/optional-mcp-bundles.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Plugin } from 'esbuild'
2-
import { mkdtempSync, rmSync } from 'node:fs'
2+
import { mkdtempSync, readdirSync, readFileSync, rmSync } from 'node:fs'
33
import { join } from 'node:path'
44
import { fileURLToPath, pathToFileURL } from 'node:url'
55
import { build } from 'esbuild'
@@ -12,6 +12,7 @@ const entries = [
1212
'packages/hub/dist/node/initiate.mjs',
1313
'packages/next/dist/hub.mjs',
1414
]
15+
const nextServerChunks = join(root, 'examples/hub-next/src/client/.next/server/chunks')
1516
const temporaryDirectories: string[] = []
1617

1718
afterEach(() => {
@@ -98,4 +99,14 @@ describe('optional MCP peers in consumer bundles', () => {
9899
await hub.close()
99100
}
100101
})
102+
103+
it('preserves the runtime importer in Next production bundles', () => {
104+
const runtimeImportChunks = readdirSync(nextServerChunks)
105+
.filter(file => file.endsWith('.js'))
106+
.map(file => readFileSync(join(nextServerChunks, file), 'utf8'))
107+
.filter(source => source.includes('packages/devframe/src/node/import-runtime-module.ts'))
108+
109+
expect(runtimeImportChunks).not.toHaveLength(0)
110+
expect(runtimeImportChunks.join('\n')).not.toContain('Cannot find module as expression is too dynamic')
111+
})
101112
})

0 commit comments

Comments
 (0)