diff --git a/README.md b/README.md index 72ceb778..0972c03a 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/packages/size-limit/create-reporter.js b/packages/size-limit/create-reporter.js index 04396bda..6e9f9963 100644 --- a/packages/size-limit/create-reporter.js +++ b/packages/size-limit/create-reporter.js @@ -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`))) diff --git a/packages/size-limit/get-config.js b/packages/size-limit/get-config.js index 54834531..5289d93d 100644 --- a/packages/size-limit/get-config.js +++ b/packages/size-limit/get-config.js @@ -21,6 +21,7 @@ let OPTIONS = { ignore: ['webpack', 'esbuild'], import: ['webpack', 'esbuild'], limit: true, + message: true, modifyEsbuildConfig: 'esbuild', modifyWebpackConfig: 'webpack', module: true, diff --git a/packages/size-limit/test/__snapshots__/create-reporter.test.js.snap b/packages/size-limit/test/__snapshots__/create-reporter.test.js.snap index 8c9fd7c0..7aba6c4c 100644 --- a/packages/size-limit/test/__snapshots__/create-reporter.test.js.snap +++ b/packages/size-limit/test/__snapshots__/create-reporter.test.js.snap @@ -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 diff --git a/packages/size-limit/test/create-reporter.test.js b/packages/size-limit/test/create-reporter.test.js index edae1c06..9e04926c 100644 --- a/packages/size-limit/test/create-reporter.test.js +++ b/packages/size-limit/test/create-reporter.test.js @@ -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'], {