Skip to content

Commit 6290368

Browse files
committed
feat(rsc): standardize loadModule runner global
1 parent 3fd9997 commit 6290368

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

packages/plugin-rsc/src/plugin.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import {
6161
parseReferenceValidationVirtual,
6262
} from './plugins/shared'
6363
import { stripLiteral } from 'strip-literal'
64+
import type { ModuleRunner } from 'vite/module-runner'
6465

6566
const isRolldownVite = 'rolldownVersion' in vite
6667

@@ -301,6 +302,12 @@ export function vitePluginRscMinimal(
301302
]
302303
}
303304

305+
declare global {
306+
function __VITE_EXPERIMENTAL_GET_MODULE_RUNNER__(
307+
environmentName: string,
308+
): Promise<ModuleRunner>
309+
}
310+
304311
export default function vitePluginRsc(
305312
rscPluginOptions: RscPluginOptions = {},
306313
): Plugin[] {
@@ -516,7 +523,22 @@ export default function vitePluginRsc(
516523
},
517524
},
518525
configureServer(server) {
519-
;(globalThis as any).__viteRscDevServer = server
526+
globalThis.__VITE_EXPERIMENTAL_GET_MODULE_RUNNER__ = async function (
527+
environmentName,
528+
) {
529+
const environment = server.environments[environmentName]
530+
if (!environment) {
531+
throw new Error(
532+
`[vite-rsc] unknown environment '${environmentName}'`,
533+
)
534+
}
535+
if (!vite.isRunnableDevEnvironment(environment)) {
536+
throw new Error(
537+
`[vite-rsc] environment '${environmentName}' is not runnable`,
538+
)
539+
}
540+
return environment.runner
541+
}
520542

521543
// intercept client hmr to propagate client boundary invalidation to server environment
522544
const oldSend = server.environments.client.hot.send
@@ -768,10 +790,9 @@ export default function vitePluginRsc(
768790
const source = getEntrySource(environment.config, entryName)
769791
const resolved = await environment.pluginContainer.resolveId(source)
770792
assert(resolved, `[vite-rsc] failed to resolve entry '${source}'`)
771-
replacement =
772-
`globalThis.__viteRscDevServer.environments[${JSON.stringify(
773-
environmentName,
774-
)}]` + `.runner.import(${JSON.stringify(resolved.id)})`
793+
const environmentNameEscaped = JSON.stringify(environmentName)
794+
const resolveIdEscaped = JSON.stringify(resolved.id)
795+
replacement = `globalThis.__VITE_EXPERIMENTAL_GET_MODULE_RUNNER__(${environmentNameEscaped}).then(runner => runner.import(${resolveIdEscaped}))`
775796
} else {
776797
replacement = JSON.stringify(
777798
`__vite_rsc_load_module:${this.environment.name}:${environmentName}:${entryName}`,

0 commit comments

Comments
 (0)