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

Allow overwriting of bundleDir via bundleDirOption from plugin option. #276

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
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -59,6 +59,7 @@ new BundleAnalyzerPlugin(options?: object)
|**`analyzerMode`**|One of: `server`, `static`, `disabled`|Default: `server`. In `server` mode analyzer will start HTTP server to show bundle report. In `static` mode single HTML file with bundle report will be generated. In `disabled` mode you can use this plugin to just generate Webpack Stats JSON file by setting `generateStatsFile` to `true`. |
|**`analyzerHost`**|`{String}`|Default: `127.0.0.1`. Host that will be used in `server` mode to start HTTP server.|
|**`analyzerPort`**|`{Number}`|Default: `8888`. Port that will be used in `server` mode to start HTTP server.|
|**`bundleDirOption`**|`{null|Boolean|String}`|Default: `null`. Option defining the directory containing all generated bundles. If `null`, make `bundleDir` to be `compiler.outputPath`, or `null` if webpack-dev-server is used. otherwise if `false`, `bundleDir` will be `null`. Else, provided `bundleDirOption` will be used as `bundleDir` |
|**`reportFilename`**|`{String}`|Default: `report.html`. Path to bundle report file that will be generated in `static` mode. It can be either an absolute path or a path relative to a bundle output directory (which is output.path in webpack config).|
|**`defaultSizes`**|One of: `stat`, `parsed`, `gzip`|Default: `parsed`. Module sizes to show in report by default. [Size definitions](#size-definitions) section describes what these values mean.|
|**`openAnalyzer`**|`{Boolean}`|Default: `true`. Automatically open report in default browser.|
Expand Down
18 changes: 16 additions & 2 deletions src/BundleAnalyzerPlugin.js
Expand Up @@ -13,6 +13,7 @@ class BundleAnalyzerPlugin {
analyzerMode: 'server',
analyzerHost: '127.0.0.1',
analyzerPort: 8888,
bundleDirOption: null,
reportFilename: 'report.html',
defaultSizes: 'parsed',
openAnalyzer: true,
Expand Down Expand Up @@ -108,7 +109,7 @@ class BundleAnalyzerPlugin {
openBrowser: this.opts.openAnalyzer,
host: this.opts.analyzerHost,
port: this.opts.analyzerPort,
bundleDir: this.getBundleDirFromCompiler(),
bundleDir: this.getBundleDir(this.opts.bundleDirOption),
logger: this.logger,
defaultSizes: this.opts.defaultSizes,
excludeAssets: this.opts.excludeAssets
Expand All @@ -120,13 +121,26 @@ class BundleAnalyzerPlugin {
await viewer.generateReport(stats, {
openBrowser: this.opts.openAnalyzer,
reportFilename: path.resolve(this.compiler.outputPath, this.opts.reportFilename),
bundleDir: this.getBundleDirFromCompiler(),
bundleDir: this.getBundleDir(this.opts.bundleDirOption),
logger: this.logger,
defaultSizes: this.opts.defaultSizes,
excludeAssets: this.opts.excludeAssets
});
}

getBundleDir(bundleDirOption) {
// bundleDirOption is not provided
if (bundleDirOption == null) {
return this.getBundleDirFromCompiler();

// bundleDirOption is explicitly turned off
} else if (bundleDirOption === false) {
return null;
}

return bundleDirOption;
}

getBundleDirFromCompiler() {
return (this.compiler.outputFileSystem.constructor.name === 'MemoryFileSystem') ? null : this.compiler.outputPath;
}
Expand Down
58 changes: 58 additions & 0 deletions test/plugin.js
Expand Up @@ -71,6 +71,64 @@ describe('Plugin', function () {
expect(_.map(chartData, 'label')).to.deep.equal(['bundle.js']);
});
});

describe('bundleDirOption', function () {
it('should report parsedSize and gzipSize when bundleDirOption is not provided', async function () {
const config = makeWebpackConfig({
analyzerOpts: {
}
});

await webpackCompile(config);

const chartData = await getChartDataFromReport();
expect(
chartData[0].groups.every(group => (
('parsedSize' in group)
&& ('gzipSize' in group)
&& ('statSize' in group)
))
).to.be.true;
});

it('should report parsedSize and gzipSize when bundleDirOption is null', async function () {
const config = makeWebpackConfig({
analyzerOpts: {
bundleDirOption: null
}
});

await webpackCompile(config);

const chartData = await getChartDataFromReport();
expect(
chartData[0].groups.every(group => (
('parsedSize' in group)
&& ('gzipSize' in group)
&& ('statSize' in group)
))
).to.be.true;
});

it('should report statSize only bundleDirOption is false', async function () {
const config = makeWebpackConfig({
analyzerOpts: {
bundleDirOption: false
}
});

await webpackCompile(config);

const chartData = await getChartDataFromReport();
expect(
chartData[0].groups.every(group => (
!group.parsedSize
&& !group.gzipSize
&& group.statSize
))
).to.be.true;
});
});
});
});

Expand Down