import s from"path";import{writeFile as l}from"fs/promises";import{createClientCompiler as P,createServerCompiler as R}from"../server/esbuild/esbuild.js";import{initPlugins as A,runPlugins as E}from"../server/plugins/lifecycle.js";import{renderPage as O,getServerProps as W}from"../server/ssr/index.js";import{promiseMapLimit as _,ensureDir as m,logger as i}from"../server/utils/index.js";import{getPageDataUrl as j,GLOBAL_DATA_URL as G}from"../shared/urls.js";import{exit as I}from"../server/utils/process.js";import{sendCliCommandExecution as L,telemetry as f}from"./telemetry/index.js";async function z(c,t){i.info("Starting project static build.");const a=s.resolve(c.outdir),w=i.startTiming(),{pluginInstances:T,lifecycleContext:r}=await A({contentDir:t.contentDir,outdir:t.outdir,serverOutDir:"",setGlobalConfig:t.setGlobalConfig});await E(T,t,r),L(c,r,t);const g=await P(t,a,"production"),d=await R(t,t.serverOutDir),h=i.startTiming();await g.rebuild(),i.infoTime(h,"Client compiled");const D=i.startTiming();await d.rebuild(),i.infoTime(D,"Server compiled"),await g.dispose(),await d.dispose(),i.info("Rendering pages...");const C=i.startTiming(),o=t.getAllRoutes();let p=0;try{await _(o,15,async e=>{const n=await t.resolveRouteStaticData(e,r),v=await W(e,null,n,t),{html:S,props:x}=await O(e,v,null,t);await l(m(s.join(a,e.slug,"index.html")),S,"utf-8");const y={templateId:e.templateId,sharedDataIds:t.routesSharedData.get(e.slug)||{},props:x};await l(m(s.join(a,j(e.slug))),JSON.stringify(y),"utf-8"),p++,i.info(`Page ${p}/${o.length} rendered.`)})}catch(e){f.send("cli_error_caught",{message:`Resolving static data failed: ${e.message}`}),i.panic(e.message)}i.successTime(C,`${o.length} pages rendered.`),i.info("Writing out global data...");const b=i.startTiming();await l(m(s.join(a,G)),JSON.stringify(t.globalData),"utf-8"),i.successTime(b,"Global data written.");const u=t.searchEngine;if(u){i.info("Writing out search data...");const e=i.startTiming();await u.export(t.outdir);const n=i.successTime(e,"Search data written.");n&&f.send("timing_performed",n)}await r.fs.dispose(),i.successTime(w,"All done"),I(0)}export{z as build};
