Skip to content

Commit 141f515

Browse files
committed
Add compressedSize plugin option
1 parent a1a2dbc commit 141f515

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

src/BundleAnalyzerPlugin.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class BundleAnalyzerPlugin {
104104
host: this.opts.analyzerHost,
105105
port: this.opts.analyzerPort,
106106
reportTitle: this.opts.reportTitle,
107+
compressedSize: this.opts.compressedSize,
107108
bundleDir: this.getBundleDirFromCompiler(),
108109
logger: this.logger,
109110
defaultSizes: this.opts.defaultSizes,
@@ -115,6 +116,7 @@ class BundleAnalyzerPlugin {
115116
async generateJSONReport(stats) {
116117
await viewer.generateJSONReport(stats, {
117118
reportFilename: path.resolve(this.compiler.outputPath, this.opts.reportFilename || 'report.json'),
119+
compressedSize: this.opts.compressedSize,
118120
bundleDir: this.getBundleDirFromCompiler(),
119121
logger: this.logger,
120122
excludeAssets: this.opts.excludeAssets
@@ -126,6 +128,7 @@ class BundleAnalyzerPlugin {
126128
openBrowser: this.opts.openAnalyzer,
127129
reportFilename: path.resolve(this.compiler.outputPath, this.opts.reportFilename || 'report.html'),
128130
reportTitle: this.opts.reportTitle,
131+
compressedSize: this.opts.compressedSize,
129132
bundleDir: this.getBundleDirFromCompiler(),
130133
logger: this.logger,
131134
defaultSizes: this.opts.defaultSizes,

src/viewer.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ async function startServer(bundleStats, opts) {
3939
logger = new Logger(),
4040
defaultSizes = 'parsed',
4141
excludeAssets = null,
42-
reportTitle
42+
reportTitle,
43+
compressedSize
4344
} = opts || {};
4445

45-
const analyzerOpts = {logger, excludeAssets};
46+
const analyzerOpts = {logger, excludeAssets, compressedSize};
4647

4748
let chartData = getChartData(analyzerOpts, bundleStats, bundleDir);
4849

@@ -126,13 +127,14 @@ async function generateReport(bundleStats, opts) {
126127
openBrowser = true,
127128
reportFilename,
128129
reportTitle,
130+
compressedSize,
129131
bundleDir = null,
130132
logger = new Logger(),
131133
defaultSizes = 'parsed',
132134
excludeAssets = null
133135
} = opts || {};
134136

135-
const chartData = getChartData({logger, excludeAssets}, bundleStats, bundleDir);
137+
const chartData = getChartData({logger, excludeAssets, compressedSize}, bundleStats, bundleDir);
136138

137139
if (!chartData) return;
138140

@@ -156,9 +158,10 @@ async function generateReport(bundleStats, opts) {
156158
}
157159

158160
async function generateJSONReport(bundleStats, opts) {
159-
const {reportFilename, bundleDir = null, logger = new Logger(), excludeAssets = null} = opts || {};
161+
const {reportFilename, bundleDir = null, logger = new Logger(), excludeAssets = null,
162+
compressedSize} = opts || {};
160163

161-
const chartData = getChartData({logger, excludeAssets}, bundleStats, bundleDir);
164+
const chartData = getChartData({logger, excludeAssets, compressedSize}, bundleStats, bundleDir);
162165

163166
if (!chartData) return;
164167

test/plugin.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,21 @@ describe('Plugin', function () {
170170
expect(error).to.equal(reportTitleError);
171171
});
172172
});
173+
174+
describe('compressedSize', function () {
175+
it('should support a function value', async function () {
176+
const config = makeWebpackConfig({
177+
analyzerOpts: {
178+
compressedSize: (x) => x.length * 10
179+
}
180+
});
181+
await webpackCompile(config, '4.44.2');
182+
await expectValidReport({
183+
parsedSize: 1311,
184+
gzipSize: 13110
185+
});
186+
});
187+
});
173188
});
174189

175190
async function expectValidReport(opts) {

0 commit comments

Comments
 (0)