Skip to content

Commit

Permalink
chore(vite): Rename 'vite' to more descriptive 'viteSsrDevServer'
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe committed Nov 20, 2024
1 parent 86e17f0 commit c237bec
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 30 deletions.
32 changes: 19 additions & 13 deletions packages/vite/src/devFeServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ async function createServer() {
// Create Vite server in middleware mode and configure the app type as
// 'custom', disabling Vite's own HTML serving logic so parent server
// can take control
const vite = await createViteServer({
const viteSsrDevServer = await createViteServer({
configFile: rwPaths.web.viteConfig,
envFile: false,
define: {
Expand Down Expand Up @@ -180,7 +180,8 @@ async function createServer() {
appType: 'custom',
})

globalThis.__rwjs__vite_ssr_runtime = await createViteRuntime(vite)
globalThis.__rwjs__vite_ssr_runtime =
await createViteRuntime(viteSsrDevServer)
globalThis.__rwjs__client_references = new Set<string>()

// const clientEntryFileSet = new Set<string>()
Expand Down Expand Up @@ -355,7 +356,7 @@ async function createServer() {
const handleWithMiddleware = (route?: RouteSpec) => {
return createServerAdapter(async (req: Request) => {
// Recreate middleware router on each request in dev
const middlewareRouter = await createMiddlewareRouter(vite)
const middlewareRouter = await createMiddlewareRouter(viteSsrDevServer)
const middleware = middlewareRouter.find(
req.method as HTTPMethod,
req.url,
Expand All @@ -367,15 +368,15 @@ async function createServer() {

const [mwRes] = await invoke(req, middleware, {
route,
viteDevServer: vite,
viteSsrDevServer,
})

return mwRes.toResponse()
})
}

// use vite's connect instance as middleware
app.use(vite.middlewares)
app.use(viteSsrDevServer.middlewares)

if (rscEnabled) {
createWebSocketServer()
Expand All @@ -389,8 +390,9 @@ async function createServer() {
app.use(
'/rw-rsc',
await createRscRequestHandler({
getMiddlewareRouter: async () => createMiddlewareRouter(vite),
viteDevServer: vite,
getMiddlewareRouter: async () =>
createMiddlewareRouter(viteSsrDevServer),
viteSsrDevServer,
}),
)
}
Expand All @@ -403,12 +405,16 @@ async function createServer() {
clientEntryPath: rwPaths.web.entryClient,
getStylesheetLinks: (route) => {
// In dev route is a RouteSpec, with additional properties
return getCssLinks({ rwPaths, route: route as RouteSpec, vite })
return getCssLinks({
rwPaths,
route: route as RouteSpec,
viteSsrDevServer,
})
},
// Recreate middleware router on each request in dev
getMiddlewareRouter: async () => createMiddlewareRouter(vite),
getMiddlewareRouter: async () => createMiddlewareRouter(viteSsrDevServer),
},
vite,
viteSsrDevServer,
)

app.get('*', createServerAdapter(routeHandler))
Expand Down Expand Up @@ -442,15 +448,15 @@ process.stdin.on('data', async (data) => {
function getCssLinks({
rwPaths,
route,
vite,
viteSsrDevServer,
}: {
rwPaths: Paths
route?: RouteSpec
vite: ViteDevServer
viteSsrDevServer: ViteDevServer
}) {
const appAndRouteModules = componentsModules(
[rwPaths.web.app, route?.filePath].filter(Boolean) as string[],
vite,
viteSsrDevServer,
)

const collectedCss = collectCssPaths(appAndRouteModules)
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/rsc/rscRequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const BASE_PATH = '/rw-rsc/'

interface CreateRscRequestHandlerOptions {
getMiddlewareRouter: () => Promise<Router.Instance<any>>
viteDevServer?: ViteDevServer
viteSsrDevServer?: ViteDevServer
}

export async function createRscRequestHandler(
Expand Down Expand Up @@ -59,7 +59,7 @@ export async function createRscRequestHandler(
matchedMw?.handler as Middleware | undefined,
{
params: matchedMw?.params,
viteDevServer: options.viteDevServer,
viteSsrDevServer: options.viteSsrDevServer,
},
)

Expand Down
22 changes: 12 additions & 10 deletions packages/vite/src/streaming/createReactStreamingHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ export const createReactStreamingHandler = async (
getStylesheetLinks,
getMiddlewareRouter,
}: CreateReactStreamingHandlerOptions,
viteDevServer?: ViteDevServer,
viteSsrDevServer?: ViteDevServer,
) => {
const rwPaths = getPaths()
const rwConfig = getConfig()
const isProd = !viteDevServer
const isProd = !viteSsrDevServer
const middlewareRouter: Router.Instance<any> = await getMiddlewareRouter()
let entryServerImport: EntryServer
let fallbackDocumentImport: Record<string, any>
Expand Down Expand Up @@ -115,7 +115,7 @@ export const createReactStreamingHandler = async (
route: currentRoute,
cssPaths: cssLinks,
params: matchedMw?.params,
viteDevServer,
viteSsrDevServer,
},
)

Expand Down Expand Up @@ -144,8 +144,8 @@ export const createReactStreamingHandler = async (
// Do this inside the handler for **dev-only**.
// This makes sure that changes to entry-server are picked up on refresh
if (!isProd) {
entryServerImport = await ssrLoadEntryServer(viteDevServer)
fallbackDocumentImport = await viteDevServer.ssrLoadModule(
entryServerImport = await ssrLoadEntryServer(viteSsrDevServer)
fallbackDocumentImport = await viteSsrDevServer.ssrLoadModule(
rwPaths.web.document,
)
}
Expand Down Expand Up @@ -173,13 +173,15 @@ export const createReactStreamingHandler = async (
req,
parsedParams,
},
viteDevServer,
viteSsrDevServer,
})

metaTags = routeHookOutput.meta

// On dev, we don't need to add the slash (for windows support) any more
const jsBundles = [viteDevServer ? clientEntryPath : '/' + clientEntryPath]
const jsBundles = [
viteSsrDevServer ? clientEntryPath : '/' + clientEntryPath,
]
if (currentRoute.bundle) {
jsBundles.push('/' + currentRoute.bundle)
}
Expand All @@ -201,14 +203,14 @@ export const createReactStreamingHandler = async (
{
waitForAllReady: isSeoCrawler,
onError: (err) => {
if (!isProd && viteDevServer) {
viteDevServer.ssrFixStacktrace(err)
if (!isProd && viteSsrDevServer) {
viteSsrDevServer.ssrFixStacktrace(err)
}

console.error(err)
},
},
viteDevServer,
viteSsrDevServer,
)

return reactResponse
Expand Down
10 changes: 6 additions & 4 deletions packages/vite/src/streaming/triggerRouteHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ interface LoadAndRunRouteHooks {
req: Request
parsedParams?: Record<string, any>
}
viteDevServer?: ViteDevServer
viteSsrDevServer?: ViteDevServer
previousOutput?: RouteHookOutput
}

Expand All @@ -71,12 +71,14 @@ const defaultRouteHookOutput = {
export const loadAndRunRouteHooks = async ({
paths = [],
reqMeta,
viteDevServer,
viteSsrDevServer,
previousOutput = defaultRouteHookOutput,
}: LoadAndRunRouteHooks): Promise<RouteHookOutput> => {
// Step 1, load the route hooks
const loadModule = async (path: string) => {
return viteDevServer ? viteDevServer.ssrLoadModule(path) : import(path)
return viteSsrDevServer
? viteSsrDevServer.ssrLoadModule(path)
: import(path)
}

let currentRouteHooks: RouteHooks
Expand Down Expand Up @@ -105,7 +107,7 @@ export const loadAndRunRouteHooks = async ({
paths,
reqMeta,
previousOutput: rhOutput,
viteDevServer,
viteSsrDevServer,
})
} else {
return rhOutput
Expand Down
2 changes: 1 addition & 1 deletion packages/web/src/server/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type MiddlewareInvokeOptions = {
route?: RWRouteManifestItem
cssPaths?: string[]
params?: Record<string, unknown>
viteDevServer?: ViteDevServer
viteSsrDevServer?: ViteDevServer
}

export type Middleware = (
Expand Down

0 comments on commit c237bec

Please sign in to comment.