66// anomalyco/textjs (MIT), which isn't published to npm; it is downloaded at the pinned commit into
77// .upstream/. If the download fails the build still writes the picker, and try() falls back to a
88// link to textjs.org/opencode.
9+ import { createHash } from "node:crypto"
910import { mkdir , rm } from "node:fs/promises"
1011import path from "node:path"
1112
@@ -14,6 +15,13 @@ const root = path.resolve(import.meta.dir, "..")
1415const upstream = path . join ( root , ".upstream" )
1516const out = path . resolve ( root , "../public/textjs" )
1617
18+ // Text.js 1.6 loads Yoga from a .wasm file and the bidi engine from its own chunk under the
19+ // `@textjs/client` resolve condition, which `text build` sets. Bun's bundler leaves the loader's
20+ // `new URL("@textjs/core/yoga.wasm", import.meta.url)` alone, so the entries load the file
21+ // themselves (`initYoga({ wasm })`) from a name hashed by its contents, defined into them below.
22+ const yoga = await Bun . file ( new URL ( import . meta. resolve ( "@textjs/core/yoga.wasm" ) ) ) . bytes ( )
23+ const yogaName = `yoga-${ createHash ( "sha256" ) . update ( yoga ) . digest ( "hex" ) . slice ( 0 , 8 ) } .wasm`
24+
1725const lite = await fetchUpstream ( ) . then (
1826 ( ) => true ,
1927 ( error ) => {
@@ -29,22 +37,30 @@ const result = await Bun.build({
2937 format : "esm" ,
3038 splitting : true ,
3139 minify : true ,
40+ conditions : [ "@textjs/client" ] ,
3241 naming : { entry : "[name]-[hash].[ext]" , chunk : "chunk-[hash].[ext]" } ,
33- define : { "process.env.NODE_ENV" : '"production"' } ,
42+ define : {
43+ "process.env.NODE_ENV" : '"production"' ,
44+ __TEXTJS_YOGA__ : JSON . stringify ( `./${ yogaName } ` ) ,
45+ } ,
3446} )
3547if ( ! result . success ) throw new AggregateError ( result . logs , "textjs build failed" )
48+ await Bun . write ( path . join ( out , yogaName ) , yoga )
3649const entries = result . outputs . filter ( ( output ) => output . kind === "entry-point" )
3750const manifest = Object . fromEntries (
3851 entries . map ( ( entry ) => [ path . basename ( entry . path ) . split ( "-" ) [ 0 ] , path . basename ( entry . path ) ] ) ,
3952)
4053await Bun . write ( path . join ( out , "manifest.json" ) , JSON . stringify ( manifest ) )
41- // What each app downloads: its entry plus every chunk it imports statically.
54+ // What each app downloads: its entry, every chunk it imports statically, and then the chunks it
55+ // fetches on demand (the bidi engine, the session dialogs), plus Yoga's wasm.
4256const sizes = new Map (
4357 await Promise . all (
4458 result . outputs . map ( async ( output ) => {
45- const text = await Bun . file ( output . path ) . text ( )
59+ const bytes = await Bun . file ( output . path ) . bytes ( )
60+ const text = new TextDecoder ( ) . decode ( bytes )
4661 const imports = [ ...text . matchAll ( / f r o m " \. \/ ( [ \w - ] + \. j s ) " / g) ] . map ( ( match ) => match [ 1 ] )
47- return [ path . basename ( output . path ) , { bytes : text . length , gzip : Bun . gzipSync ( text ) . length , imports } ] as const
62+ const lazy = [ ...text . matchAll ( / i m p o r t \( " \. \/ ( [ \w - ] + \. j s ) " \) / g) ] . map ( ( match ) => match [ 1 ] )
63+ return [ path . basename ( output . path ) , { bytes : bytes . length , gzip : Bun . gzipSync ( text ) . length , imports, lazy } ] as const
4864 } ) ,
4965 ) ,
5066)
@@ -54,11 +70,21 @@ const closure = (file: string, seen = new Set<string>()): Set<string> => {
5470 sizes . get ( file ) ?. imports . forEach ( ( next ) => closure ( next , seen ) )
5571 return seen
5672}
73+ const lazyClosure = ( eager : string [ ] ) : Set < string > => {
74+ const seen = new Set < string > ( )
75+ const known = new Set ( eager )
76+ for ( const file of eager ) sizes . get ( file ) ?. lazy . forEach ( ( next ) => closure ( next , seen ) )
77+ return new Set ( [ ...seen ] . filter ( ( file ) => ! known . has ( file ) ) )
78+ }
5779for ( const [ name , file ] of Object . entries ( manifest ) ) {
5880 const files = [ ...closure ( file ) ]
59- const kb = ( key : "bytes" | "gzip" ) =>
60- ( files . reduce ( ( sum , f ) => sum + ( sizes . get ( f ) ?. [ key ] ?? 0 ) , 0 ) / 1024 ) . toFixed ( 1 )
61- console . log ( `textjs: ${ name } ${ kb ( "bytes" ) } KB, ${ kb ( "gzip" ) } KB gzip (${ files . length } files)` )
81+ const kb = ( key : "bytes" | "gzip" , list : string [ ] ) =>
82+ ( list . reduce ( ( sum , f ) => sum + ( sizes . get ( f ) ?. [ key ] ?? 0 ) , 0 ) / 1024 ) . toFixed ( 1 )
83+ console . log ( `textjs: ${ name } ${ kb ( "bytes" , files ) } KB, ${ kb ( "gzip" , files ) } KB gzip (${ files . length } files)` )
84+ const lazy = [ ...lazyClosure ( files ) ]
85+ if ( lazy . length ) console . log ( `textjs: ${ name } +${ kb ( "gzip" , lazy ) } KB gzip on demand (${ lazy . join ( ", " ) } )` )
86+ const yogaKb = `${ ( yoga . length / 1024 ) . toFixed ( 1 ) } KB, ${ ( Bun . gzipSync ( yoga ) . length / 1024 ) . toFixed ( 1 ) } KB gzip`
87+ console . log ( `textjs: ${ name } +${ yogaKb } yoga (${ yogaName } )` )
6288}
6389
6490async function fetchUpstream ( ) {
0 commit comments