Skip to content

Commit

Permalink
ci: display info about sizes of tree-shakable modules
Browse files Browse the repository at this point in the history
  • Loading branch information
owenpearson committed Aug 1, 2023
1 parent ade44d6 commit d232195
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/bundle-report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ jobs:
sourcePath: bundle-reports
githubToken: ${{ secrets.GITHUB_TOKEN }}
artifactName: bundle-report
- run: npm run modulereport
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
37 changes: 37 additions & 0 deletions scripts/moduleReport.js
Original file line number Diff line number Diff line change
@@ -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}`);
});

0 comments on commit d232195

Please sign in to comment.