Skip to content

Commit 91a2446

Browse files
committed
feat: add skipCommentIfEmpty config option
1 parent c3405bd commit 91a2446

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

.changeset/poor-bats-notice.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'nextjs-bundle-analysis': minor
33
---
44

5-
Add app name to comment for uniqueness
5+
Improve monorepo support by adding the `name` from `package.json` to the generated comment. Add support for a `skipCommentIfEmpty` configuration option that will set the comment to an empty string when no pages have changed size.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ For example, if you build to `dist`, you should:
4343

4444
(Optional, defaults to `0`) The threshold under which pages will be considered unchanged. For example, if `minimumChangeThreshold` was set to `500` and a page's size increased by `300 B`, it will be considered unchanged.
4545

46+
### `skipCommentIfEmpty (boolean)`
47+
48+
(Optional, defaults to `false`) When set to `true`, if no pages have changed size the generated comment will be an empty string.
49+
4650
## Caveats
4751

4852
- This plugin only analyzes the direct bundle output from next.js. If you have added any other scripts via the `<script>` tag, especially third party scripts and things like analytics or other tracking scripts, these are not included in the analysis. Scripts of this nature should _probably_ be loaded in behind a consent manager and should not make an impact on your initial load, and as long as this is how you handle them it should make no difference, but it's important to be aware of this and account for the extra size added by these scripts if they are present in your app.

compare.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* SPDX-License-Identifier: MPL-2.0
55
*/
66

7-
87
const originalFilesize = require('filesize')
98
const numberToWords = require('number-to-words')
109
const fs = require('fs')
@@ -29,6 +28,7 @@ const SHOW_DETAILS =
2928
options.showDetails === undefined ? true : options.showDetails
3029
const BUILD_OUTPUT_DIRECTORY = getBuildOutputDirectory(options)
3130
const PACKAGE_NAME = options.name
31+
const SKIP_COMMENT_IF_EMPTY = options.skipCommentIfEmpty
3232

3333
// import the current and base branch bundle stats
3434
const currentBundle = require(path.join(
@@ -45,7 +45,7 @@ const baseBundle = require(path.join(
4545
// kick it off
4646
let output = `## 📦 Next.js Bundle Analysis for ${PACKAGE_NAME}
4747
48-
This analysis was generated by the [next.js bundle analysis action](https://github.com/hashicorp/nextjs-bundle-analysis) 🤖
48+
This analysis was generated by the [Next.js Bundle Analysis action](https://github.com/hashicorp/nextjs-bundle-analysis). 🤖
4949
5050
`
5151

@@ -186,14 +186,21 @@ ${
186186
}
187187

188188
// and finally, if there are no changes at all, we try to be clear about that
189-
if (!newPages.length && !changedPages.length && !globalBundleChanges) {
190-
output += 'This PR introduced no changes to the javascript bundle 🙌'
189+
const hasNoChanges =
190+
!newPages.length && !changedPages.length && !globalBundleChanges
191+
if (hasNoChanges) {
192+
output += 'This PR introduced no changes to the JavaScript bundle! 🙌'
191193
}
192194

193195
// we add this tag so that our action can be able to easily and consistently find the
194196
// right comment to edit as more commits are pushed.
195197
output += `<!-- __NEXTJS_BUNDLE_${PACKAGE_NAME} -->`
196198

199+
// however, if ignoreIfEmpty is true, set output to an empty string
200+
if (hasNoChanges && SKIP_COMMENT_IF_EMPTY) {
201+
output = ''
202+
}
203+
197204
// log the output, mostly for testing and debugging. this will show up in the
198205
// github actions console.
199206
console.log(output)

0 commit comments

Comments
 (0)