@@ -32,6 +32,7 @@ export const startDevServer2 = async (options: StartDevServerOptions) => {
3232 const devBundlePath = path . join ( tempDir , "dev-bundle.js" )
3333
3434 let server : http . Server | null = null
35+ let buildContext : esbuild . BuildContext | null = null
3536 const mutableHttpHandler = {
3637 current : ( _req , res ) => {
3738 // This handler is used before the first build or if a build fails to produce a handler.
@@ -42,10 +43,6 @@ export const startDevServer2 = async (options: StartDevServerOptions) => {
4243
4344 const ignore = await isGitIgnored ( { cwd : rootDirectory } )
4445
45- let isBuilding = false
46- let rebuildScheduled = false
47- let manifestNeedsUpdate = true
48-
4946 const updateManifest = async ( ) => {
5047 const manifestContent = await constructManifest ( {
5148 routesDirectory : config . routesDirectory ,
@@ -55,25 +52,28 @@ export const startDevServer2 = async (options: StartDevServerOptions) => {
5552 await fs . writeFile ( manifestPath , manifestContent , "utf-8" )
5653 }
5754
58- const runSingleBuild = async ( ) => {
55+ const build = async ( ) => {
5956 options . onBuildStart ?.( )
57+ const buildStartedAt = performance . now ( )
58+
6059 try {
61- if ( manifestNeedsUpdate ) {
60+ if ( ! buildContext ) {
6261 await updateManifest ( )
63- manifestNeedsUpdate = false
62+ buildContext = await esbuild . context ( {
63+ entryPoints : [ manifestPath ] ,
64+ bundle : true ,
65+ platform : config . platform === "wintercg-minimal" ? "browser" : "node" ,
66+ packages : config . platform === "node" ? "external" : undefined ,
67+ format : config . platform === "wintercg-minimal" ? "cjs" : "esm" ,
68+ outfile : devBundlePath ,
69+ write : true ,
70+ sourcemap : "inline" ,
71+ logLevel : "silent" ,
72+ } )
6473 }
6574
66- const result = await esbuild . build ( {
67- entryPoints : [ manifestPath ] ,
68- bundle : true ,
69- platform : config . platform === "wintercg-minimal" ? "browser" : "node" ,
70- packages : config . platform === "node" ? "external" : undefined ,
71- format : config . platform === "wintercg-minimal" ? "cjs" : "esm" ,
72- outfile : devBundlePath ,
73- write : true ,
74- sourcemap : "inline" ,
75- logLevel : "silent" ,
76- } )
75+ const result = await buildContext . rebuild ( )
76+ const durationMs = performance . now ( ) - buildStartedAt
7777
7878 if ( result . errors . length === 0 ) {
7979 options . onBuildEnd ?.( {
@@ -190,27 +190,9 @@ export const startDevServer2 = async (options: StartDevServerOptions) => {
190190 errorMessage : err . message ,
191191 buildUpdatedAtMs : Date . now ( ) ,
192192 } )
193- // If the manifest update failed, try again on the next build.
194- manifestNeedsUpdate = true
195193 }
196194 }
197195
198- const build = async ( ) => {
199- if ( isBuilding ) {
200- rebuildScheduled = true
201- return
202- }
203-
204- isBuilding = true
205-
206- do {
207- rebuildScheduled = false
208- await runSingleBuild ( )
209- } while ( rebuildScheduled )
210-
211- isBuilding = false
212- }
213-
214196 const watcher = new Watcher ( rootDirectory , {
215197 recursive : true ,
216198 ignoreInitial : false , // Build on initial scan
@@ -228,7 +210,7 @@ export const startDevServer2 = async (options: StartDevServerOptions) => {
228210
229211 const handleFileChange = async ( isManifestChange : boolean = false ) => {
230212 if ( isManifestChange ) {
231- manifestNeedsUpdate = true
213+ await updateManifest ( )
232214 }
233215 await build ( )
234216 }
@@ -247,15 +229,19 @@ export const startDevServer2 = async (options: StartDevServerOptions) => {
247229 await handleFileChange ( true )
248230 } )
249231
250- // Initial build is triggered immediately.
232+ // Initial build is triggered by watcher's ignoreInitial: false
233+ // If ignoreInitial were true, you'd call:
234+ await updateManifest ( )
251235 await build ( )
252236
253237 const stop = async ( ) => {
254238 watcher . close ( )
255239 if ( server ) {
256240 await new Promise < void > ( ( resolve ) => server ! . close ( ( ) => resolve ( ) ) )
257241 }
258- esbuild . stop ( )
242+ if ( buildContext ) {
243+ await buildContext . dispose ( )
244+ }
259245 }
260246
261247 process . on ( "SIGINT" , async ( ) => {
0 commit comments