Skip to content

Commit

Permalink
add message option
Browse files Browse the repository at this point in the history
  • Loading branch information
dartess committed Nov 7, 2024
1 parent 22c14ee commit 62002de
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,9 @@ Each section in the config can have these options:
Format: `100 B`, `10 kB`, `500 ms`, `1 s`.
* **name**: the name of the current section. It will only be useful
if you have multiple sections.
* **message**: an optional custom message to display additional information,
such as guidance for resolving errors, relevant links, or instructions
for next steps when a limit is exceeded.
* **entry**: when using a custom webpack config, a webpack entry could be given.
It could be a string or an array of strings.
By default, the total size of all entry points will be checked.
Expand Down
3 changes: 3 additions & 0 deletions packages/size-limit/create-reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ function createHumanReporter(process, isSilentMode = false) {
}
let diff = formatBytes(check.size - check.sizeLimit)
print(red(`Package size limit has exceeded by ${diff}`))
if (check.message) {
print(check.message)
}
} else if (check.highlightLess && check.size < check.sizeLimit) {
let diff = formatBytes(check.sizeLimit - check.size)
print(bgGreen(black(`Package size is ${diff} less than limit`)))
Expand Down
1 change: 1 addition & 0 deletions packages/size-limit/get-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ let OPTIONS = {
ignore: ['webpack', 'esbuild'],
import: ['webpack', 'esbuild'],
limit: true,
message: true,
modifyEsbuildConfig: 'esbuild',
modifyWebpackConfig: 'webpack',
module: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ exports[`renders config-less result 1`] = `
"
`;

exports[`renders custom message 1`] = `
"
Package size limit has exceeded by 1 B
see docs for additional instructions
Size limit: 100 B
Size: 101 B brotlied
Try to reduce size or increase limit at .size-limit.json
"
`;

exports[`renders failed results 1`] = `
"
ok
Expand Down
18 changes: 18 additions & 0 deletions packages/size-limit/test/create-reporter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,24 @@ it('renders single result', () => {
).toMatchSnapshot()
})

it('renders custom message', () => {
expect(
results(['file'], {
checks: [
{
message: 'see docs for additional instructions',
name: 'big fail',
passed: false,
size: 101,
sizeLimit: 100
}
],
configPath: '.size-limit.json',
failed: true
})
).toMatchSnapshot()
})

it('renders config-less result', () => {
expect(
results(['time'], {
Expand Down

0 comments on commit 62002de

Please sign in to comment.