@@ -60,48 +60,77 @@ async function createApp() {
60
60
} ) ;
61
61
62
62
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
+ }
65
104
} ;
66
105
67
106
const { collectStyles} = require ( './styles.js' ) ;
68
107
globalThis . __vite_find_assets__ = async entries => {
69
108
return Object . keys ( await collectStyles ( viteServer , entries ) ) ;
70
109
} ;
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
- } ;
79
110
} else {
80
111
const reactServerManifest = JSON . parse (
81
112
await readFile ( 'build/react-server/manifest.json' , 'utf8' )
82
113
) ;
83
114
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 ;
96
118
} ;
97
119
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 ] ;
104
132
} ;
133
+
105
134
const { findAssetsInManifest} = require ( './manifest.js' ) ;
106
135
107
136
globalThis . __vite_find_assets__ = async entries => {
@@ -111,12 +140,23 @@ async function createApp() {
111
140
} ;
112
141
}
113
142
143
+ loadModule = async metadata => {
144
+ await __vite_preload__ ( metadata ) ;
145
+ return __vite_require__ ( metadata ) ;
146
+ } ;
147
+
114
148
async function renderApp ( res , returnValue ) {
115
149
const { renderToPipeableStream} = await import (
116
150
'react-server-dom-vite/server'
117
151
) ;
118
152
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
+ } ) ;
120
160
const root = React . createElement ( App ) ;
121
161
122
162
// For client-invoked server actions we refresh the tree and return a return value.
@@ -140,7 +180,7 @@ async function createApp() {
140
180
if ( serverReference ) {
141
181
// This is the client-side case
142
182
const [ filepath , name ] = serverReference . split ( '#' ) ;
143
- const action = ( await loadModule ( filepath ) ) [ name ] ;
183
+ const action = await loadModule ( { specifier : filepath , name} ) ;
144
184
// Validate that this is actually a function we intended to expose and
145
185
// not the client trying to invoke arbitrary functions. In a real app,
146
186
// you'd have a manifest verifying this before even importing it.
0 commit comments