Skip to content

Commit

Permalink
use cosmicconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthkp committed Jun 28, 2019
1 parent 9e059eb commit ef721e1
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 17 deletions.
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"semi": false,
"singleQuote": true
}
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
[![NPM Downloads](https://img.shields.io/npm/dm/bundlesize.svg?style=flat)](https://www.npmjs.com/package/bundlesize)
 


#### minimal setup

```sh
Expand Down Expand Up @@ -44,6 +43,7 @@ npx bundlesize
 

#### 1) Add the path and maxSize in your `package.json`.

By default the gzipped size is tested. You can use the `compression` option to change this. (`gzip`, `brotli`, or `none`).

```json
Expand All @@ -59,6 +59,19 @@ By default the gzipped size is tested. You can use the `compression` option to c
}
```

You can also keep the config in a separate file as well. Create a `bundlesize.config.json` in your project root in a `config` folder.

```json
{
"files": [
{
"path": "./dist.js",
"maxSize": "3 kB"
}
]
}
```

`bundlesize` also supports [glob patterns](https://github.com/isaacs/node-glob)

Example:
Expand Down Expand Up @@ -91,14 +104,14 @@ Currently works for [Travis CI](https://travis-ci.org), [CircleCI](https://circl
- Add this token as `BUNDLESIZE_GITHUB_TOKEN` as environment parameter in your CIs project settings.

Using a different CI? You will need to supply an additional 4 environment variables.
- `CI_REPO_OWNER` given the repo `https://github.com/myusername/myrepo` would be `myusername`

- `CI_REPO_OWNER` given the repo `https://github.com/myusername/myrepo` would be `myusername`
- `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}`

(Ask me for help if you're stuck)


 

#### CLI
Expand Down Expand Up @@ -156,7 +169,7 @@ For more granular configuration, we recommend configuring it in the `package.jso

#### TODO

- Work with other CI tools
- Work with other CI tools
- [AppVeyor](https://www.appveyor.com/) ([#234](https://github.com/siddharthkp/bundlesize/issues/234))
- Automate setup (setting env_var)

Expand Down
16 changes: 16 additions & 0 deletions examples/bundlesize.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"files": [
{
"path": "./build/direct-path.js",
"maxSize": "50Kb"
},
{
"path": "./dist/generated-file-chunk-*.js",
"maxSize": "50Kb"
},
{
"path": "./dist/all-files-in-folder/**/*.js",
"maxSize": "50Kb"
}
]
}
25 changes: 25 additions & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "your-project",
"version": "1.0.0",
"scripts": {
"test": "bundlesize"
},
"bundlesize": [
{
"path": "./index.js",
"maxSize": "600B"
},
{
"path": "./src/files.js",
"maxSize": "600B"
},
{
"path": "./src/compressed-size.js",
"maxSize": "420B",
"compression": "none"
}
],
"devDependencies": {
"bundlesize": "^0.18.0"
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@
"bytes": "^3.1.0",
"ci-env": "^1.4.0",
"commander": "^2.20.0",
"cosmiconfig": "^5.2.1",
"github-build": "^1.2.0",
"glob": "^7.1.4",
"gzip-size": "^4.0.0",
"prettycli": "^1.4.3",
"read-pkg-up": "^3.0.0"
"prettycli": "^1.4.3"
},
"bundlesize": [
{
Expand Down
30 changes: 19 additions & 11 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
const readPkgUp = require('read-pkg-up')
const cosmiconfig = require('cosmiconfig')

const pkg = readPkgUp.sync().pkg
const program = require('commander')
const { error } = require('prettycli')
const debug = require('./debug')

/* Config from package.json */
const packageJSONconfig = pkg.bundlesize
/* Config from file */

let fileConfig

const explorer = cosmiconfig('bundlesize', {
searchPlaces: ['package.json', 'bundlesize.config.json', 'config/bundlesize.config.json']
})

const result = explorer.searchSync()

if (result) {
if (result.filepath.includes('package.json')) fileConfig = result.config
else fileConfig = result.config.files
}

/* Config from CLI */

program
.option('-f, --files [files]', 'files to test against (dist/*.js)')
.option('-s, --max-size [maxSize]', 'maximum size threshold (3Kb)')
.option('--debug', 'run in debug mode')
.option(
'-c, --compression [compression]',
'specify which compression algorithm to use'
)
.option('-c, --compression [compression]', 'specify which compression algorithm to use')
.parse(process.argv)

let cliConfig
Expand All @@ -34,7 +42,7 @@ if (program.files) {

/* Send to readme if no configuration is provided */

if (!packageJSONconfig && !cliConfig) {
if (!fileConfig && !cliConfig) {
error(
`Config not found.
Expand All @@ -45,10 +53,10 @@ if (!packageJSONconfig && !cliConfig) {
)
}

const config = cliConfig || packageJSONconfig
const config = cliConfig || fileConfig

debug('cli config', cliConfig)
debug('package json config', packageJSONconfig)
debug('file config', fileConfig)
debug('selected config', config)

module.exports = config

0 comments on commit ef721e1

Please sign in to comment.