11import { promises as fsPromise } from "fs" ;
2- import { ListrContext , ListrTaskWrapper } from "listr2" ;
32import { join } from "path" ;
43
54import { SizeReportContext } from "../index" ;
65import { PackageContext } from "../load-test-scope" ;
7- import { getEsbuildSize , getRollupSize , getWebpackSize } from "./bundlers-size" ;
8- import { generateProject } from "./generate-project" ;
9- import { calculateNpmSize } from "./npm-size" ;
6+ import { getEsbuildSize , getRollupSize , getWebpackSize } from "./bundlers-size.js " ;
7+ import { generateProject } from "./generate-project.js " ;
8+ import { calculateNpmSize } from "./npm-size.js " ;
109
1110export interface PackageSizeReportOptions extends SizeReportContext {
1211 packageName : string ;
@@ -23,48 +22,48 @@ export interface PackageSizeReportOutput {
2322 rollupSize : number | undefined ;
2423}
2524
26- export const getPackageSizeReportRunner =
27- ( options : PackageSizeReportOptions ) => async ( context : ListrContext , task : ListrTaskWrapper < ListrContext , any > ) => {
28- try {
29- task . output = " preparing..." ;
30- const projectDir = join ( options . tmpDir , options . packageName . replace ( "/" , "_" ) ) ;
31- await fsPromise . mkdir ( projectDir ) ;
32- const entryPoint = join ( projectDir , "index.js" ) ;
33- const bundlersContext = { ...options , entryPoint, projectDir } ;
25+ export const generatePackageSizeReport = async ( options : PackageSizeReportOptions ) => {
26+ const { packageName } = options ;
27+ try {
28+ console . info ( `[ ${ packageName } ] preparing...` ) ;
29+ const projectDir = join ( options . tmpDir , options . packageName . replace ( "/" , "_" ) ) ;
30+ await fsPromise . mkdir ( projectDir ) ;
31+ const entryPoint = join ( projectDir , "index.js" ) ;
32+ const bundlersContext = { ...options , entryPoint, projectDir } ;
3433
35- task . output = " generating project and installing dependencies" ;
36- await generateProject ( projectDir , options ) ;
34+ console . info ( `[ ${ packageName } ] generating project and installing dependencies` ) ;
35+ await generateProject ( projectDir , options ) ;
3736
38- task . output = " calculating npm size" ;
39- const npmSizeResult = calculateNpmSize ( projectDir , options . packageName ) ;
37+ console . info ( `[ ${ packageName } ] calculating npm size` ) ;
38+ const npmSizeResult = calculateNpmSize ( projectDir , options . packageName ) ;
4039
41- const skipBundlerTests = bundlersContext . packageContext . skipBundlerTests ;
40+ const skipBundlerTests = bundlersContext . packageContext . skipBundlerTests ;
4241
43- task . output = " calculating webpack 5 full bundle size" ;
44- const webpackSize = skipBundlerTests ? undefined : await getWebpackSize ( bundlersContext ) ;
42+ console . info ( `[ ${ packageName } ] calculating webpack 5 full bundle size` ) ;
43+ const webpackSize = skipBundlerTests ? undefined : await getWebpackSize ( bundlersContext ) ;
4544
46- task . output = " calculating rollup full bundle size" ;
47- const rollupSize = skipBundlerTests ? undefined : await getRollupSize ( bundlersContext ) ;
45+ console . info ( `[ ${ packageName } ] calculating rollup full bundle size` ) ;
46+ const rollupSize = skipBundlerTests ? undefined : await getRollupSize ( bundlersContext ) ;
4847
49- task . output = " calculating esbuild full bundle size" ;
50- const esbuildSize = skipBundlerTests ? undefined : await getEsbuildSize ( bundlersContext ) ;
48+ console . info ( `[ ${ packageName } ] calculating esbuild full bundle size` ) ;
49+ const esbuildSize = skipBundlerTests ? undefined : await getEsbuildSize ( bundlersContext ) ;
5150
52- task . output = " output results" ;
53- const packageVersion = JSON . parse (
54- await fsPromise . readFile (
55- join ( options . workspacePackages . filter ( ( pkg ) => pkg . name === options . packageName ) [ 0 ] . location , "package.json" ) ,
56- "utf8"
57- )
58- ) . version ;
59- options . output . push ( {
60- name : options . packageName ,
61- version : packageVersion ,
62- ...npmSizeResult ,
63- webpackSize,
64- esbuildSize,
65- rollupSize,
66- } ) ;
67- } catch ( e ) {
68- e . message = `[${ options . packageName } ]` + e . message ;
69- }
70- } ;
51+ console . info ( `[ ${ packageName } ] output results` ) ;
52+ const packageVersion = JSON . parse (
53+ await fsPromise . readFile (
54+ join ( options . workspacePackages . filter ( ( pkg ) => pkg . name === options . packageName ) [ 0 ] . location , "package.json" ) ,
55+ "utf8"
56+ )
57+ ) . version ;
58+ options . output . push ( {
59+ name : options . packageName ,
60+ version : packageVersion ,
61+ ...npmSizeResult ,
62+ webpackSize,
63+ esbuildSize,
64+ rollupSize,
65+ } ) ;
66+ } catch ( e ) {
67+ e . message = `[${ options . packageName } ] ` + e . message ;
68+ }
69+ } ;
0 commit comments