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

Make brotli optional #320

Merged
merged 10 commits into from
Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -37,7 +37,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 @@ -64,6 +63,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