diff --git a/README.md b/README.md index 8ef592e6..6f003931 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,21 @@ Example: ``` +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.   @@ -105,6 +120,7 @@ example usage: ```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 ecec9008..5126dd58 100644 --- a/package.json +++ b/package.json @@ -14,8 +14,9 @@ }, "scripts": { "precommit": "lint-staged", - "test": "npm run test:default && npm run test:no-compression && npm run test:brotli-compression", + "test": "npm run test:default && npm run test:min-size && npm run test:no-compression && npm run test:brotli-compression", "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", "lint": "eslint src store/*.js" @@ -52,7 +53,8 @@ "bundlesize": [ { "path": "./index.js", - "maxSize": "600B" + "maxSize": "600B", + "minSize": "100B" }, { "path": "./src/files.js", @@ -61,6 +63,7 @@ { "path": "./src/compressed-size.js", "maxSize": "420B", + "minSize": "200B", "compression": "none" } ], 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 ad6c87b1..ee5c0218 100644 --- a/src/config.js +++ b/src/config.js @@ -13,6 +13,7 @@ const packageJSONconfig = pkg.bundlesize 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( '-c, --compression [compression]', @@ -21,12 +22,12 @@ program .parse(process.argv) let cliConfig - if (program.files) { cliConfig = [ { 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