Add a workflow to print bundle size #9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Bundle size | |
on: | |
pull_request: | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.ref }} | |
cancel-in-progress: true | |
jobs: | |
build: | |
name: Build on the base branch and the PR branch | |
strategy: | |
matrix: | |
branch: | |
- ${{ github.base_ref }} | |
- ${{ github.head_ref }} | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
ref: ${{ matrix.branch }} | |
- name: Setup | |
uses: ./.github/actions/setup | |
- name: Build the packages | |
run: turbo run build | |
- name: Collect sizes in bytes | |
id: sizes | |
run: du -sb packages/*/dist > sizes-${{ matrix.branch }}.txt | |
- name: Upload the sizes | |
uses: actions/upload-artifact@v4 | |
with: | |
name: sizes-${{ matrix.branch }} | |
path: sizes-${{ matrix.branch }}.txt | |
print: | |
name: Print the sizes | |
runs-on: ubuntu-latest | |
needs: build | |
steps: | |
- name: Download the sizes | |
uses: actions/download-artifact@v4 | |
with: | |
merge-multiple: true | |
path: '.' | |
- name: Create the report | |
uses: actions/github-script@v7 | |
with: | |
result-encoding: string | |
script: | | |
function parseDuOutput(output) { | |
return Object.fromEntries(output.trim().split('\n').map(line => { | |
const [size, folderName] = line.split(/\s+/); | |
const [_packages, packageName, _dist] = folderName.split('/'); | |
return [packageName, parseInt(size, 10)]; | |
})); | |
} | |
function formatSize(sizeInBytes) { | |
if (sizeInBytes < 1000) { | |
return `${sizeInBytes} B`; | |
} else { | |
const sizeInKb = sizeInBytes / 1000; | |
return `${sizeInKb.toFixed(2)} KB`; | |
} | |
} | |
const fs = require('fs'); | |
const sizes = parseDuOutput(fs.readFileSync(`sizes-${${{ toJson(github.base_ref) }}}.txt`, 'utf8')); | |
const sizesPR = parseDuOutput(fs.readFileSync(`sizes-${${{ toJson(github.head_ref) }}}.txt`, 'utf8')); | |
core.summary.addHeading('📊 Package size report', '3'); | |
core.summary.addTable([ | |
...(['Package', 'Before', 'After'].map((data) => ({ data, header: true }))), | |
...Object.keys(sizes).flatMap((packageName) => { | |
const size = sizes[packageName]; | |
const sizePR = sizesPR[packageName]; | |
return [packageName, formatSize(size), sizePR === size ? 'No change' : formatSize(sizePR)]; | |
}).map((data) => ({ data })), | |
); | |
core.summary.write(); |