-
-
Notifications
You must be signed in to change notification settings - Fork 489
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
feat: Brotli compression support #432
base: master
Are you sure you want to change the base?
Changes from all commits
912aa8c
6a4a361
ec1fa85
4dfbb5c
80511a8
70d8cf1
3f183c2
b8dcd04
b71f9ae
13f00fe
db1d33d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,11 +18,17 @@ import Search from './Search'; | |
import {store} from '../store'; | ||
import ModulesList from './ModulesList'; | ||
|
||
const SIZE_SWITCH_ITEMS = [ | ||
{label: 'Stat', prop: 'statSize'}, | ||
{label: 'Parsed', prop: 'parsedSize'}, | ||
{label: 'Gzipped', prop: 'gzipSize'} | ||
]; | ||
function allSizeSwitchItems() { | ||
const items = [ | ||
{label: 'Stat', prop: 'statSize'}, | ||
{label: 'Parsed', prop: 'parsedSize'} | ||
]; | ||
|
||
if (window.compressionAlgorithm === 'gzip') items.push({label: 'Gzipped', prop: 'gzipSize'}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice if we could first do this as a new feature, release in a minor version and then do a major release later to consolidate the API on on plain What do you think of this plan? |
||
if (window.compressionAlgorithm === 'brotli') items.push({label: 'Brotli', prop: 'brotliSize'}); | ||
|
||
return items; | ||
} | ||
|
||
@observer | ||
export default class ModulesTreemap extends Component { | ||
|
@@ -138,7 +144,7 @@ export default class ModulesTreemap extends Component { | |
renderModuleSize(module, sizeType) { | ||
const sizeProp = `${sizeType}Size`; | ||
const size = module[sizeProp]; | ||
const sizeLabel = SIZE_SWITCH_ITEMS.find(item => item.prop === sizeProp).label; | ||
const sizeLabel = allSizeSwitchItems().find(item => item.prop === sizeProp).label; | ||
const isActive = (store.activeSize === sizeProp); | ||
|
||
return (typeof size === 'number') ? | ||
|
@@ -162,7 +168,8 @@ export default class ModulesTreemap extends Component { | |
}; | ||
|
||
@computed get sizeSwitchItems() { | ||
return store.hasParsedSizes ? SIZE_SWITCH_ITEMS : SIZE_SWITCH_ITEMS.slice(0, 1); | ||
const items = allSizeSwitchItems(); | ||
return store.hasParsedSizes ? items : items.slice(0, 1); | ||
} | ||
|
||
@computed get activeSizeItem() { | ||
|
@@ -316,7 +323,7 @@ export default class ModulesTreemap extends Component { | |
<br/> | ||
{this.renderModuleSize(module, 'stat')} | ||
{!module.inaccurateSizes && this.renderModuleSize(module, 'parsed')} | ||
{!module.inaccurateSizes && this.renderModuleSize(module, 'gzip')} | ||
{!module.inaccurateSizes && this.renderModuleSize(module, window.compressionAlgorithm)} | ||
{module.path && | ||
<div>Path: <strong>{module.path}</strong></div> | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const zlib = require('zlib'); | ||
|
||
const COMPRESSED_SIZE = { | ||
gzip: gzipSize, | ||
brotli: brotliSize | ||
}; | ||
|
||
export function getCompressedSize(compressionAlgorithm, input) { | ||
const fn = COMPRESSED_SIZE[compressionAlgorithm]; | ||
if (!fn) throw new Error(`Unsupported compression algorithm: ${compressionAlgorithm}.`); | ||
return fn(input); | ||
} | ||
|
||
function gzipSize(input) { | ||
return zlib.gzipSync(input, {level: 9}).length; | ||
} | ||
|
||
function brotliSize(input) { | ||
if (typeof zlib.brotliCompressSync !== 'function') { | ||
throw new Error('Brotli compression requires Node.js v10.16.0 or higher.'); | ||
} | ||
dcsaszar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return zlib.brotliCompressSync(input).length; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case user will have to provide compression algorithm twice: for
compressionAlgorithm
anddefaultSizes
and it opens possibilities for invalid option combinations likedefaultSizes: 'gzip', compressionAlgorithm: 'brotli'
which don't make sense. I personally would changedefaultSizes: 'gzip'
todefaultSizes: 'compressed'
and deprecate the former, but support it (convert tocompressed
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation handles the mismatch gracefully, so it's more of a cosmetic issue on the user side.
Reintroducing
compressed
would help here. If used, the config always looks "good", even when switching the algorithm. If we supportcompressed
, fail on mismatches betweencompressionAlgorithm
anddefaultSizes
?