Skip to content

Commit 9a6fbd5

Browse files
authored
ci: next bundle analysis (#309)
* ci: next bundle analysis * remove dep * fix path
1 parent 26f301c commit 9a6fbd5

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
name: Next.js Bundle Analysis
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- main
8+
workflow_dispatch:
9+
10+
jobs:
11+
analyze:
12+
env:
13+
working-directory: website
14+
defaults:
15+
run:
16+
working-directory: ${{ env.working-directory }}
17+
18+
runs-on: ubuntu-latest
19+
20+
steps:
21+
- name: Cancel Previous Runs
22+
uses: styfle/[email protected]
23+
with:
24+
access_token: ${{ github.token }}
25+
26+
- name: Checkout the repo
27+
uses: actions/checkout@v3
28+
29+
- name: Setup env
30+
uses: the-guild-org/shared-config/setup@main
31+
with:
32+
nodeVersion: 18
33+
packageManager: pnpm
34+
35+
- name: Restore Next.js Build
36+
uses: actions/cache@v3
37+
id: restore-build-cache
38+
env:
39+
cache-name: cache-next-build
40+
with:
41+
# if you use a custom build directory, replace all instances of `.next` in this file with your build directory
42+
# ex: if your app builds to `dist`, replace `.next` with `dist`
43+
path: ${{ env.working-directory }}/.next/cache
44+
# change this if you prefer a more strict cache
45+
key: ${{ runner.os }}-build-${{ env.cache-name }}
46+
47+
- name: Build Next.js App
48+
# change this if your site requires a custom build command
49+
run: pnpm build
50+
51+
# Here's the first place where next-bundle-analysis' own script is used
52+
# This step pulls the raw bundle stats for the current bundle
53+
- name: Analyze Bundle
54+
run: pnpm --package=nextjs-bundle-analysis dlx report
55+
56+
- name: Upload bundle
57+
uses: actions/upload-artifact@v3
58+
with:
59+
name: bundle
60+
path: ${{ env.working-directory }}/.next/analyze/__bundle_analysis.json
61+
62+
- name: Download Base Branch Bundle Stats
63+
uses: dawidd6/action-download-artifact@v2
64+
if: success() && github.event.number
65+
with:
66+
workflow: nextjs-bundle-analysis.yml
67+
branch: ${{ github.event.pull_request.base.ref }}
68+
path: ${{ env.working-directory }}/.next/analyze/base
69+
70+
# And here's the second place - this runs after we have both the current and
71+
# base branch bundle stats, and will compare them to determine what changed.
72+
# There are two configurable arguments that come from package.json:
73+
#
74+
# - budget: optional, set a budget (bytes) against which size changes are measured
75+
# it's set to 350kb here by default, as informed by the following piece:
76+
# https://infrequently.org/2021/03/the-performance-inequality-gap/
77+
#
78+
# - red-status-percentage: sets the percent size increase where you get a red
79+
# status indicator, defaults to 20%
80+
#
81+
# Either of these arguments can be changed or removed by editing the `nextBundleAnalysis`
82+
# entry in your package.json file.
83+
- name: Compare with Base Branch Bundle
84+
if: success() && github.event.number
85+
run: ls -laR .next/analyze/base && pnpm --package=nextjs-bundle-analysis dlx compare
86+
87+
- name: Get Comment Body
88+
id: get-comment-body
89+
if: success() && github.event.number
90+
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
91+
run: |
92+
echo "body<<EOF" >> $GITHUB_OUTPUT
93+
echo "$(cat .next/analyze/__bundle_analysis_comment.txt)" >> $GITHUB_OUTPUT
94+
echo EOF >> $GITHUB_OUTPUT
95+
96+
- name: Find Comment
97+
uses: peter-evans/find-comment@v2
98+
if: success() && github.event.number
99+
id: fc
100+
with:
101+
issue-number: ${{ github.event.number }}
102+
body-includes: '<!-- __NEXTJS_BUNDLE -->'
103+
104+
- name: Create or Update Comment
105+
uses: peter-evans/[email protected]
106+
if: success() && github.event.number
107+
with:
108+
issue-number: ${{ github.event.number }}
109+
body: ${{ steps.get-comment-body.outputs.body }}
110+
comment-id: ${{ steps.fc.outputs.comment-id }}
111+
edit-mode: replace

website/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,10 @@
5858
"lint-staged": {
5959
"**/*.{js,jsx,ts,tsx,mjs,cjs}": "eslint --fix",
6060
"**/*.{js,jsx,ts,tsx,mjs,cjs,md,mdx,yml,yaml,json}": "prettier --write"
61+
},
62+
"nextBundleAnalysis": {
63+
"budget": 358400,
64+
"budgetPercentIncreaseRed": 20,
65+
"showDetails": true
6166
}
6267
}

0 commit comments

Comments
 (0)