Skip to content

Commit

Permalink
Add support for "none" and "brotli" compresion option values. (fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
developit committed Jan 29, 2021
1 parent 20e5122 commit b921279
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@
"ci-env": "^1.9.0",
"escape-string-regexp": "^1.0.5",
"glob": "^7.1.4",
"gzip-size": "^5.1.1",
"minimatch": "^3.0.4",
"pretty-bytes": "^5.3.0",
"util.promisify": "^1.0.0"
Expand Down
35 changes: 32 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import path from 'path';
import promisify from 'util.promisify';
import globPromise from 'glob';
import minimatch from 'minimatch';
import gzipSize from 'gzip-size';
import zlib from 'zlib';
import chalk from 'chalk';
import prettyBytes from 'pretty-bytes';
import escapeRegExp from 'escape-string-regexp';
Expand All @@ -29,6 +29,15 @@ import fs from 'fs-extra';
const glob = promisify(globPromise);
const NAME = 'SizePlugin';

const GZIP_OPTS = {
level: 9
};
const BROTLI_OPTS = {
params: {
[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY
}
};

/**
* `new SizePlugin(options)`
* @param {Object} options
Expand All @@ -38,6 +47,7 @@ const NAME = 'SizePlugin';
* @param {boolean} [options.publish] option to publish filesizes to size-plugin-store
* @param {boolean} [options.writeFile] option to save filesizes to disk
* @param {function} [options.stripHash] custom function to remove/normalize hashed filenames for comparison
* @param {'none' | 'gzip' | 'brotli'} [options.compression = 'gzip'] change the compression algorithm used for calculated sizes
* @param {string} [options.mode] custom Webpack "mode" - only use this to emulate "mode" in Webpack 3.
* @param {(item:Item)=>string?} [options.decorateItem] custom function to decorate items
* @param {(data:Data)=>string?} [options.decorateAfter] custom function to decorate all output
Expand All @@ -48,6 +58,7 @@ export default class SizePlugin {
this.options = options || {};
this.pattern = this.options.pattern || '**/*.{mjs,js,css,html}';
this.exclude = this.options.exclude;
this.compression = this.options.compression || 'gzip';
this.options.filename = this.options.filename || 'size-plugin.json';
this.options.writeFile = this.options.writeFile !== false;
this.filename = path.join(process.cwd(), this.options.filename);
Expand Down Expand Up @@ -191,7 +202,7 @@ export default class SizePlugin {
file => isMatched(file) && !isExcluded(file)
);
const sizes = await Promise.all(
assetNames.map(name => gzipSize(assets[name].source()))
assetNames.map(name => this.getCompressedSize(assets[name].source()))
);

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

const sizes = await Promise.all(
files.map(file => gzipSize.file(path.join(cwd, file)).catch(() => null))
files.map(async file => {
const source = await fs.promises.readFile(path.join(cwd, file)).catch(() => null);
if (source == null) return null;
return this.getCompressedSize(source);
})
);

return toMap(files.map(filename => this.stripHash(filename)), sizes);
}

async getCompressedSize(source) {
let compressed = source;
if (this.compression === 'gzip') {
const gz = promisify(zlib.gzip);
compressed = await gz(source, GZIP_OPTS);
}
else if (this.compression === 'brotli') {
if (!zlib.brotliCompress) throw Error('Brotli not supported in this Node version.');
const br = promisify(zlib.brotliCompress);
compressed = await br(source, BROTLI_OPTS);
}
return Buffer.byteLength(compressed);
}
}


Expand Down

0 comments on commit b921279

Please sign in to comment.