1- #!/usr/bin/env node
21/**
32 * @fileoverview Fixed esbuild script that properly handles React externalization
43 * This version addresses top-level await and import.meta issues
54 */
65
7- import { build } from 'esbuild'
86import { existsSync } from 'node:fs'
9- import { mkdir , rm , writeFile , copyFile } from 'node:fs/promises'
7+ import { mkdir , rm , writeFile } from 'node:fs/promises'
108import path from 'node:path'
11- import { fileURLToPath } from 'node:url'
129import { performance } from 'node:perf_hooks'
10+ import { fileURLToPath } from 'node:url'
11+
12+ import { build } from 'esbuild'
1313
1414const __filename = fileURLToPath ( import . meta. url )
1515const __dirname = path . dirname ( __filename )
@@ -43,7 +43,8 @@ async function buildMainCli() {
4343 { in : 'constants.mts' , out : 'constants' } ,
4444 ]
4545
46- for ( const entry of mainEntries ) {
46+ // Build all entries in parallel
47+ await Promise . all ( mainEntries . map ( async ( entry ) => {
4748 await build ( {
4849 entryPoints : [ path . join ( srcPath , entry . in ) ] ,
4950 bundle : true ,
@@ -66,7 +67,8 @@ async function buildMainCli() {
6667 'yoga-layout' ,
6768 'yoga-wasm-web' ,
6869 'react-devtools-core' ,
69- '../dist/shadow-*' , // Shadow bins will be built separately
70+ // Shadow bins will be built separately
71+ '../dist/shadow-*' ,
7072 ] ,
7173
7274 // Replace import.meta.url with a Node-compatible alternative
@@ -88,7 +90,7 @@ async function buildMainCli() {
8890 } )
8991
9092 console . log ( ` ✅ Built ${ entry . out } .js` )
91- }
93+ } ) )
9294}
9395
9496// Build shadow binaries
@@ -102,13 +104,14 @@ async function buildShadowBins() {
102104 { in : 'shadow/pnpm/bin.mts' , out : 'shadow-pnpm-bin' } ,
103105 ]
104106
105- for ( const entry of shadowEntries ) {
107+ // Build all shadow entries in parallel
108+ await Promise . all ( shadowEntries . map ( async ( entry ) => {
106109 const inputPath = path . join ( srcPath , entry . in )
107110
108111 // Check if file exists
109112 if ( ! existsSync ( inputPath ) ) {
110113 console . log ( ` ⚠️ Skipping ${ entry . out } (file not found)` )
111- continue
114+ return
112115 }
113116
114117 await build ( {
@@ -145,7 +148,7 @@ async function buildShadowBins() {
145148 } )
146149
147150 console . log ( ` ✅ Built ${ entry . out } .js` )
148- }
151+ } ) )
149152}
150153
151154// Build external modules
@@ -157,17 +160,19 @@ async function buildExternals() {
157160 { in : 'external/yoga-layout.mjs' , out : 'external/yoga-layout' } ,
158161 ]
159162
160- for ( const entry of externalEntries ) {
163+ // Build all external entries in parallel
164+ await Promise . all ( externalEntries . map ( async ( entry ) => {
161165 const inputPath = path . join ( srcPath , entry . in )
162166
163167 if ( ! existsSync ( inputPath ) ) {
164168 console . log ( ` ⚠️ Skipping ${ entry . out } (file not found)` )
165- continue
169+ return
166170 }
167171
168172 await build ( {
169173 entryPoints : [ inputPath ] ,
170- bundle : false , // Don't bundle externals
174+ // Don't bundle externals
175+ bundle : false ,
171176 platform : 'node' ,
172177 target : 'node18' ,
173178 format : 'cjs' ,
@@ -181,7 +186,7 @@ async function buildExternals() {
181186 } )
182187
183188 console . log ( ` ✅ Built ${ entry . out } .js` )
184- }
189+ } ) )
185190}
186191
187192// Create inject helper for import.meta compatibility
@@ -211,16 +216,18 @@ async function reportSizes() {
211216 const files = await fs . readdir ( distPath )
212217
213218 let totalSize = 0
214- const sizes = [ ]
215-
216- for ( const file of files ) {
217- if ( file . endsWith ( '.js' ) ) {
218- const stats = await fs . stat ( path . join ( distPath , file ) )
219- const sizeKB = ( stats . size / 1024 ) . toFixed ( 2 )
220- totalSize += stats . size
221- sizes . push ( { file, sizeKB, size : stats . size } )
222- }
223- }
219+
220+ // Get all file stats in parallel
221+ const sizes = await Promise . all (
222+ files
223+ . filter ( file => file . endsWith ( '.js' ) )
224+ . map ( async file => {
225+ const stats = await fs . stat ( path . join ( distPath , file ) )
226+ const sizeKB = ( stats . size / 1024 ) . toFixed ( 2 )
227+ totalSize += stats . size
228+ return { file, sizeKB, size : stats . size }
229+ } )
230+ )
224231
225232 // Sort by size
226233 sizes . sort ( ( a , b ) => b . size - a . size )
@@ -271,7 +278,7 @@ async function main() {
271278
272279 } catch ( error ) {
273280 console . error ( '❌ Build failed:' , error )
274- process . exit ( 1 )
281+ throw error
275282 }
276283}
277284
0 commit comments