Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support output.path interpolation #304

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/BundleAnalyzerPlugin.js
Expand Up @@ -35,6 +35,7 @@ class BundleAnalyzerPlugin {

const done = (stats, callback) => {
callback = callback || (() => {});
const bundleDir = stats.compilation.getPath(stats.compilation.outputOptions.path);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea if this getPath function is a public API from webpack. And neither do I know if stats.compilation.outputOptions.path is OK to use here.


const actions = [];

Expand All @@ -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));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a huge fan in how I had to add another argument to these methods to get this working

}

if (actions.length) {
Expand Down Expand Up @@ -99,27 +100,27 @@ class BundleAnalyzerPlugin {
}
}

async startAnalyzerServer(stats) {
async startAnalyzerServer(stats, bundleDir) {
if (this.server) {
(await this.server).updateChartData(stats);
} else {
this.server = viewer.startServer(stats, {
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
});
}
}

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
Expand Down
15 changes: 15 additions & 0 deletions test/plugin.js
Expand Up @@ -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');
Copy link
Member Author

@valscion valscion Jul 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this hash is the same across different webpack versions... this might cause a very brittle test.

I don't yet have good ideas on how to figure out the hash that was used to get to the report page.

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();

Expand Down