From 76d2ab0eb9237cc0f0f863a5b0a500b9a5ad50b7 Mon Sep 17 00:00:00 2001 From: Owen Pearson Date: Wed, 19 Jul 2023 10:58:21 +0100 Subject: [PATCH] ci: display info about sizes of tree-shakable modules --- .github/workflows/bundle-report.yml | 1 + package.json | 1 + scripts/moduleReport.js | 37 +++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 scripts/moduleReport.js diff --git a/.github/workflows/bundle-report.yml b/.github/workflows/bundle-report.yml index 7866bb9dc3..39dda65d14 100644 --- a/.github/workflows/bundle-report.yml +++ b/.github/workflows/bundle-report.yml @@ -37,3 +37,4 @@ jobs: sourcePath: bundle-reports githubToken: ${{ secrets.GITHUB_TOKEN }} artifactName: bundle-report + - run: npm run modulereport diff --git a/package.json b/package.json index 4c62961d7d..a4efcb6679 100644 --- a/package.json +++ b/package.json @@ -96,6 +96,7 @@ "format:check": "prettier --check --ignore-path .gitignore --ignore-path .prettierignore src test ably.d.ts webpack.config.js Gruntfile.js scripts/cdn_deploy.js", "sourcemap": "source-map-explorer build/ably.min.js", "sourcemap:noencryption": "source-map-explorer build/ably.noencryption.min.js", + "modulereport": "node scripts/moduleReport.js", "docs": "typedoc --entryPoints ably.d.ts --out docs/generated --readme docs/landing-page.md" } } diff --git a/scripts/moduleReport.js b/scripts/moduleReport.js new file mode 100644 index 0000000000..a1d8f70e68 --- /dev/null +++ b/scripts/moduleReport.js @@ -0,0 +1,37 @@ +const esbuild = require('esbuild'); + +// List of all modules accepted in ModulesMap +const moduleNames = ['Rest']; + +function formatBytes(bytes) { + const kb = bytes / 1024; + const formatted = kb.toFixed(2); + return `${formatted}kb`; +} + +// Gets the bundled size of an array of named exports from 'ably/modules' formatted as a string +function getImportSize(modules) { + const outfile = modules.join(''); + const result = esbuild.buildSync({ + stdin: { + contents: `export { ${modules.join(', ')} } from './build/modules'`, + resolveDir: '.', + }, + metafile: true, + minify: true, + bundle: true, + outfile, + write: false, + }); + + return formatBytes(result.metafile.outputs[outfile].bytes); +} + +// First display the size of the BaseClient +console.log(`BaseClient: ${getImportSize(['BaseClient'])}`); + +// Then display the size of each module together with the BaseClient +moduleNames.forEach((moduleName) => { + const sizeInBytes = getImportSize(['BaseClient', moduleName]); + console.log(`BaseClient + ${moduleName}: ${sizeInBytes}`); +});