Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSC: getViteConfig in rscWorker #10567

Merged
merged 1 commit into from
May 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 23 additions & 8 deletions packages/vite/src/rsc/rscWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,25 @@ parentPort.on('message', (message: MessageReq) => {

// Let me re-assign root
type ConfigType = Omit<ResolvedConfig, 'root'> & { root: string }
const configPromise: Promise<ConfigType> = resolveConfig({}, 'serve')

/**
* Gets the Vite config.
* Makes sure root is configured properly and then caches the result
*/
async function getViteConfig() {
let cachedConfig: ConfigType | null = null

return (async () => {
if (cachedConfig) {
return cachedConfig
}

cachedConfig = await resolveConfig({}, 'serve')
setRootInConfig(cachedConfig)

return cachedConfig
})()
}

const getFunctionComponent = async (rscId: string) => {
// TODO (RSC): Get rid of this when we only use the worker in dev mode
Expand Down Expand Up @@ -276,8 +294,7 @@ function resolveClientEntryForDev(id: string, config: { base: string }) {
}

async function setClientEntries(): Promise<void> {
// This is the Vite config
const config = await configPromise
const config = await getViteConfig()

const entriesFile = getPaths().web.distRscEntries
console.log('setClientEntries :: entriesFile', entriesFile)
Expand Down Expand Up @@ -372,8 +389,7 @@ async function renderRsc(input: RenderInput): Promise<PipeableStream> {

console.log('renderRsc input', input)

const config = await configPromise
setRootInConfig(config)
const config = await getViteConfig()

const component = await getFunctionComponent(input.rscId)

Expand All @@ -393,15 +409,14 @@ function isSerializedFormData(data?: unknown): data is SerializedFormData {
}

async function handleRsa(input: RenderInput): Promise<PipeableStream> {
const config = await configPromise
setRootInConfig(config)

console.log('handleRsa input', input)

if (!input.rsfId || !input.args) {
throw new Error('Unexpected input')
}

const config = await getViteConfig()

const [fileId, name] = input.rsfId.split('#')
const fname = path.join(config.root, fileId)
console.log('Server Action, fileId', fileId, 'name', name, 'fname', fname)
Expand Down