Skip to content

Commit b921279

Browse files
committed
Add support for "none" and "brotli" compresion option values. (fixes #23)
1 parent 20e5122 commit b921279

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@
8383
"ci-env": "^1.9.0",
8484
"escape-string-regexp": "^1.0.5",
8585
"glob": "^7.1.4",
86-
"gzip-size": "^5.1.1",
8786
"minimatch": "^3.0.4",
8887
"pretty-bytes": "^5.3.0",
8988
"util.promisify": "^1.0.0"

src/index.js

+32-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import path from 'path';
1818
import promisify from 'util.promisify';
1919
import globPromise from 'glob';
2020
import minimatch from 'minimatch';
21-
import gzipSize from 'gzip-size';
21+
import zlib from 'zlib';
2222
import chalk from 'chalk';
2323
import prettyBytes from 'pretty-bytes';
2424
import escapeRegExp from 'escape-string-regexp';
@@ -29,6 +29,15 @@ import fs from 'fs-extra';
2929
const glob = promisify(globPromise);
3030
const NAME = 'SizePlugin';
3131

32+
const GZIP_OPTS = {
33+
level: 9
34+
};
35+
const BROTLI_OPTS = {
36+
params: {
37+
[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY
38+
}
39+
};
40+
3241
/**
3342
* `new SizePlugin(options)`
3443
* @param {Object} options
@@ -38,6 +47,7 @@ const NAME = 'SizePlugin';
3847
* @param {boolean} [options.publish] option to publish filesizes to size-plugin-store
3948
* @param {boolean} [options.writeFile] option to save filesizes to disk
4049
* @param {function} [options.stripHash] custom function to remove/normalize hashed filenames for comparison
50+
* @param {'none' | 'gzip' | 'brotli'} [options.compression = 'gzip'] change the compression algorithm used for calculated sizes
4151
* @param {string} [options.mode] custom Webpack "mode" - only use this to emulate "mode" in Webpack 3.
4252
* @param {(item:Item)=>string?} [options.decorateItem] custom function to decorate items
4353
* @param {(data:Data)=>string?} [options.decorateAfter] custom function to decorate all output
@@ -48,6 +58,7 @@ export default class SizePlugin {
4858
this.options = options || {};
4959
this.pattern = this.options.pattern || '**/*.{mjs,js,css,html}';
5060
this.exclude = this.options.exclude;
61+
this.compression = this.options.compression || 'gzip';
5162
this.options.filename = this.options.filename || 'size-plugin.json';
5263
this.options.writeFile = this.options.writeFile !== false;
5364
this.filename = path.join(process.cwd(), this.options.filename);
@@ -191,7 +202,7 @@ export default class SizePlugin {
191202
file => isMatched(file) && !isExcluded(file)
192203
);
193204
const sizes = await Promise.all(
194-
assetNames.map(name => gzipSize(assets[name].source()))
205+
assetNames.map(name => this.getCompressedSize(assets[name].source()))
195206
);
196207

197208
// map of de-hashed filenames to their final size
@@ -271,11 +282,29 @@ export default class SizePlugin {
271282
const files = await glob(this.pattern, { cwd, ignore: this.exclude });
272283

273284
const sizes = await Promise.all(
274-
files.map(file => gzipSize.file(path.join(cwd, file)).catch(() => null))
285+
files.map(async file => {
286+
const source = await fs.promises.readFile(path.join(cwd, file)).catch(() => null);
287+
if (source == null) return null;
288+
return this.getCompressedSize(source);
289+
})
275290
);
276291

277292
return toMap(files.map(filename => this.stripHash(filename)), sizes);
278293
}
294+
295+
async getCompressedSize(source) {
296+
let compressed = source;
297+
if (this.compression === 'gzip') {
298+
const gz = promisify(zlib.gzip);
299+
compressed = await gz(source, GZIP_OPTS);
300+
}
301+
else if (this.compression === 'brotli') {
302+
if (!zlib.brotliCompress) throw Error('Brotli not supported in this Node version.');
303+
const br = promisify(zlib.brotliCompress);
304+
compressed = await br(source, BROTLI_OPTS);
305+
}
306+
return Buffer.byteLength(compressed);
307+
}
279308
}
280309

281310

0 commit comments

Comments
 (0)