Skip to content

Commit d71f1d5

Browse files
Added next bundle analyzer
1 parent 46a7a7c commit d71f1d5

File tree

4 files changed

+326
-1
lines changed

4 files changed

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

next.config.mjs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import withBundleAnalyzer from '@next/bundle-analyzer'
2+
3+
14
/**
25
* @type {import('next').NextConfig}
36
*/
@@ -10,4 +13,8 @@ const nextConfig = {
1013
swcMinify: true,
1114
}
1215

13-
export default nextConfig
16+
const withNextBundleAnalyzer = withBundleAnalyzer({
17+
enabled: process.env.NEXT_BUNDLE_ANALYZER === 'true'
18+
})
19+
20+
export default withNextBundleAnalyzer(nextConfig)

0 commit comments

Comments
 (0)