Skip to content

Commit 472996a

Browse files
TobbeJosh-Walker-GM
authored andcommitted
chore(rsc): Simplify worker by not using vite config (#11430)
1 parent ec5686f commit 472996a

File tree

2 files changed

+18
-128
lines changed

2 files changed

+18
-128
lines changed

packages/vite/src/plugins/vite-plugin-rsc-transform-server.ts

-7
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,6 @@ export function rscTransformUseServerPlugin(
8484
}
8585
}
8686

87-
console.log('windows paths')
88-
console.log('windows paths, outDir:', outDir)
89-
console.log('windows paths, severEntryFiles:', serverEntryFiles)
90-
console.log('windows paths, severEntryKey:', serverEntryKey)
91-
console.log('windows paths, builtFileName:', builtFileName)
92-
console.log('windows paths')
93-
9487
if (!builtFileName) {
9588
throw new Error(
9689
`Could not find ${id} in serverEntryFiles: ` +

packages/vite/src/rsc/rscWorker.ts

+18-121
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import { parentPort } from 'node:worker_threads'
1111
import { createElement } from 'react'
1212

1313
import RSDWServer from 'react-server-dom-webpack/server'
14-
import type { ResolvedConfig } from 'vite'
15-
import { resolveConfig } from 'vite'
1614

1715
import { getPaths } from '@redwoodjs/project-config'
1816
import {
@@ -155,44 +153,14 @@ parentPort.on('message', (message: MessageReq) => {
155153
}
156154
})
157155

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-
180156
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+
)
196164

197165
if (!routesPath) {
198166
throw new StatusError('No entry found for __rwjs__Routes', 404)
@@ -203,56 +171,7 @@ const getRoutesComponent: any = async () => {
203171
return routes.default
204172
}
205173

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-zA-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-
253174
async function setClientEntries(): Promise<void> {
254-
const config = await getViteConfig()
255-
256175
const entriesFile = getPaths().web.distRscEntries
257176
console.log('setClientEntries :: entriesFile', entriesFile)
258177
const { clientEntries } = await loadServerFile(entriesFile)
@@ -271,7 +190,7 @@ async function setClientEntries(): Promise<void> {
271190
fullKey = fullKey.replaceAll('\\', '/')
272191
}
273192

274-
return [fullKey, config.base + val]
193+
return [fullKey, '/' + val]
275194
}),
276195
)
277196

@@ -281,25 +200,7 @@ async function setClientEntries(): Promise<void> {
281200
)
282201
}
283202

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() {
303204
// TODO (RSC): Try removing the proxy here and see if it's really necessary.
304205
// Looks like it'd work to just have a regular object with a getter.
305206
// Remove the proxy and see what breaks.
@@ -312,15 +213,14 @@ function getBundlerConfig(config: ConfigType) {
312213
// filePath /Users/tobbe/dev/waku/examples/01_counter/dist/assets/rsc0.js
313214
// name Counter
314215

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]
317218

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)
324224
}
325225

326226
console.log('rscWorker proxy id', id)
@@ -346,15 +246,13 @@ async function renderRsc(input: RenderInput): Promise<PipeableStream> {
346246

347247
console.log('renderRsc input', input)
348248

349-
const config = await getViteConfig()
350-
351249
const serverRoutes = await getRoutesComponent()
352250
const element = createElement(serverRoutes, input.props)
353251

354252
console.log('rscWorker.ts renderRsc renderRsc props', input.props)
355253
console.log('rscWorker.ts renderRsc element', element)
356254

357-
return renderToPipeableStream(element, getBundlerConfig(config))
255+
return renderToPipeableStream(element, getBundlerConfig())
358256
// TODO (RSC): We used to transform() the stream here to remove
359257
// "prefixToRemove", which was the common base path to all filenames. We
360258
// then added it back in handleRsa with a simple
@@ -405,7 +303,6 @@ async function handleRsa(input: RenderInput): Promise<PipeableStream> {
405303

406304
const data = await method(...input.args)
407305
console.log('rscWorker.ts rsa return data', data)
408-
const config = await getViteConfig()
409306

410307
const serverRoutes = await getRoutesComponent()
411308
console.log('rscWorker.ts handleRsa serverRoutes', serverRoutes)
@@ -417,5 +314,5 @@ async function handleRsa(input: RenderInput): Promise<PipeableStream> {
417314
}
418315
console.log('rscWorker.ts handleRsa elements', elements)
419316

420-
return renderToPipeableStream(elements, getBundlerConfig(config))
317+
return renderToPipeableStream(elements, getBundlerConfig())
421318
}

0 commit comments

Comments
 (0)