-
-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
51b2094
commit 5ce8b93
Showing
7 changed files
with
123 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const brotliSize = require('brotli-size') | ||
|
||
module.exports = { sync: brotliSize.sync } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
||
| ||
|
||
#### Note: | ||
|
||
If you are using Node version >= 10.16.0, you do not need this plugin. | ||
|
||
| ||
|
||
#### Install | ||
|
||
```sh | ||
npm install bundlesize-plugin-brotli --save-dev | ||
|
||
# or | ||
|
||
yarn add bundlesize-plugin-brotli --dev | ||
``` | ||
|
||
| ||
|
||
#### Setting up bundlesize | ||
|
||
| ||
|
||
See bundlesize usage here: https://github.com/siddharthkp/bundlesize | ||
|
||
| ||
|
||
#### Using brotli compression | ||
|
||
| ||
|
||
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" | ||
} | ||
] | ||
} | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters