diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 00000000..b2095be8 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,4 @@ +{ + "semi": false, + "singleQuote": true +} diff --git a/README.md b/README.md index 6abb5174..72e2e75f 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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: @@ -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 @@ -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) diff --git a/examples/bundlesize.config.json b/examples/bundlesize.config.json new file mode 100644 index 00000000..62787d20 --- /dev/null +++ b/examples/bundlesize.config.json @@ -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" + } + ] +} diff --git a/examples/package.json b/examples/package.json new file mode 100644 index 00000000..11120ced --- /dev/null +++ b/examples/package.json @@ -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" + } +} diff --git a/package.json b/package.json index 4bc0d298..e582f393 100644 --- a/package.json +++ b/package.json @@ -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": [ { diff --git a/src/config.js b/src/config.js index ad6c87b1..92d1daee 100644 --- a/src/config.js +++ b/src/config.js @@ -1,12 +1,23 @@ -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 */ @@ -14,10 +25,7 @@ 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 @@ -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. @@ -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