Skip to content

Commit cb14941

Browse files
committed
feat: analysis
1 parent 6235948 commit cb14941

File tree

4 files changed

+278
-2
lines changed

4 files changed

+278
-2
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Copyright (c) HashiCorp, Inc.
2+
# SPDX-License-Identifier: MPL-2.0
3+
4+
name: 'Next.js Bundle Analysis'
5+
6+
on:
7+
pull_request:
8+
push:
9+
branches:
10+
- main # change this if your default branch is named differently
11+
workflow_dispatch:
12+
13+
defaults:
14+
run:
15+
# change this if your nextjs app does not live at the root of the repo
16+
working-directory: ./
17+
18+
permissions:
19+
contents: read # for checkout repository
20+
actions: read # for fetching base branch bundle stats
21+
pull-requests: write # for comments
22+
23+
jobs:
24+
analyze:
25+
runs-on: ubuntu-latest
26+
steps:
27+
- uses: actions/checkout@v3
28+
29+
- name: Install Node.js
30+
uses: actions/setup-node@v3
31+
with:
32+
node-version: 18
33+
34+
- name: Install dependencies
35+
uses: bahmutov/npm-install@v1
36+
37+
# If pnpm is used, you need to switch the previous step with the following one. pnpm does not create a package-lock.json
38+
# so the step above will fail to pull dependencies
39+
# - uses: pnpm/action-setup@v2
40+
# name: Install pnpm
41+
# id: pnpm-install
42+
# with:
43+
# version: 7
44+
# run_install: true
45+
46+
- name: Restore next build
47+
uses: actions/cache@v3
48+
id: restore-build-cache
49+
env:
50+
cache-name: cache-next-build
51+
with:
52+
# if you use a custom build directory, replace all instances of `.next` in this file with your build directory
53+
# ex: if your app builds to `dist`, replace `.next` with `dist`
54+
path: .next/cache
55+
# change this if you prefer a more strict cache
56+
key: ${{ runner.os }}-build-${{ env.cache-name }}
57+
58+
- name: Build next.js app
59+
# change this if your site requires a custom build command
60+
run: ./node_modules/.bin/next build
61+
62+
# Here's the first place where next-bundle-analysis' own script is used
63+
# This step pulls the raw bundle stats for the current bundle
64+
- name: Analyze bundle
65+
run: npx -p nextjs-bundle-analysis report
66+
67+
- name: Upload bundle
68+
uses: actions/upload-artifact@v3
69+
with:
70+
name: bundle
71+
path: .next/analyze/__bundle_analysis.json
72+
73+
- name: Download base branch bundle stats
74+
uses: dawidd6/action-download-artifact@v2
75+
if: success() && github.event.number
76+
with:
77+
workflow: nextjs_bundle_analysis.yml
78+
branch: ${{ github.event.pull_request.base.ref }}
79+
path: .next/analyze/base
80+
81+
# And here's the second place - this runs after we have both the current and
82+
# base branch bundle stats, and will compare them to determine what changed.
83+
# There are two configurable arguments that come from package.json:
84+
#
85+
# - budget: optional, set a budget (bytes) against which size changes are measured
86+
# it's set to 350kb here by default, as informed by the following piece:
87+
# https://infrequently.org/2021/03/the-performance-inequality-gap/
88+
#
89+
# - red-status-percentage: sets the percent size increase where you get a red
90+
# status indicator, defaults to 20%
91+
#
92+
# Either of these arguments can be changed or removed by editing the `nextBundleAnalysis`
93+
# entry in your package.json file.
94+
- name: Compare with base branch bundle
95+
if: success() && github.event.number
96+
run: ls -laR .next/analyze/base && npx -p nextjs-bundle-analysis compare
97+
98+
- name: Get Comment Body
99+
id: get-comment-body
100+
if: success() && github.event.number
101+
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
102+
run: |
103+
echo "body<<EOF" >> $GITHUB_OUTPUT
104+
echo "$(cat .next/analyze/__bundle_analysis_comment.txt)" >> $GITHUB_OUTPUT
105+
echo EOF >> $GITHUB_OUTPUT
106+
107+
- name: Find Comment
108+
uses: peter-evans/find-comment@v2
109+
if: success() && github.event.number
110+
id: fc
111+
with:
112+
issue-number: ${{ github.event.number }}
113+
body-includes: '<!-- __NEXTJS_BUNDLE -->'
114+
115+
- name: Create Comment
116+
uses: peter-evans/create-or-update-comment@v2
117+
if: success() && github.event.number && steps.fc.outputs.comment-id == 0
118+
with:
119+
issue-number: ${{ github.event.number }}
120+
body: ${{ steps.get-comment-body.outputs.body }}
121+
122+
- name: Update Comment
123+
uses: peter-evans/create-or-update-comment@v2
124+
if: success() && github.event.number && steps.fc.outputs.comment-id != 0
125+
with:
126+
issue-number: ${{ github.event.number }}
127+
body: ${{ steps.get-comment-body.outputs.body }}
128+
comment-id: ${{ steps.fc.outputs.comment-id }}
129+
edit-mode: replace

next.config.mjs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
/** @type {import('next').NextConfig} */
2-
const nextConfig = {};
2+
import NextBundleAnalyzer from '@next/bundle-analyzer'
3+
4+
let nextConfig = {};
5+
6+
nextConfig = NextBundleAnalyzer({
7+
enabled: true,
8+
})(nextConfig)
39

410
export default nextConfig;

package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.1.0",
44
"private": true,
55
"scripts": {
6+
"analyze": "set ANALYZE=true && set BUNDLE_ANALYZE=browser && npm run build",
67
"dev": "next dev",
78
"build": "next build",
89
"start": "next start",
@@ -13,6 +14,7 @@
1314
"prepare": "husky"
1415
},
1516
"dependencies": {
17+
"@next/bundle-analyzer": "^14.2.15",
1618
"@types/prettier": "^3.0.0",
1719
"next": "14.2.15",
1820
"prettier": "3.3.2",
@@ -51,5 +53,11 @@
5153
"engines": {
5254
"node": ">=18",
5355
"pnpm": "=9.4.0"
56+
},
57+
"nextBundleAnalysis": {
58+
"budget": 358400,
59+
"budgetPercentIncreaseRed": 20,
60+
"minimumChangeThreshold": 0,
61+
"showDetails": true
5462
}
55-
}
63+
}

0 commit comments

Comments
 (0)