Skip to content

Commit

Permalink
Allowing child compilations generated by loaders like worker-loader t…
Browse files Browse the repository at this point in the history
…o be added to the bundle analyzer charts.
  • Loading branch information
masterkidan committed Aug 20, 2020
1 parent b618f65 commit 6f388d9
Show file tree
Hide file tree
Showing 5 changed files with 1,162 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,24 @@ function getViewerData(bundleStats, bundleDir, opts) {

// Sometimes all the information is located in `children` array (e.g. problem in #10)
if (_.isEmpty(bundleStats.assets) && !_.isEmpty(bundleStats.children)) {
const {children} = bundleStats;
bundleStats = bundleStats.children[0];
// Sometimes if there are additional child chunks produced add them as child assets,
// leave the 1st one as that is considered the 'root' asset.
for (let i = 1; i < children.length; i++) {
bundleStats.children[i].assets.forEach((asset) => {
asset.isChild = true;
bundleStats.assets.push(asset);
});
}
} else if (!_.isEmpty(bundleStats.children)) {
// Sometimes if there are additional child chunks produced add them as child assets
bundleStats.children.forEach((child) => {
child.assets.forEach((asset) => {
asset.isChild = true;
bundleStats.assets.push(asset);
});
});
}

// Picking only `*.js or *.mjs` assets from bundle that has non-empty `chunks` array
Expand Down Expand Up @@ -70,8 +87,11 @@ function getViewerData(bundleStats, bundleDir, opts) {
}
}

const modules = getBundleModules(bundleStats);
const assets = _.transform(bundleStats.assets, (result, statAsset) => {
// If asset is a childAsset, then calculate appropriate bundle modules by looking through stats.children
const modules = statAsset.isChild ?
getBundleModules(getChildAssetBundles(bundleStats, statAsset.name)) :
getBundleModules(bundleStats);
const asset = result[statAsset.name] = _.pick(statAsset, 'size');

if (bundlesSources && _.has(bundlesSources, statAsset.name)) {
Expand Down Expand Up @@ -113,6 +133,15 @@ function readStatsFromFile(filename) {
);
}

function getChildAssetBundles(bundleStats, assetName) {
return _.find(bundleStats.children, (c) =>
_(c.assetsByChunkName)
.values()
.flatten()
.includes(assetName)
);
}

function getBundleModules(bundleStats) {
return _(bundleStats.chunks)
.map('modules')
Expand Down
8 changes: 8 additions & 0 deletions test/analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ describe('Analyzer', function () {
await expectValidReport();
});

it('should generate report containing worker bundles', async function () {
generateReportFrom('with-worker-loader/stats.json');
const chartData = await getChartData();
expect(chartData[1]).to.containSubset({
label: 'bundle.worker.js'
});
});

it('should support stats files with modules inside `chunks` array', async function () {
generateReportFrom('with-modules-in-chunks/stats.json');
const chartData = await getChartData();
Expand Down
1 change: 1 addition & 0 deletions test/stats/with-worker-loader/bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions test/stats/with-worker-loader/bundle.worker.js

Large diffs are not rendered by default.

Loading

0 comments on commit 6f388d9

Please sign in to comment.