From a5ce4aed5df639d774f76509f76c1b1e0911548d Mon Sep 17 00:00:00 2001 From: Vesa Laakso Date: Tue, 30 Jul 2019 21:19:55 +0300 Subject: [PATCH] Support output.path interpolation --- src/BundleAnalyzerPlugin.js | 15 ++++++++------- test/plugin.js | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/BundleAnalyzerPlugin.js b/src/BundleAnalyzerPlugin.js index 54c4c283..5f52a43d 100644 --- a/src/BundleAnalyzerPlugin.js +++ b/src/BundleAnalyzerPlugin.js @@ -35,6 +35,7 @@ class BundleAnalyzerPlugin { const done = (stats, callback) => { callback = callback || (() => {}); + const bundleDir = stats.compilation.getPath(stats.compilation.outputOptions.path); const actions = []; @@ -48,9 +49,9 @@ class BundleAnalyzerPlugin { } if (this.opts.analyzerMode === 'server') { - actions.push(() => this.startAnalyzerServer(stats.toJson())); + actions.push(() => this.startAnalyzerServer(stats.toJson(), bundleDir)); } else if (this.opts.analyzerMode === 'static') { - actions.push(() => this.generateStaticReport(stats.toJson())); + actions.push(() => this.generateStaticReport(stats.toJson(), bundleDir)); } if (actions.length) { @@ -99,7 +100,7 @@ class BundleAnalyzerPlugin { } } - async startAnalyzerServer(stats) { + async startAnalyzerServer(stats, bundleDir) { if (this.server) { (await this.server).updateChartData(stats); } else { @@ -107,7 +108,7 @@ class BundleAnalyzerPlugin { openBrowser: this.opts.openAnalyzer, host: this.opts.analyzerHost, port: this.opts.analyzerPort, - bundleDir: this.getBundleDirFromCompiler(), + bundleDir: bundleDir || this.getBundleDirFromCompiler(), logger: this.logger, defaultSizes: this.opts.defaultSizes, excludeAssets: this.opts.excludeAssets @@ -115,11 +116,11 @@ class BundleAnalyzerPlugin { } } - async generateStaticReport(stats) { + async generateStaticReport(stats, bundleDir) { await viewer.generateReport(stats, { openBrowser: this.opts.openAnalyzer, - reportFilename: path.resolve(this.compiler.outputPath, this.opts.reportFilename), - bundleDir: this.getBundleDirFromCompiler(), + reportFilename: path.resolve(bundleDir || this.compiler.outputPath, this.opts.reportFilename), + bundleDir: bundleDir || this.getBundleDirFromCompiler(), logger: this.logger, defaultSizes: this.opts.defaultSizes, excludeAssets: this.opts.excludeAssets diff --git a/test/plugin.js b/test/plugin.js index 4e24d029..64f72b47 100644 --- a/test/plugin.js +++ b/test/plugin.js @@ -47,6 +47,21 @@ describe('Plugin', function () { }); }); + it('should support webpack config with interpolation in `output.path`', async function () { + const config = makeWebpackConfig(); + config.output.path += '/[hash]'; + + await webpackCompile(config); + + const chartData = await getChartDataFromReport('7e56a1f8a54acedcc9ce/report.html'); + expect(chartData[0]).to.containSubset({ + label: 'bundle.js', + statSize: 141, + parsedSize: 1311, + gzipSize: 342 + }); + }); + it('should support webpack config with `multi` module', async function () { const config = makeWebpackConfig();