1- /* eslint-disable import/no-dynamic-require */
1+ import child from 'child_process' ;
22import path from 'path' ;
33import zlib from 'zlib' ;
44import { encode } from 'base16384' ;
@@ -10,9 +10,12 @@ const logger = new Logger('build');
1010logger . info ( 'Building...' ) ;
1111
1212function encodeBinary ( a : Buffer ) {
13- const file = zlib . gzipSync ( a ) ;
13+ const file = zlib . gzipSync ( a , { level : 9 } ) ;
1414 return chunk ( [ ...encode ( file ) ] , 1000 ) . map ( ( i ) => String . fromCodePoint ( ...i ) ) . join ( '' ) ;
1515}
16+ function size ( a : string ) {
17+ return `${ Math . floor ( ( Buffer . from ( a ) . length / 1024 / 1024 ) * 10 ) / 10 } MB` ;
18+ }
1619
1720const nopMap = '//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIiJdLCJtYXBwaW5ncyI6IkEifQ==' ;
1821
@@ -24,23 +27,27 @@ const nopMap = '//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIj
2427 outdir : path . join ( process . cwd ( ) , 'dist' ) ,
2528 splitting : false ,
2629 write : false ,
30+ target : 'node16' ,
2731 tsconfig : path . resolve ( process . cwd ( ) , 'tsconfig.json' ) ,
28- minify : true ,
32+ minify : ! process . argv . includes ( '--debug' ) ,
2933 entryPoints : [ path . resolve ( process . cwd ( ) , 'packages/server/index.ts' ) ] ,
3034 charset : 'utf8' ,
31- sourcemap : 'inline' ,
35+ sourcemap : process . argv . includes ( '--debug' ) ? 'inline' : false ,
36+ metafile : true ,
3237 plugins : [ {
3338 name : 'base16384' ,
3439 setup ( b ) {
3540 b . onLoad ( { filter : / \. ( f r o n t e n d | t t f | w a s m ) $ / , namespace : 'file' } , ( t ) => {
3641 const file = fs . readFileSync ( path . join ( t . path ) ) ;
42+ const contents = `module.exports = "${ process . argv . includes ( '--no-binary' ) ? '' : encodeBinary ( file ) } ";\n${ nopMap } ` ;
43+ console . log ( t . path , size ( contents ) ) ;
3744 return {
38- contents : `module.exports = " ${ encodeBinary ( file ) } ";\n ${ nopMap } ` ,
45+ contents,
3946 loader : 'tsx' ,
4047 } ;
4148 } ) ;
42- b . onLoad ( { filter : / n o d e _ m o d u l e s \/ . + \. j s $ / } , ( t ) => ( {
43- contents : `${ fs . readFileSync ( t . path , 'utf8 ' ) } \n${ nopMap } ` ,
49+ b . onLoad ( { filter : / n o d e _ m o d u l e s . * \. [ j t ] s x ? $ / } , async ( t ) => ( {
50+ contents : `${ await fs . readFile ( t . path , 'utf-8 ' ) } \n${ nopMap } ` ,
4451 loader : 'default' ,
4552 } ) ) ;
4653 } ,
@@ -52,7 +59,19 @@ const nopMap = '//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIj
5259 } ) ;
5360 if ( res . errors . length ) console . error ( res . errors ) ;
5461 if ( res . warnings . length ) console . warn ( res . warnings ) ;
55- logger . info ( `Resource Size: ${ Math . floor ( ( res . outputFiles [ 0 ] . text . length / 1024 / 1024 ) * 10 ) / 10 } MB ` ) ;
62+ logger . info ( `Resource Size: ${ size ( res . outputFiles [ 0 ] . text ) } ` ) ;
5663 fs . writeFileSync ( path . resolve ( process . cwd ( ) , 'dist/xcpc-tools.js' ) , res . outputFiles [ 0 ] . text ) ;
64+ fs . writeFileSync ( path . resolve ( process . cwd ( ) , 'dist/metafile.json' ) , JSON . stringify ( res . metafile ) ) ;
5765 logger . info ( 'Saved to dist/xcpc-tools.js' ) ;
66+ if ( ! process . env . SEA ) return ;
67+ fs . writeFileSync ( path . resolve ( process . cwd ( ) , 'dist/sea-config.json' ) , JSON . stringify ( {
68+ main : 'xcpc-tools.js' ,
69+ output : 'sea-prep.blob' ,
70+ } ) ) ;
71+ child . execSync ( 'node --experimental-sea-config sea-config.json' , { cwd : path . resolve ( process . cwd ( ) , 'dist' ) } ) ;
72+ fs . copyFileSync ( path . resolve ( process . cwd ( ) , 'nanode-v22.x-icu_none-v8_opts-lto-x64' ) , path . resolve ( process . cwd ( ) , 'dist/nanode' ) ) ;
73+ child . execSync (
74+ 'npx postject nanode NODE_SEA_BLOB sea-prep.blob --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2' ,
75+ { cwd : path . resolve ( process . cwd ( ) , 'dist' ) } ,
76+ ) ;
5877} ) ( ) ;
0 commit comments