Skip to content

Commit 09363f0

Browse files
committed
Add compressedSizeLabel option
1 parent 141f515 commit 09363f0

6 files changed

Lines changed: 37 additions & 10 deletions

File tree

client/components/ModulesTreemap.jsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ import Search from './Search';
1818
import {store} from '../store';
1919
import ModulesList from './ModulesList';
2020

21-
const SIZE_SWITCH_ITEMS = [
22-
{label: 'Stat', prop: 'statSize'},
23-
{label: 'Parsed', prop: 'parsedSize'},
24-
{label: 'Gzipped', prop: 'gzipSize'}
25-
];
21+
function allSizeSwitchItems() {
22+
return [
23+
{label: 'Stat', prop: 'statSize'},
24+
{label: 'Parsed', prop: 'parsedSize'},
25+
{label: window.compressedSizeLabel, prop: 'gzipSize'}
26+
];
27+
}
2628

2729
@observer
2830
export default class ModulesTreemap extends Component {
@@ -138,7 +140,7 @@ export default class ModulesTreemap extends Component {
138140
renderModuleSize(module, sizeType) {
139141
const sizeProp = `${sizeType}Size`;
140142
const size = module[sizeProp];
141-
const sizeLabel = SIZE_SWITCH_ITEMS.find(item => item.prop === sizeProp).label;
143+
const sizeLabel = allSizeSwitchItems().find(item => item.prop === sizeProp).label;
142144
const isActive = (store.activeSize === sizeProp);
143145

144146
return (typeof size === 'number') ?
@@ -162,7 +164,8 @@ export default class ModulesTreemap extends Component {
162164
};
163165

164166
@computed get sizeSwitchItems() {
165-
return store.hasParsedSizes ? SIZE_SWITCH_ITEMS : SIZE_SWITCH_ITEMS.slice(0, 1);
167+
const items = allSizeSwitchItems();
168+
return store.hasParsedSizes ? items : items.slice(0, 1);
166169
}
167170

168171
@computed get activeSizeItem() {

src/BundleAnalyzerPlugin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class BundleAnalyzerPlugin {
105105
port: this.opts.analyzerPort,
106106
reportTitle: this.opts.reportTitle,
107107
compressedSize: this.opts.compressedSize,
108+
compressedSizeLabel: this.opts.compressedSizeLabel,
108109
bundleDir: this.getBundleDirFromCompiler(),
109110
logger: this.logger,
110111
defaultSizes: this.opts.defaultSizes,

src/bin/analyzer.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ const program = commander
5858
br(`Possible values: ${[...SIZES].join(', ')}`),
5959
'parsed'
6060
)
61+
.option(
62+
'--compressed-size-label <label>',
63+
'String to use for the compressed module size option.',
64+
'Gzipped'
65+
)
6166
.option(
6267
'-O, --no-open',
6368
"Don't open report in default browser automatically."
@@ -83,6 +88,7 @@ let {
8388
report: reportFilename,
8489
title: reportTitle,
8590
defaultSizes,
91+
compressedSizeLabel,
8692
logLevel,
8793
open: openBrowser,
8894
exclude: excludeAssets,
@@ -125,6 +131,7 @@ if (mode === 'server') {
125131
port,
126132
host,
127133
defaultSizes,
134+
compressedSizeLabel,
128135
reportTitle,
129136
bundleDir,
130137
excludeAssets,
@@ -136,6 +143,7 @@ if (mode === 'server') {
136143
reportFilename: resolve(reportFilename || 'report.html'),
137144
reportTitle,
138145
defaultSizes,
146+
compressedSizeLabel,
139147
bundleDir,
140148
excludeAssets,
141149
logger: new Logger(logLevel)
@@ -156,7 +164,7 @@ function showHelp(error) {
156164
}
157165

158166
function br(str) {
159-
return `\n${' '.repeat(28)}${str}`;
167+
return `\n${' '.repeat(33)}${str}`;
160168
}
161169

162170
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, defaultSizes, mode} = {}) {
42+
function renderViewer({title, enableWebSocket, chartData, defaultSizes, compressedSizeLabel, mode} = {}) {
4343
return html`<!DOCTYPE html>
4444
<html>
4545
<head>
@@ -59,6 +59,7 @@ function renderViewer({title, enableWebSocket, chartData, defaultSizes, mode} =
5959
<script>
6060
window.chartData = ${escapeJson(chartData)};
6161
window.defaultSizes = ${escapeJson(defaultSizes)};
62+
window.compressedSizeLabel = ${escapeJson(compressedSizeLabel)};
6263
</script>
6364
</body>
6465
</html>`;

src/viewer.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ async function startServer(bundleStats, opts) {
4040
defaultSizes = 'parsed',
4141
excludeAssets = null,
4242
reportTitle,
43-
compressedSize
43+
compressedSize,
44+
compressedSizeLabel = 'Gzipped'
4445
} = opts || {};
4546

4647
const analyzerOpts = {logger, excludeAssets, compressedSize};
@@ -61,6 +62,7 @@ async function startServer(bundleStats, opts) {
6162
title: resolveTitle(reportTitle),
6263
chartData,
6364
defaultSizes,
65+
compressedSizeLabel,
6466
enableWebSocket: true
6567
});
6668
res.writeHead(200, {'Content-Type': 'text/html'});
@@ -128,6 +130,7 @@ async function generateReport(bundleStats, opts) {
128130
reportFilename,
129131
reportTitle,
130132
compressedSize,
133+
compressedSizeLabel = 'Gzipped',
131134
bundleDir = null,
132135
logger = new Logger(),
133136
defaultSizes = 'parsed',
@@ -143,6 +146,7 @@ async function generateReport(bundleStats, opts) {
143146
title: resolveTitle(reportTitle),
144147
chartData,
145148
defaultSizes,
149+
compressedSizeLabel,
146150
enableWebSocket: false
147151
});
148152
const reportFilepath = path.resolve(bundleDir || process.cwd(), reportFilename);

test/analyzer.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ describe('Analyzer', function () {
199199
expect(generatedReportTitle).to.match(/^webpack-bundle-analyzer \[.* at \d{2}:\d{2}\]/u);
200200
});
201201
});
202+
203+
it('should take the --compressed-size-label option', async function () {
204+
generateReportFrom('with-modules-chunk.json', '--compressed-size-label "A size label"');
205+
expect(await getCompressedSizeLabel()).to.equal('A size label');
206+
});
202207
});
203208
});
204209

@@ -224,6 +229,11 @@ async function getChartData() {
224229
return await nightmare.goto(`file://${__dirname}/output/report.html`).evaluate(() => window.chartData);
225230
}
226231

232+
async function getCompressedSizeLabel() {
233+
return await nightmare.goto(`file://${__dirname}/output/report.html`).evaluate(
234+
() => window.compressedSizeLabel);
235+
}
236+
227237
function forEachChartItem(chartData, cb) {
228238
for (const item of chartData) {
229239
cb(item);

0 commit comments

Comments
 (0)