Skip to content

Commit

Permalink
Add minSize option, fixes siddharthkp#207
Browse files Browse the repository at this point in the history
  • Loading branch information
larionov authored and Sergey Larionov committed Jun 10, 2022
1 parent 6de086e commit 72b05e6
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 10 deletions.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

 
Expand Down Expand Up @@ -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)

Expand All @@ -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).
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
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
2 changes: 2 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -46,6 +47,7 @@ if (program.files) {
{
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

0 comments on commit 72b05e6

Please sign in to comment.