diff --git a/README.md b/README.md index e4a46242..5dbcd821 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,28 @@ You can keep this array either in   +You can check for minimum size too. + +  + +Example: + +```json +"bundlesize": [ + { + "path": "./dist.js", + "maxSize": "3 kB" + "minSize": "2 kB" + } +] + +``` +  + +This makes it great for using with applications that are bundled with another tool. It will match multiple files if necessary and create a new row for each file. + +  + #### Customisation   @@ -191,7 +213,7 @@ You will need to supply an additional 5 environment variables. - `CI_REPO_NAME` given the repo `https://github.com/myusername/myrepo` would be `myrepo` - `CI_COMMIT_MESSAGE` the commit message - `CI_COMMIT_SHA` the SHA of the CI commit, in [Jenkins](https://jenkins.io/) you would use `${env.GIT_COMMIT}` -- `CI=true` usually set automatically in CI environments +- `CI=true` usually set automatically in CI environments (Ask me for help if you're stuck) @@ -205,6 +227,7 @@ bundlesize can also be used without creating a configuration file. We do not rec ```sh bundlesize -f "dist/*.js" -s 20kB +bundlesize -f "dist/*.js" -s 20kB --min-size 15kB ``` For more granular configuration, we recommend configuring it in the `package.json` (documented above). diff --git a/package.json b/package.json index 2671cd61..84135622 100644 --- a/package.json +++ b/package.json @@ -16,8 +16,9 @@ "precommit": "lint-staged", "t": "yarn test", "test": "ava -v tests/index.js", - "test:old": "npm run test:default && npm run test:no-compression && npm run test:brotli-compression", + "test:old": "npm run test:default && npm run test:no-compression && npm run test:brotli-compression && npm test:min-size", "test:default": "node index && cat pipe.js | node pipe --name pipe.js --max-size 1kB", + "test:min-size": "node index && cat pipe.js | node pipe --name pipe.js --max-size 1kB --min-size 0.5kB", "test:no-compression": "cat pipe.js | node pipe --compression none --name pipe.js", "test:brotli-compression": "cat pipe.js | node pipe --compression brotli --name pipe.js" }, diff --git a/pipe.js b/pipe.js index 56c5d9d8..3fabe6e9 100644 --- a/pipe.js +++ b/pipe.js @@ -17,6 +17,7 @@ if (process.stdin.isTTY) { program .option('-n, --name [name]', 'custom name for a file (lib.min.js)') .option('-s, --max-size [maxSize]', 'maximum size threshold (3Kb)') + .option('--min-size [minSize]', 'minimum size threshold (2Kb)') .option( '-c, --compression [gzip|brotli|none]', 'specify which compression algorithm to use' @@ -27,6 +28,7 @@ program const config = { name: program.name || require('read-pkg-up').sync().pkg.name, maxSize: program.maxSize, + minSize: program.minSize, compression: program.compression || 'gzip' } @@ -36,9 +38,11 @@ process.stdin.setEncoding('utf8') readStream(process.stdin).then(data => { const size = compressedSize(data, config.compression) const maxSize = bytes(config.maxSize) || Infinity + const minSize = bytes(config.minSize) || 0 const file = { path: config.name, maxSize, + minSize, size, compression: config.compression } diff --git a/src/config.js b/src/config.js index c1405a1b..c5db0535 100644 --- a/src/config.js +++ b/src/config.js @@ -14,6 +14,7 @@ const configPaths = ['package.json', 'bundlesize.config.json'] program .option('-f, --files [files]', 'files to test against (dist/*.js)') .option('-s, --max-size [maxSize]', 'maximum size threshold (3Kb)') + .option('--min-size [minSize]', 'minimum size threshold (2Kb)') .option('--debug', 'run in debug mode') .option('--config [config]', 'Get path of configuration file') .option( @@ -46,6 +47,7 @@ if (program.files) { { path: program.files, maxSize: program.maxSize, + minSize: program.minSize, compression: program.compression || 'gzip' } ] diff --git a/src/files.js b/src/files.js index 586231fc..02a0998d 100644 --- a/src/files.js +++ b/src/files.js @@ -16,9 +16,10 @@ config.map(file => { } else { paths.map(path => { const maxSize = bytes(file.maxSize) || Infinity + const minSize = bytes(file.minSize) || 0 const compression = file.compression || 'gzip' const size = compressedSize(fs.readFileSync(path, 'utf8'), compression) - files.push({ maxSize, path, size, compression }) + files.push({ maxSize, minSize, path, size, compression }) }) } }) diff --git a/src/reporter.js b/src/reporter.js index 816c1a3a..01f66f0e 100644 --- a/src/reporter.js +++ b/src/reporter.js @@ -32,7 +32,8 @@ const getGlobalMessage = ({ results, totalSize, totalSizeMaster, - totalMaxSize + totalMaxSize, + totalMinSize }) => { let globalMessage @@ -60,13 +61,14 @@ const getGlobalMessage = ({ // multiple files, no failures const prettySize = bytes(totalSize) const prettyMaxSize = bytes(totalMaxSize) + const prettyMinSize = bytes(totalMinSize) const change = totalSize - totalSizeMaster const prettyChange = change === 0 ? 'no change' : change > 0 ? `+${bytes(change)}` : `-${bytes(Math.abs(change))}` - globalMessage = `Total bundle size is ${prettySize}/${prettyMaxSize} (${prettyChange})` + globalMessage = `Total bundle size is ${prettyMinSize} < ${prettySize} < ${prettyMaxSize} (${prettyChange})` } return globalMessage } @@ -75,21 +77,27 @@ const analyse = ({ files, masterValues }) => { return files.map(file => { let fail = false file.master = masterValues[file.path] - const { path, size, master, maxSize, compression = 'gzip' } = file - + const { path, size, master, maxSize, minSize, compression = 'gzip' } = file let compressionText = '(no compression)' if (compression && compression !== 'none') { compressionText = `(${compression})` } - let message = `${path}: ${bytes(size)} ` + const prettySize = bytes(maxSize) + const prettyMinSize = bytes(minSize) + + let minSizeText = '' + if (minSize > 0) { + minSizeText = `minSize ${prettyMinSize} < ` + } + let message = `${path}: ${minSizeText}${bytes(size)} ` if (maxSize === Infinity) { message += compressionText } - const prettySize = bytes(maxSize) /* if size > maxSize, fail + else if size < minSize, fail else if size > master, warn + pass else yay + pass */ @@ -98,6 +106,10 @@ const analyse = ({ files, masterValues }) => { fail = true if (prettySize) message += `> maxSize ${prettySize} ${compressionText}` error(message, { fail: false, label: 'FAIL' }) + } else if (size < minSize) { + fail = true + if (prettyMinSize) message += `< minSize ${prettySize} ${compressionText}` + error(message, { fail: false, label: 'FAIL' }) } else if (!master) { if (prettySize) message += `< maxSize ${prettySize} ${compressionText}` info('PASS', message) @@ -156,7 +168,8 @@ const compare = (files, masterValues = {}) => { results, totalSize: results.reduce((acc, result) => acc + result.size, 0), totalSizeMaster: results.reduce((acc, result) => acc + result.master, 0), - totalMaxSize: results.reduce((acc, result) => acc + result.maxSize, 0) + totalMaxSize: results.reduce((acc, result) => acc + result.maxSize, 0), + totalMinSize: results.reduce((acc, result) => acc + result.minSize, 0) }) let fail = results.filter(result => result.fail).length > 0