@@ -60,48 +60,77 @@ async function createApp() {
6060 } ) ;
6161
6262 globalThis . __vite_module_cache__ = new Map ( ) ;
63- globalThis . __vite_require__ = id => {
64- return viteServer . ssrLoadModule ( id ) ;
63+ globalThis . __vite_preload__ = metadata => {
64+ const existingPromise = __vite_module_cache__ . get ( metadata . specifier ) ;
65+ if ( existingPromise ) {
66+ if ( existingPromise . status === 'fulfilled' ) {
67+ return null ;
68+ }
69+ return existingPromise ;
70+ } else {
71+ const modulePromise = viteServer . ssrLoadModule ( metadata . specifier ) ;
72+ modulePromise . then (
73+ value => {
74+ const fulfilledThenable = modulePromise ;
75+ fulfilledThenable . status = 'fulfilled' ;
76+ fulfilledThenable . value = value ;
77+ } ,
78+ reason => {
79+ const rejectedThenable = modulePromise ;
80+ rejectedThenable . status = 'rejected' ;
81+ rejectedThenable . reason = reason ;
82+ }
83+ ) ;
84+ __vite_module_cache__ . set ( metadata . specifier , modulePromise ) ;
85+ return modulePromise ;
86+ }
87+ } ;
88+
89+ globalThis . __vite_require__ = metadata => {
90+ let moduleExports ;
91+ // We assume that preloadModule has been called before, which
92+ // should have added something to the module cache.
93+ const promise = __vite_module_cache__ . get ( metadata . specifier ) ;
94+ if ( promise ) {
95+ if ( promise . status === 'fulfilled' ) {
96+ moduleExports = promise . value ;
97+ } else {
98+ throw promise . reason ;
99+ }
100+ return moduleExports [ metadata . name ] ;
101+ } else {
102+ throw new Error ( 'Module not found in cache: ' + id ) ;
103+ }
65104 } ;
66105
67106 const { collectStyles} = require ( './styles.js' ) ;
68107 globalThis . __vite_find_assets__ = async entries => {
69108 return Object . keys ( await collectStyles ( viteServer , entries ) ) ;
70109 } ;
71-
72- loadModule = async entry => {
73- return await viteServer . ssrLoadModule (
74- path . isAbsolute ( entry )
75- ? entry
76- : path . join ( viteServer . config . root , entry )
77- ) ;
78- } ;
79110 } else {
80111 const reactServerManifest = JSON . parse (
81112 await readFile ( 'build/react-server/manifest.json' , 'utf8' )
82113 ) ;
83114
84- loadModule = async entry => {
85- const id = reactServerManifest [ entry ] ;
86- if ( id ) {
87- return await import (
88- path . join ( process . cwd ( ) , 'build/react-server' , id . file )
89- ) ;
90- } else {
91- // this is probably a server action module
92- return await import (
93- path . join ( process . cwd ( ) , 'build/react-server' , entry + '.js' )
94- ) ;
95- }
115+ globalThis . __vite_module_cache__ = new Map ( ) ;
116+ globalThis . __vite_preload__ = metadata => {
117+ return null ;
96118 } ;
97119
98- globalThis . __vite_module_cache__ = new Map ( ) ;
99- globalThis . __vite_require__ = id => {
100- console . log ( { id} ) ;
101- return import (
102- path . join ( process . cwd ( ) , 'build' , 'react-server' , id + '.js' )
103- ) ;
120+ globalThis . __vite_require__ = metadata => {
121+ const module = require ( path . join (
122+ process . cwd ( ) ,
123+ 'build' ,
124+ 'server' ,
125+ metadata . specifier + '.cjs'
126+ ) ) ;
127+
128+ if ( metadata . name === 'default' ) {
129+ return module ;
130+ }
131+ return module [ metadata . name ] ;
104132 } ;
133+
105134 const { findAssetsInManifest} = require ( './manifest.js' ) ;
106135
107136 globalThis . __vite_find_assets__ = async entries => {
@@ -111,12 +140,23 @@ async function createApp() {
111140 } ;
112141 }
113142
143+ loadModule = async metadata => {
144+ await __vite_preload__ ( metadata ) ;
145+ return __vite_require__ ( metadata ) ;
146+ } ;
147+
114148 async function renderApp ( res , returnValue ) {
115149 const { renderToPipeableStream} = await import (
116150 'react-server-dom-vite/server'
117151 ) ;
118152
119- const { default : App } = await loadModule ( 'src/App.jsx' ) ;
153+ const App = await loadModule ( {
154+ specifier :
155+ process . env . NODE_ENV === 'development'
156+ ? path . join ( process . cwd ( ) , 'src/App.jsx' )
157+ : 'App' ,
158+ name : 'default' ,
159+ } ) ;
120160 const root = React . createElement ( App ) ;
121161
122162 // For client-invoked server actions we refresh the tree and return a return value.
@@ -140,7 +180,7 @@ async function createApp() {
140180 if ( serverReference ) {
141181 // This is the client-side case
142182 const [ filepath , name ] = serverReference . split ( '#' ) ;
143- const action = ( await loadModule ( filepath ) ) [ name ] ;
183+ const action = await loadModule ( { specifier : filepath , name} ) ;
144184 // Validate that this is actually a function we intended to expose and
145185 // not the client trying to invoke arbitrary functions. In a real app,
146186 // you'd have a manifest verifying this before even importing it.
0 commit comments