Skip to content

Commit

Permalink
SSR: Extract buildForStreamingServer function
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe committed Mar 2, 2024
1 parent b41ca33 commit d04e834
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 20 deletions.
32 changes: 12 additions & 20 deletions packages/vite/src/buildFeServer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { build as viteBuild } from 'vite'

import { buildWeb } from '@redwoodjs/internal/dist/build/web'
import { getConfig, getPaths } from '@redwoodjs/project-config'

import { buildRouteHooks } from './buildRouteHooks'
import { buildRouteManifest } from './buildRouteManifest'
import { buildRscFeServer } from './buildRscFeServer'
import { buildForStreamingServer } from './streaming/buildForStreamingServer'
import { ensureProcessDirWeb } from './utils'

export interface BuildOptions {
Expand All @@ -20,6 +19,9 @@ export const buildFeServer = async ({ verbose, webDir }: BuildOptions = {}) => {
const rwConfig = getConfig()
const viteConfigPath = rwPaths.web.viteConfig

const rscEnabled = rwConfig.experimental?.rsc?.enabled
const streamingSsrEnabled = rwConfig.experimental?.streamingSsr?.enabled

if (!viteConfigPath) {
throw new Error(
'Vite config not found. You need to setup your project with Vite ' +
Expand All @@ -35,7 +37,7 @@ export const buildFeServer = async ({ verbose, webDir }: BuildOptions = {}) => {
)
}

if (rwConfig.experimental?.rsc?.enabled) {
if (rscEnabled) {
if (!rwPaths.web.entries) {
throw new Error('RSC entries file not found')
}
Expand All @@ -50,24 +52,14 @@ export const buildFeServer = async ({ verbose, webDir }: BuildOptions = {}) => {
//
}

//
// SSR Specific code below
//

// Step 1A: Generate the client bundle
await buildWeb({ verbose })
// We generate the RSC client bundle in the rscBuildClient function
// Streaming and RSC client bundles are **not** the same
if (streamingSsrEnabled && !rscEnabled) {
console.log('Building client for streaming SSR...\n')
await buildWeb({ verbose })
}

// Step 1B: Generate the server output
await viteBuild({
configFile: viteConfigPath,
build: {
outDir: rwPaths.web.distServer,
ssr: true, // use boolean here, instead of string.
// rollup inputs are defined in the vite plugin
},
envFile: false,
logLevel: verbose ? 'info' : 'warn',
})
await buildForStreamingServer({ verbose })

await buildRouteHooks(verbose, rwPaths)

Expand Down
33 changes: 33 additions & 0 deletions packages/vite/src/streaming/buildForStreamingServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { build as viteBuild } from 'vite'

import { getPaths } from '@redwoodjs/project-config'

export async function buildForStreamingServer({
verbose = false,
}: {
verbose?: boolean
}) {
console.log('Starting streaming server build...\n')
const rwPaths = getPaths()

if (!rwPaths.web.viteConfig) {
throw new Error('Vite config not found')
}

await viteBuild({
configFile: rwPaths.web.viteConfig,
build: {
outDir: rwPaths.web.distServer,
ssr: true,
emptyOutDir: true,
},
legacy: {
// @MARK The Streaming SSR build produces CJS output. RSC is ESM
// TODO: Remove this config once we can build ESM output for streaming
// too
buildSsrCjsExternalHeuristics: true,
},
envFile: false,
logLevel: verbose ? 'info' : 'warn',
})
}

0 comments on commit d04e834

Please sign in to comment.