1- import dotenv from 'dotenv ' ;
1+ import chalk from 'chalk ' ;
22import esbuild from 'esbuild' ;
33import playwright from 'playwright' ;
4- import chalk from 'chalk' ;
5-
6- // Loads environment variables from the '.env' file
7- dotenv . config ( ) ;
8-
9- // TODO implement yargs https://yargs.js.org/
4+ import fs from 'fs' ;
105
11- // https://nodejs.org/docs/latest/api/process.html#process_process_argv
12- // Extract entry (at third parameter) from the executed command
13- // yarn run ./path/to/entry -> './path/to/entry' is extracted
14- const entry = process . argv . slice ( 2 ) [ 0 ] ;
15- const isDev =
16- process . argv . slice ( 2 ) [ 1 ] === '--dev' || process . env . DEV === 'true' ;
17- if ( entry == null ) {
18- throw new Error (
19- "No valid entry was provided! Valid entry example: 'yarn run ./benchmarks/react/counter'"
20- ) ;
21- }
22-
23- const startSpeedBench = async ( ) => {
6+ export const startSpeedBench = async ( entry : string , isDev : boolean ) => {
247 console . log ( chalk . blue ( 'Starting the speed benchmark server..\n' ) ) ;
258
269 // Bundle Benchmark Test Suite
@@ -116,7 +99,7 @@ const startSpeedBench = async () => {
11699 server . stop ( ) ;
117100} ;
118101
119- const startBundleBench = async ( ) => {
102+ export const startBundleBench = async ( entry : string , isDev : boolean ) => {
120103 const bundle = await esbuild . build ( {
121104 inject : [ './lodash.ts' ] , // https://esbuild.github.io/api/#inject
122105 entryPoints : [ entry ] , // https://esbuild.github.io/api/#entry-points
@@ -130,12 +113,38 @@ const startBundleBench = async () => {
130113 metafile : true , // https://esbuild.github.io/api/#metafile
131114 } ) ;
132115
116+ console . log (
117+ `${ chalk . blue ( '[i]' ) } ${ chalk . gray (
118+ `Entry was ${ chalk . green ( `successfully` ) } bundled`
119+ ) } `
120+ ) ;
121+
122+ if ( isDev ) {
123+ console . log (
124+ `${ chalk . blue ( '[i]' ) } ${ chalk . gray (
125+ `Development mode is ${ chalk . green ( `active` ) } `
126+ ) } `
127+ ) ;
128+ }
129+
133130 // Extract metafile from bundle (https://esbuild.github.io/api/#metafile)
134131 const metafile = bundle . metafile ;
135132
133+ // Calculate bundle file size
134+ let bundleSize = 0 ;
135+ bundle . outputFiles ?. map ( ( file ) => {
136+ const stats = fs . statSync ( file . path ) ;
137+ const fileSizeInBytes = stats . size ;
138+ const fileSizeInKilobytes = fileSizeInBytes / 1024 ;
139+ bundleSize += fileSizeInKilobytes ;
140+ } ) ;
141+
142+ console . log (
143+ `${ chalk . blue ( '[i]' ) } ${ chalk . gray (
144+ `Total bundle size of the bundle is ${ chalk . blueBright . bold ( bundleSize ) } `
145+ ) } `
146+ ) ;
147+
136148 console . log ( metafile ) ;
137149 // TODO analyze metafile
138150} ;
139-
140- // Execute the Benchmark
141- startSpeedBench ( ) ;
0 commit comments