Skip to content

Commit

Permalink
Make brotli optional (#320)
Browse files Browse the repository at this point in the history
* add a smart layer over brotli

* add plugin

* remove brotli size

* undo unintentional change

* unused dep

* add 3 node versions to test

* call out explicit versions in travis

* add brotli plugin as a dev dependency

* show raw error as well

* fix polyfill require
  • Loading branch information
siddharthkp authored Jul 9, 2019
1 parent 51b2094 commit 5ce8b93
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
language: node_js
node_js:
- 12 # Latest
- 10 # LTS
- 8
cache:
directories:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"license": "MIT",
"dependencies": {
"axios": "^0.19.0",
"brotli-size": "0.1.0",
"bytes": "^3.1.0",
"ci-env": "^1.4.0",
"commander": "^2.20.0",
Expand All @@ -65,6 +64,7 @@
"babel-preset-es2015": "^7.0.0-beta.3",
"babel-preset-stage-3": "^7.0.0-beta.3",
"babel-traverse": "^7.0.0-beta.3",
"bundlesize-plugin-brotli": "^1.0.0",
"execa": "^2.0.1",
"husky": "^0.14.3",
"lint-staged": "^7.1.1",
Expand Down
3 changes: 3 additions & 0 deletions packages/bundlesize-plugin-brotli/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const brotliSize = require('brotli-size')

module.exports = { sync: brotliSize.sync }
12 changes: 12 additions & 0 deletions packages/bundlesize-plugin-brotli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "bundlesize-plugin-brotli",
"version": "1.0.0",
"description": "Plugin to use brotli compression with bundlesize",
"main": "index.js",
"keywords": [],
"author": "",
"license": "MIT",
"dependencies": {
"brotli-size": "0.1.0"
}
}
56 changes: 56 additions & 0 deletions packages/bundlesize-plugin-brotli/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<p align="center">
<img src="https://cdn.rawgit.com/siddharthkp/bundlesize/master/art/logo.png" height="200px">
<br><br>
<b>Plugin to use brotli compression with bundlesize on Node < 10.6.0</b>
<br>
</p>

&nbsp;

#### Note:

If you are using Node version >= 10.16.0, you do not need this plugin.

&nbsp;

#### Install

```sh
npm install bundlesize-plugin-brotli --save-dev

# or

yarn add bundlesize-plugin-brotli --dev
```

&nbsp;

#### Setting up bundlesize

&nbsp;

See bundlesize usage here: https://github.com/siddharthkp/bundlesize

&nbsp;

#### Using brotli compression

&nbsp;

By default, bundlesize `gzips` your build files before comparing.

If you are using `brotli` instead of gzip, you can specify that with each file:

```json
{
"files": [
{
"path": "./build/vendor.js",
"maxSize": "5 kB",
"compression": "brotli"
}
]
}
```

&nbsp;
48 changes: 48 additions & 0 deletions src/brotli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const { error } = require('prettycli')
const zlib = require('zlib')

const config = require('./config')

function getBrotliSync() {
// Does this project want brotli compression?
const needsBrotli = config.find(row => row.compression === 'brotli')

// If it doesn't, save us the trouble.
if (!needsBrotli) return null

// Check if the installed version of node supports brotli
const hasBrotli = zlib.brotliCompressSync
if (hasBrotli) return nativeBrotliSync

// Looks like this version of node does not have brotli
// We recommend using bundlesize-plugin-brotli which acts
// like a polyfill

try {
const polyfill = require('bundlesize-plugin-brotli')
// if the user has installed the plugin, we can safely return it
return polyfill.sync
} catch (err) {
// if they haven't, show them an error and exit with error code 1
if (err && err.code === 'MODULE_NOT_FOUND') {
const message = `Missing dependency: bundlesize-plugin-brotli
To use brotli with Node versions lower than v10.16.0,
please install bundlesize-plugin-brotli as a dev dependency.
You can read about the compression options here:
https://github.com/siddharthkp/bundlesize#customisation`

error(message, { silent: true })
} else {
// if it's a different error, show the raw error
error(err, { silent: true })
}
}
}

function nativeBrotliSync(input) {
return zlib.brotliCompressSync(input).length
}

module.exports = { sync: getBrotliSync() }
2 changes: 1 addition & 1 deletion src/compressed-size.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const gzip = require('gzip-size')
const brotli = require('brotli-size')
const brotli = require('./brotli')

const getCompressedSize = (data, compression = 'gzip') => {
let size
Expand Down

0 comments on commit 5ce8b93

Please sign in to comment.