11import { mkdir , readFile , rename , rm , writeFile } from "node:fs/promises" ;
22import path from "node:path" ;
33import { build as esbuild } from "esbuild" ;
4- import { createBuilder } from "vite" ;
4+ import { createBuilder , type Rolldown } from "vite" ;
55import { ZuploEnv } from "../app/env.js" ;
66import { getZudokuRootDir } from "../cli/common/package-json.js" ;
7- import {
8- findOutputPathOfServerConfig ,
9- loadZudokuConfig ,
10- } from "../config/loader.js" ;
7+ import { type ConfigWithMeta , loadZudokuConfig } from "../config/loader.js" ;
118import { getIssuer } from "../lib/auth/issuer.js" ;
129import invariant from "../lib/util/invariant.js" ;
1310import { joinUrl } from "../lib/util/joinUrl.js" ;
@@ -37,6 +34,9 @@ export async function runBuild(options: BuildOptions) {
3734 invariant ( builder . environments . client , "Client environment is missing" ) ;
3835 invariant ( builder . environments . ssr , "SSR environment is missing" ) ;
3936
37+ const distDir = path . resolve ( path . join ( dir , "dist" ) ) ;
38+ await rm ( distDir , { recursive : true , force : true } ) ;
39+
4040 const [ clientResult , serverResult ] = await Promise . all ( [
4141 builder . build ( builder . environments . client ) ,
4242 builder . build ( builder . environments . ssr ) ,
@@ -46,7 +46,11 @@ export async function runBuild(options: BuildOptions) {
4646 clientResult && ! Array . isArray ( clientResult ) && "output" in clientResult ,
4747 "Client build failed to produce valid output" ,
4848 ) ;
49- invariant ( serverResult , "SSR build failed to produce valid output" ) ;
49+
50+ invariant (
51+ serverResult && ! Array . isArray ( serverResult ) && "output" in serverResult ,
52+ "SSR build failed to produce valid output" ,
53+ ) ;
5054
5155 const { config } = await loadZudokuConfig (
5256 { mode : "production" , command : "build" } ,
@@ -102,18 +106,27 @@ export async function runBuild(options: BuildOptions) {
102106
103107type PrerenderOptions = {
104108 dir : string ;
105- config : Awaited < ReturnType < typeof loadZudokuConfig > > [ "config" ] ;
109+ config : ConfigWithMeta ;
106110 html : string ;
107111 clientOutDir : string ;
108112 serverOutDir : string ;
109- serverResult : Awaited < ReturnType < typeof import ( "vite" ) . build > > ;
113+ serverResult : Rolldown . RolldownOutput ;
114+ } ;
115+
116+ const findServerConfigFilename = ( result : Rolldown . RolldownOutput ) => {
117+ const entry = result . output . find (
118+ ( o ) => o . type === "chunk" && o . isEntry && o . fileName === "zudoku.config.js" ,
119+ ) ;
120+ invariant ( entry , "Could not find zudoku.config entry in server build output" ) ;
121+
122+ return entry . fileName ;
110123} ;
111124
112125const runPrerender = async ( options : PrerenderOptions ) => {
113126 const { dir, config, html, clientOutDir, serverOutDir, serverResult } =
114127 options ;
115128 const issuer = await getIssuer ( config ) ;
116- const serverConfigFilename = findOutputPathOfServerConfig ( serverResult ) ;
129+ const serverConfigFilename = findServerConfigFilename ( serverResult ) ;
117130
118131 try {
119132 const { workerResults, rewrites } = await prerender ( {
0 commit comments