@@ -10,6 +10,7 @@ const { minify } = require('terser');
1010const zlib = require ( 'node:zlib' ) ;
1111const { init : initEsmLexer , parse } = require ( 'es-module-lexer' ) ;
1212const MagicString = require ( 'magic-string' ) ;
13+ const { platform } = require ( 'node:os' ) ;
1314
1415/**
1516 * Transform ESM to CJS using destructured imports
@@ -223,18 +224,28 @@ async function main() {
223224 compress : {
224225 ...mangleConfig . minify . compress ,
225226 pure_getters : true ,
227+ // For some reason this is needed else
228+ // the var declarations will come before
229+ // the imports
226230 hoist_vars : false ,
227- hoist_funs : false ,
231+ inline : 3 ,
232+ sequences : 100 ,
228233 keep_infinity : true ,
234+ reduce_vars : true ,
235+ reduce_funcs : false ,
236+ collapse_vars : true ,
237+ side_effects : true ,
229238 unsafe_proto : true ,
230239 passes : 10 ,
231- toplevel : true
240+ ecma : 2020 ,
241+ module : true
232242 } ,
233243 mangle : {
234- toplevel : true ,
235244 properties : { ...mangleConfig . minify . mangle . properties , reserved }
236245 } ,
237246 format : {
247+ ascii_only : true ,
248+ wrap_iife : false ,
238249 shorthand : true ,
239250 wrap_func_args : false ,
240251 comments : / ^ \s * ( [ @ # ] _ _ [ A - Z ] + _ _ \s * $ | @ c c _ o n ) / ,
@@ -292,12 +303,15 @@ async function main() {
292303 bundle : true ,
293304 sourcemap : true ,
294305 sourcesContent : true ,
306+ treeShaking : true ,
307+ platform : 'browser' ,
308+ jsxSideEffects : false ,
295309 plugins : [ babelRenamePlugin ( ) ] ,
296310 target : [ 'es2020' ] ,
297311 define : { 'process.env.NODE_ENV' : '"production"' }
298312 } ;
299313
300- // Build ESM first
314+ // @ts -expect-error
301315 await build ( {
302316 ...shared ,
303317 format : 'esm' ,
@@ -333,7 +347,7 @@ async function main() {
333347 params : { [ zlib . constants . BROTLI_PARAM_QUALITY ] : 11 }
334348 } ) . length ;
335349 sizeRows . push ( {
336- pkg : pkg . id ,
350+ pkg : pkg . id + ( ext === '.mjs' ? ' (esm)' : '(cjs)' ) ,
337351 file : path . relative ( root , abs ) ,
338352 raw,
339353 gz,
@@ -343,9 +357,26 @@ async function main() {
343357 }
344358
345359 console . log ( '\n[build] Artifact sizes (bytes):' ) ;
346- console . log ( [ 'Package' , 'File' , 'Raw' , 'Gzip' , 'Brotli' ] . join ( '\t' ) ) ;
347- for ( const row of sizeRows ) {
348- console . log ( [ row . pkg , row . file , row . raw , row . gz , row . br ] . join ( '\t' ) ) ;
360+
361+ const headers = [ 'Package' , 'Raw' , 'Gzip' , 'Brotli' ] ;
362+ const rows = sizeRows . map ( r => [
363+ r . pkg ,
364+ String ( r . raw ) ,
365+ String ( r . gz ) ,
366+ String ( r . br )
367+ ] ) ;
368+ const colWidths = headers . map ( ( h , i ) =>
369+ Math . max ( h . length , ...rows . map ( r => r [ i ] . length ) )
370+ ) ;
371+
372+ function pad ( v , i ) {
373+ const w = colWidths [ i ] ;
374+ return v + ' ' . repeat ( w - v . length ) ;
375+ }
376+
377+ console . log ( headers . map ( pad ) . join ( ' ' ) ) ;
378+ for ( const r of rows ) {
379+ console . log ( r . map ( pad ) . join ( ' ' ) ) ;
349380 }
350381 console . log ( '\nDone.' ) ;
351382}
0 commit comments