Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add minSize option #209

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

 
Expand All @@ -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).
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -52,7 +53,8 @@
"bundlesize": [
{
"path": "./index.js",
"maxSize": "600B"
"maxSize": "600B",
"minSize": "100B"
},
{
"path": "./src/files.js",
Expand All @@ -61,6 +63,7 @@
{
"path": "./src/compressed-size.js",
"maxSize": "420B",
"minSize": "200B",
"compression": "none"
}
],
Expand Down
4 changes: 4 additions & 0 deletions pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
}

Expand All @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]',
Expand All @@ -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'
}
]
Expand Down
3 changes: 2 additions & 1 deletion src/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
})
}
})
Expand Down
27 changes: 20 additions & 7 deletions src/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const getGlobalMessage = ({
results,
totalSize,
totalSizeMaster,
totalMaxSize
totalMaxSize,
totalMinSize
}) => {
let globalMessage

Expand Down Expand Up @@ -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
}
Expand All @@ -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
*/
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down