Skip to content

Commit 67b3e14

Browse files
committed
feat: add support for Zstandard compression algorithm
1 parent 09a540a commit 67b3e14

13 files changed

Lines changed: 52 additions & 6 deletions

File tree

client/components/ModulesTreemap.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ function getSizeSwitchItems() {
2929

3030
if (window.compressionAlgorithm === 'brotli') items.push({label: 'Brotli', prop: 'brotliSize'});
3131

32+
if (window.compressionAlgorithm === 'zstd') items.push({label: 'Zstandard', prop: 'zstdSize'});
33+
3234
return items;
3335
};
3436

client/store.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import localStorage from './localStorage';
44

55
export class Store {
66
cid = 0;
7-
sizes = new Set(['statSize', 'parsedSize', 'gzipSize', 'brotliSize']);
7+
sizes = new Set(['statSize', 'parsedSize', 'gzipSize', 'brotliSize', 'zstdSize']);
88

99
@observable.ref allChunks;
1010
@observable.shallow selectedChunks;

src/analyzer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ function getViewerData(bundleStats, bundleDir, opts) {
113113
asset.parsedSize = Buffer.byteLength(assetSources.src);
114114
if (compressionAlgorithm === 'gzip') asset.gzipSize = getCompressedSize('gzip', assetSources.src);
115115
if (compressionAlgorithm === 'brotli') asset.brotliSize = getCompressedSize('brotli', assetSources.src);
116+
if (compressionAlgorithm === 'zstd') asset.zstdSize = getCompressedSize('zstd', assetSources.src);
116117
}
117118

118119
// Picking modules from current bundle script
@@ -169,6 +170,7 @@ function getViewerData(bundleStats, bundleDir, opts) {
169170
parsedSize: asset.parsedSize,
170171
gzipSize: asset.gzipSize,
171172
brotliSize: asset.brotliSize,
173+
zstdSize: asset.zstdSize,
172174
groups: Object.values(asset.tree.children).map(i => i.toChartData()),
173175
isInitialByEntrypoint: chunkToInitialByEntrypoint[filename] ?? {}
174176
}));

src/bin/analyzer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const Logger = require('../Logger');
1111
const utils = require('../utils');
1212

1313
const SIZES = new Set(['stat', 'parsed', 'gzip']);
14-
const COMPRESSION_ALGORITHMS = new Set(['gzip', 'brotli']);
14+
const COMPRESSION_ALGORITHMS = new Set(['gzip', 'brotli', 'zstd']);
1515

1616
const program = commander
1717
.version(require('../../package.json').version)

src/sizeUtils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const zlib = require('zlib');
33
export function getCompressedSize(compressionAlgorithm, input) {
44
if (compressionAlgorithm === 'gzip') return zlib.gzipSync(input, {level: 9}).length;
55
if (compressionAlgorithm === 'brotli') return zlib.brotliCompressSync(input).length;
6+
if (compressionAlgorithm === 'zstd') return zlib.zstdCompressSync(input).length;
67

78
throw new Error(`Unsupported compression algorithm: ${compressionAlgorithm}.`);
89
}

src/tree/ConcatenatedModule.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ export default class ConcatenatedModule extends Module {
2424
return this.getBrotliSize() ?? this.getEstimatedSize('brotliSize');
2525
}
2626

27+
get zstdSize() {
28+
return this.getZstdSize() ?? this.getEstimatedSize('zstdSize');
29+
}
30+
2731
getEstimatedSize(sizeType) {
2832
const parentModuleSize = this.parent[sizeType];
2933

src/tree/ContentFolder.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export default class ContentFolder extends BaseFolder {
1919
return this.getSize('brotliSize');
2020
}
2121

22+
get zstdSize() {
23+
return this.getSize('zstdSize');
24+
}
25+
2226
getSize(sizeType) {
2327
const ownerModuleSize = this.ownerModule[sizeType];
2428

@@ -33,6 +37,7 @@ export default class ContentFolder extends BaseFolder {
3337
parsedSize: this.parsedSize,
3438
gzipSize: this.gzipSize,
3539
brotliSize: this.brotliSize,
40+
zstdSize: this.zstdSize,
3641
inaccurateSizes: true
3742
};
3843
}

src/tree/ContentModule.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export default class ContentModule extends Module {
1919
return this.getSize('brotliSize');
2020
}
2121

22+
get zstdSize() {
23+
return this.getSize('zstdSize');
24+
}
25+
2226
getSize(sizeType) {
2327
const ownerModuleSize = this.ownerModule[sizeType];
2428

src/tree/Folder.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ export default class Folder extends BaseFolder {
2323
return this.opts.compressionAlgorithm === 'brotli' ? this.getCompressedSize('brotli') : undefined;
2424
}
2525

26+
get zstdSize() {
27+
return this.opts.compressionAlgorithm === 'zstd' ? this.getCompressedSize('zstd') : undefined;
28+
}
29+
2630
getCompressedSize(compressionAlgorithm) {
2731
const key = `_${compressionAlgorithm}Size`;
2832

@@ -71,7 +75,8 @@ export default class Folder extends BaseFolder {
7175
...super.toChartData(),
7276
parsedSize: this.parsedSize,
7377
gzipSize: this.gzipSize,
74-
brotliSize: this.brotliSize
78+
brotliSize: this.brotliSize,
79+
zstdSize: this.zstdSize
7580
};
7681
}
7782

src/tree/Module.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export default class Module extends Node {
1717
this.data.parsedSrc = value;
1818
delete this._gzipSize;
1919
delete this._brotliSize;
20+
delete this._zstdSize;
2021
}
2122

2223
get size() {
@@ -39,6 +40,10 @@ export default class Module extends Node {
3940
return this.getBrotliSize();
4041
}
4142

43+
get zstdSize() {
44+
return this.getZstdSize();
45+
}
46+
4247
getParsedSize() {
4348
return this.src ? this.src.length : undefined;
4449
}
@@ -51,6 +56,10 @@ export default class Module extends Node {
5156
return this.opts.compressionAlgorithm === 'brotli' ? this.getCompressedSize('brotli') : undefined;
5257
}
5358

59+
getZstdSize() {
60+
return this.opts.compressionAlgorithm === 'zstd' ? this.getCompressedSize('zstd') : undefined;
61+
}
62+
5463
getCompressedSize(compressionAlgorithm) {
5564
const key = `_${compressionAlgorithm}Size`;
5665
if (!(key in this)) {
@@ -78,7 +87,8 @@ export default class Module extends Node {
7887
statSize: this.size,
7988
parsedSize: this.parsedSize,
8089
gzipSize: this.gzipSize,
81-
brotliSize: this.brotliSize
90+
brotliSize: this.brotliSize,
91+
zstdSize: this.zstdSize
8292
};
8393
}
8494

0 commit comments

Comments
 (0)