@@ -11,8 +11,6 @@ import { parentPort } from 'node:worker_threads'
11
11
import { createElement } from 'react'
12
12
13
13
import RSDWServer from 'react-server-dom-webpack/server'
14
- import type { ResolvedConfig } from 'vite'
15
- import { resolveConfig } from 'vite'
16
14
17
15
import { getPaths } from '@redwoodjs/project-config'
18
16
import {
@@ -155,44 +153,14 @@ parentPort.on('message', (message: MessageReq) => {
155
153
}
156
154
} )
157
155
158
- // Let me re-assign root
159
- type ConfigType = Omit < ResolvedConfig , 'root' > & { root : string }
160
-
161
- /**
162
- * Gets the Vite config.
163
- * Makes sure root is configured properly and then caches the result
164
- */
165
- async function getViteConfig ( ) {
166
- let cachedConfig : ConfigType | null = null
167
-
168
- return ( async ( ) => {
169
- if ( cachedConfig ) {
170
- return cachedConfig
171
- }
172
-
173
- cachedConfig = await resolveConfig ( { } , 'serve' )
174
- setRootInConfig ( cachedConfig )
175
-
176
- return cachedConfig
177
- } ) ( )
178
- }
179
-
180
156
const getRoutesComponent : any = async ( ) => {
181
- // TODO (RSC): Get rid of this when we only use the worker in dev mode
182
- const isDev = Object . keys ( absoluteClientEntries ) . length === 0
183
-
184
- let routesPath : string | undefined
185
- if ( isDev ) {
186
- routesPath = getPaths ( ) . web . routes
187
- } else {
188
- const serverEntries = await getEntriesFromDist ( )
189
- console . log ( 'rscWorker.ts serverEntries' , serverEntries )
190
-
191
- routesPath = path . join (
192
- getPaths ( ) . web . distRsc ,
193
- serverEntries [ '__rwjs__Routes' ] ,
194
- )
195
- }
157
+ const serverEntries = await getEntriesFromDist ( )
158
+ console . log ( 'rscWorker.ts serverEntries' , serverEntries )
159
+
160
+ const routesPath = path . join (
161
+ getPaths ( ) . web . distRsc ,
162
+ serverEntries [ '__rwjs__Routes' ] ,
163
+ )
196
164
197
165
if ( ! routesPath ) {
198
166
throw new StatusError ( 'No entry found for __rwjs__Routes' , 404 )
@@ -203,56 +171,7 @@ const getRoutesComponent: any = async () => {
203
171
return routes . default
204
172
}
205
173
206
- function resolveClientEntryForProd (
207
- filePath : string ,
208
- config : Awaited < ReturnType < typeof resolveConfig > > ,
209
- ) {
210
- const filePathSlash = filePath . replaceAll ( '\\' , '/' )
211
- const clientEntry = absoluteClientEntries [ filePathSlash ]
212
-
213
- console . log ( 'absoluteClientEntries' , absoluteClientEntries )
214
- console . log ( 'filePath' , filePathSlash )
215
-
216
- if ( ! clientEntry ) {
217
- if ( absoluteClientEntries [ '*' ] === '*' ) {
218
- return config . base + path . relative ( config . root , filePathSlash )
219
- }
220
-
221
- throw new Error ( 'No client entry found for ' + filePathSlash )
222
- }
223
-
224
- return clientEntry
225
- }
226
-
227
- function fileURLToFilePath ( fileURL : string ) {
228
- if ( ! fileURL . startsWith ( 'file://' ) ) {
229
- throw new Error ( 'Not a file URL' )
230
- }
231
- return decodeURI ( fileURL . slice ( 'file://' . length ) )
232
- }
233
-
234
- const ABSOLUTE_WIN32_PATH_REGEXP = / ^ \/ [ a - z A - Z ] : \/ /
235
-
236
- function encodeFilePathToAbsolute ( filePath : string ) {
237
- if ( ABSOLUTE_WIN32_PATH_REGEXP . test ( filePath ) ) {
238
- throw new Error ( 'Unsupported absolute file path' )
239
- }
240
- if ( filePath . startsWith ( '/' ) ) {
241
- return filePath
242
- }
243
- return '/' + filePath
244
- }
245
-
246
- function resolveClientEntryForDev ( id : string , config : { base : string } ) {
247
- console . log ( 'resolveClientEntryForDev config.base' , config . base )
248
- const filePath = id . startsWith ( 'file://' ) ? fileURLToFilePath ( id ) : id
249
- // HACK this relies on Vite's internal implementation detail.
250
- return config . base + '@fs' + encodeFilePathToAbsolute ( filePath )
251
- }
252
-
253
174
async function setClientEntries ( ) : Promise < void > {
254
- const config = await getViteConfig ( )
255
-
256
175
const entriesFile = getPaths ( ) . web . distRscEntries
257
176
console . log ( 'setClientEntries :: entriesFile' , entriesFile )
258
177
const { clientEntries } = await loadServerFile ( entriesFile )
@@ -271,7 +190,7 @@ async function setClientEntries(): Promise<void> {
271
190
fullKey = fullKey . replaceAll ( '\\' , '/' )
272
191
}
273
192
274
- return [ fullKey , config . base + val ]
193
+ return [ fullKey , '/' + val ]
275
194
} ) ,
276
195
)
277
196
@@ -281,25 +200,7 @@ async function setClientEntries(): Promise<void> {
281
200
)
282
201
}
283
202
284
- function setRootInConfig ( config : ConfigType ) {
285
- const rwPaths = getPaths ( )
286
-
287
- // TODO (RSC): Should root be configurable by the user? We probably need it
288
- // to be different values in different contexts. Should we introduce more
289
- // config options?
290
- // config.root currently comes from the user's project, where it in turn
291
- // comes from our `redwood()` vite plugin defined in index.ts. By default
292
- // (i.e. in the redwood() plugin) it points to <base>/web/src. But we need it
293
- // to be just <base>/, so for now we override it here.
294
- config . root =
295
- process . platform === 'win32'
296
- ? rwPaths . base . replaceAll ( '\\' , '/' )
297
- : rwPaths . base
298
- console . log ( 'config.root' , config . root )
299
- console . log ( 'rwPaths.base' , rwPaths . base )
300
- }
301
-
302
- function getBundlerConfig ( config : ConfigType ) {
203
+ function getBundlerConfig ( ) {
303
204
// TODO (RSC): Try removing the proxy here and see if it's really necessary.
304
205
// Looks like it'd work to just have a regular object with a getter.
305
206
// Remove the proxy and see what breaks.
@@ -312,15 +213,14 @@ function getBundlerConfig(config: ConfigType) {
312
213
// filePath /Users/tobbe/dev/waku/examples/01_counter/dist/assets/rsc0.js
313
214
// name Counter
314
215
315
- // TODO (RSC): Get rid of this when we only use the worker in dev mode
316
- const isDev = Object . keys ( absoluteClientEntries ) . length === 0
216
+ const filePathSlash = filePath . replaceAll ( '\\' , '/' )
217
+ const id = absoluteClientEntries [ filePathSlash ]
317
218
318
- let id : string
319
- if ( isDev ) {
320
- id = resolveClientEntryForDev ( filePath , config )
321
- } else {
322
- // Needs config.root to be set properly
323
- id = resolveClientEntryForProd ( filePath , config )
219
+ console . log ( 'absoluteClientEntries' , absoluteClientEntries )
220
+ console . log ( 'filePath' , filePathSlash )
221
+
222
+ if ( ! id ) {
223
+ throw new Error ( 'No client entry found for ' + filePathSlash )
324
224
}
325
225
326
226
console . log ( 'rscWorker proxy id' , id )
@@ -346,15 +246,13 @@ async function renderRsc(input: RenderInput): Promise<PipeableStream> {
346
246
347
247
console . log ( 'renderRsc input' , input )
348
248
349
- const config = await getViteConfig ( )
350
-
351
249
const serverRoutes = await getRoutesComponent ( )
352
250
const element = createElement ( serverRoutes , input . props )
353
251
354
252
console . log ( 'rscWorker.ts renderRsc renderRsc props' , input . props )
355
253
console . log ( 'rscWorker.ts renderRsc element' , element )
356
254
357
- return renderToPipeableStream ( element , getBundlerConfig ( config ) )
255
+ return renderToPipeableStream ( element , getBundlerConfig ( ) )
358
256
// TODO (RSC): We used to transform() the stream here to remove
359
257
// "prefixToRemove", which was the common base path to all filenames. We
360
258
// then added it back in handleRsa with a simple
@@ -405,7 +303,6 @@ async function handleRsa(input: RenderInput): Promise<PipeableStream> {
405
303
406
304
const data = await method ( ...input . args )
407
305
console . log ( 'rscWorker.ts rsa return data' , data )
408
- const config = await getViteConfig ( )
409
306
410
307
const serverRoutes = await getRoutesComponent ( )
411
308
console . log ( 'rscWorker.ts handleRsa serverRoutes' , serverRoutes )
@@ -417,5 +314,5 @@ async function handleRsa(input: RenderInput): Promise<PipeableStream> {
417
314
}
418
315
console . log ( 'rscWorker.ts handleRsa elements' , elements )
419
316
420
- return renderToPipeableStream ( elements , getBundlerConfig ( config ) )
317
+ return renderToPipeableStream ( elements , getBundlerConfig ( ) )
421
318
}
0 commit comments