Skip to content

Commit e6934f7

Browse files
committed
Add compressionAlgorithm option
1 parent 8173e3f commit e6934f7

5 files changed

Lines changed: 59 additions & 6 deletions

File tree

src/BundleAnalyzerPlugin.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class BundleAnalyzerPlugin {
1212
this.opts = {
1313
analyzerMode: 'server',
1414
analyzerHost: '127.0.0.1',
15+
compressionAlgorithm: 'gzip',
1516
reportFilename: null,
1617
reportTitle: utils.defaultTitle,
1718
defaultSizes: 'parsed',
@@ -105,6 +106,7 @@ class BundleAnalyzerPlugin {
105106
host: this.opts.analyzerHost,
106107
port: this.opts.analyzerPort,
107108
reportTitle: this.opts.reportTitle,
109+
compressionAlgorithm: this.opts.compressionAlgorithm,
108110
bundleDir: this.getBundleDirFromCompiler(),
109111
logger: this.logger,
110112
defaultSizes: this.opts.defaultSizes,
@@ -117,6 +119,7 @@ class BundleAnalyzerPlugin {
117119
async generateJSONReport(stats) {
118120
await viewer.generateJSONReport(stats, {
119121
reportFilename: path.resolve(this.compiler.outputPath, this.opts.reportFilename || 'report.json'),
122+
compressionAlgorithm: this.opts.compressionAlgorithm,
120123
bundleDir: this.getBundleDirFromCompiler(),
121124
logger: this.logger,
122125
excludeAssets: this.opts.excludeAssets
@@ -128,6 +131,7 @@ class BundleAnalyzerPlugin {
128131
openBrowser: this.opts.openAnalyzer,
129132
reportFilename: path.resolve(this.compiler.outputPath, this.opts.reportFilename || 'report.html'),
130133
reportTitle: this.opts.reportTitle,
134+
compressionAlgorithm: this.opts.compressionAlgorithm,
131135
bundleDir: this.getBundleDirFromCompiler(),
132136
logger: this.logger,
133137
defaultSizes: this.opts.defaultSizes,

src/bin/analyzer.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +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']);
1415

1516
const program = commander
1617
.version(require('../../package.json').version)
@@ -58,6 +59,12 @@ const program = commander
5859
br(`Possible values: ${[...SIZES].join(', ')}`),
5960
'parsed'
6061
)
62+
.option(
63+
'--compression-algorithm <type>',
64+
'Compression algorithm that will be used to calculate the compressed module sizes.' +
65+
br(`Possible values: ${[...COMPRESSION_ALGORITHMS].join(', ')}`),
66+
'gzip'
67+
)
6168
.option(
6269
'-O, --no-open',
6370
"Don't open report in default browser automatically."
@@ -84,6 +91,7 @@ let {
8491
report: reportFilename,
8592
title: reportTitle,
8693
defaultSizes,
94+
compressionAlgorithm,
8795
logLevel,
8896
open: openBrowser,
8997
exclude: excludeAssets
@@ -104,6 +112,9 @@ if (mode === 'server') {
104112
port = port === 'auto' ? 0 : Number(port);
105113
if (isNaN(port)) showHelp('Invalid port. Should be a number or `auto`');
106114
}
115+
if (!COMPRESSION_ALGORITHMS.has(compressionAlgorithm)) {
116+
showHelp(`Invalid compression algorithm option. Possible values are: ${[...COMPRESSION_ALGORITHMS].join(', ')}`);
117+
}
107118
if (!SIZES.has(defaultSizes)) showHelp(`Invalid default sizes option. Possible values are: ${[...SIZES].join(', ')}`);
108119

109120
bundleStatsFile = resolve(bundleStatsFile);
@@ -121,6 +132,7 @@ async function parseAndAnalyse(bundleStatsFile) {
121132
port,
122133
host,
123134
defaultSizes,
135+
compressionAlgorithm,
124136
reportTitle,
125137
bundleDir,
126138
excludeAssets,
@@ -133,13 +145,15 @@ async function parseAndAnalyse(bundleStatsFile) {
133145
reportFilename: resolve(reportFilename || 'report.html'),
134146
reportTitle,
135147
defaultSizes,
148+
compressionAlgorithm,
136149
bundleDir,
137150
excludeAssets,
138151
logger: new Logger(logLevel)
139152
});
140153
} else if (mode === 'json') {
141154
viewer.generateJSONReport(bundleStats, {
142155
reportFilename: resolve(reportFilename || 'report.json'),
156+
compressionAlgorithm,
143157
bundleDir,
144158
excludeAssets,
145159
logger: new Logger(logLevel)
@@ -159,7 +173,7 @@ function showHelp(error) {
159173
}
160174

161175
function br(str) {
162-
return `\n${' '.repeat(28)}${str}`;
176+
return `\n${' '.repeat(32)}${str}`;
163177
}
164178

165179
function array() {

src/template.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function getScript(filename, mode) {
3939
}
4040
}
4141

42-
function renderViewer({title, enableWebSocket, chartData, entrypoints, defaultSizes, mode} = {}) {
42+
function renderViewer({title, enableWebSocket, chartData, entrypoints, defaultSizes, compressionAlgorithm, mode} = {}) {
4343
return html`<!DOCTYPE html>
4444
<html>
4545
<head>
@@ -60,6 +60,7 @@ function renderViewer({title, enableWebSocket, chartData, entrypoints, defaultSi
6060
window.chartData = ${escapeJson(chartData)};
6161
window.entrypoints = ${escapeJson(entrypoints)};
6262
window.defaultSizes = ${escapeJson(defaultSizes)};
63+
window.compressionAlgorithm = ${escapeJson(compressionAlgorithm)};
6364
</script>
6465
</body>
6566
</html>`;

src/viewer.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ async function startServer(bundleStats, opts) {
3838
bundleDir = null,
3939
logger = new Logger(),
4040
defaultSizes = 'parsed',
41+
compressionAlgorithm,
4142
excludeAssets = null,
4243
reportTitle,
4344
analyzerUrl
4445
} = opts || {};
4546

46-
const analyzerOpts = {logger, excludeAssets};
47+
const analyzerOpts = {logger, excludeAssets, compressionAlgorithm};
4748

4849
let chartData = getChartData(analyzerOpts, bundleStats, bundleDir);
4950
const entrypoints = getEntrypoints(bundleStats);
@@ -63,6 +64,7 @@ async function startServer(bundleStats, opts) {
6364
chartData,
6465
entrypoints,
6566
defaultSizes,
67+
compressionAlgorithm,
6668
enableWebSocket: true
6769
});
6870
res.writeHead(200, {'Content-Type': 'text/html'});
@@ -136,10 +138,11 @@ async function generateReport(bundleStats, opts) {
136138
bundleDir = null,
137139
logger = new Logger(),
138140
defaultSizes = 'parsed',
141+
compressionAlgorithm,
139142
excludeAssets = null
140143
} = opts || {};
141144

142-
const chartData = getChartData({logger, excludeAssets}, bundleStats, bundleDir);
145+
const chartData = getChartData({logger, excludeAssets, compressionAlgorithm}, bundleStats, bundleDir);
143146
const entrypoints = getEntrypoints(bundleStats);
144147

145148
if (!chartData) return;
@@ -150,6 +153,7 @@ async function generateReport(bundleStats, opts) {
150153
chartData,
151154
entrypoints,
152155
defaultSizes,
156+
compressionAlgorithm,
153157
enableWebSocket: false
154158
});
155159
const reportFilepath = path.resolve(bundleDir || process.cwd(), reportFilename);
@@ -165,9 +169,15 @@ async function generateReport(bundleStats, opts) {
165169
}
166170

167171
async function generateJSONReport(bundleStats, opts) {
168-
const {reportFilename, bundleDir = null, logger = new Logger(), excludeAssets = null} = opts || {};
172+
const {
173+
reportFilename,
174+
bundleDir = null,
175+
logger = new Logger(),
176+
excludeAssets = null,
177+
compressionAlgorithm
178+
} = opts || {};
169179

170-
const chartData = getChartData({logger, excludeAssets}, bundleStats, bundleDir);
180+
const chartData = getChartData({logger, excludeAssets, compressionAlgorithm}, bundleStats, bundleDir);
171181

172182
if (!chartData) return;
173183

test/analyzer.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,24 @@ describe('Analyzer', function () {
248248
expect(generatedReportTitle).to.match(/^webpack-bundle-analyzer \[.* at \d{2}:\d{2}\]/u);
249249
});
250250
});
251+
252+
253+
describe('compression algorithm', function () {
254+
it('should accept --compression-algorithm brotli', async function () {
255+
generateReportFrom('with-modules-chunk.json', '--compression-algorithm brotli');
256+
expect(await getCompressionAlgorithm()).to.equal('brotli');
257+
});
258+
259+
it('should accept --compression-algorithm gzip', async function () {
260+
generateReportFrom('with-modules-chunk.json', '--compression-algorithm gzip');
261+
expect(await getCompressionAlgorithm()).to.equal('gzip');
262+
});
263+
264+
it('should default to gzip', async function () {
265+
generateReportFrom('with-modules-chunk.json');
266+
expect(await getCompressionAlgorithm()).to.equal('gzip');
267+
});
268+
});
251269
});
252270
});
253271

@@ -277,6 +295,12 @@ async function getChartData() {
277295
return await page.evaluate(() => window.chartData);
278296
}
279297

298+
async function getCompressionAlgorithm() {
299+
const page = await browser.newPage();
300+
await page.goto(`file://${__dirname}/output/report.html`);
301+
return await page.evaluate(() => window.compressionAlgorithm);
302+
}
303+
280304
function forEachChartItem(chartData, cb) {
281305
for (const item of chartData) {
282306
cb(item);

0 commit comments

Comments
 (0)