An action that compares 2 webpack compilation stats files, and comments on the PR with a description of the difference
In your application, ensure you output the stats.json, from `BundleAnalyzerPlugin'
// webpack.config.jsconst{BundleAnalyzerPlugin}=require('webpack-bundle-analyzer')// optionally you can also output compressed/gzipped stats. Requires a version >=1.1.0constCompressionPlugin=require('compression-webpack-plugin')module.exports={plugins: [ ...plugins,// not requirednewCompressionPlugin(),// requirednewBundleAnalyzerPlugin({// generate the stats.json filegenerateStatsFile: true})]}Then, in your action configuration, build both the head and the branch (in any way you see fit) and pass paths to the stats.json files as inputs ot this action
# .github/workflows/bundlesize-compare.ymlon: # this action will error unless run in a pr contextpull_request: pull_request_target: jobs: # Build current and upload stats.json# You may replace this with your own build method. All that# is required is that the stats.json be an artifactbuild-head: name: 'Build head'runs-on: ubuntu-latestpermissions: contents: readsteps: - uses: actions/checkout@v4with: ref: ${{github.event.pull_request.head.ref}} - name: Install dependenciesrun: npm ci - name: Buildrun: npm run build - name: Upload stats.jsonuses: actions/upload-artifact@v4with: name: head-statspath: ./dist/stats.json# Build base for comparison and upload stats.json# You may replace this with your own build method. All that# is required is that the stats.json be an artifactbuild-base: name: 'Build base'runs-on: ubuntu-latestpermissions: contents: readsteps: - uses: actions/checkout@v4with: ref: ${{github.base_ref }} - name: Install dependenciesrun: npm ci - name: Buildrun: npm run build - name: Upload stats.jsonuses: actions/upload-artifact@v4with: name: base-statspath: ./dist/stats.json# run the action against the stats.json filescompare: name: 'Compare base & head bundle sizes'runs-on: ubuntu-latestneeds: [build-base, build-head]permissions: pull-requests: writesteps: - uses: actions/download-artifact@v4 - uses: github/[email protected]with: github-token: ${{secrets.GITHUB_TOKEN }}current-stats-json-path: ./head-stats/stats.jsonbase-stats-json-path: ./base-stats/stats.jsonThis action requires the write permission for the permissions.pull-requests scope.
| name | description | required | type |
|---|---|---|---|
| current-stats-json-path | The path to the current stats.json file | true | string |
| base-stats-json-path | The path to the base stats.json file | true | string |
| github-token | The Github token | true | string |
| title | An optional addition to the title, which also helps key comments, useful if running more than 1 copy of this action | false | string |
| describe-assets | Option for asset description output. One of "all" (default), "changed-only", or "none". | false | string |