Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/bright-bundles-parse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"webpack-bundle-analyzer": patch
---

Parse Webpack 5 module maps nested in UMD wrappers, using bundle module IDs and Webpack runtime structure to choose among IIFE candidates.
37 changes: 33 additions & 4 deletions src/analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,20 @@ function getViewerData(bundleStats, bundleDir, opts) {
);
});

const rootModules = getBundleModules(bundleStats);
const rootModulesByChunk = getModulesByChunk(rootModules);
/** @type {Map<StatsAsset, StatsModule[]>} */
const rootAssetModules = new Map();

for (const statAsset of bundleStats.assets) {
if (!statAsset.isChild) {
rootAssetModules.set(
statAsset,
getAssetModulesByChunk(statAsset, rootModulesByChunk),
);
}
}

// Trying to parse bundle assets and get real module sizes if `bundleDir` is provided
/** @type {Record<string, { src: string, runtimeSrc: string }> | null} */
let bundlesSources = null;
Expand All @@ -329,11 +343,27 @@ function getViewerData(bundleStats, bundleDir, opts) {

for (const statAsset of bundleStats.assets) {
const assetFile = path.join(bundleDir, statAsset.name);
const expectedModuleIds = statAsset.isChild
? []
: /** @type {StatsModule[]} */ (rootAssetModules.get(statAsset)).reduce(
(moduleIds, statsModule) => {
if (
typeof statsModule.id === "string" ||
typeof statsModule.id === "number"
) {
moduleIds.push(statsModule.id);
}

return moduleIds;
},
/** @type {(string | number)[]} */ ([]),
);
let bundleInfo;

try {
bundleInfo = parseBundle(assetFile, {
sourceType: statAsset.info.javascriptModule ? "module" : "script",
expectedModuleIds,
});
} catch (err) {
const msg =
Expand Down Expand Up @@ -364,9 +394,6 @@ function getViewerData(bundleStats, bundleDir, opts) {

/** @typedef {{ size: number, parsedSize?: number, gzipSize?: number, brotliSize?: number, zstdSize?: number, modules: StatsModule[], tree: Folder }} Asset */

const rootModules = getBundleModules(bundleStats);
const rootModulesByChunk = getModulesByChunk(rootModules);

const assets = bundleStats.assets.reduce((result, statAsset) => {
/** @type {StatsModule[]} */
let assetModules;
Expand All @@ -383,7 +410,9 @@ function getViewerData(bundleStats, bundleDir, opts) {
assetHasModule(statAsset, statModule),
);
} else {
assetModules = getAssetModulesByChunk(statAsset, rootModulesByChunk);
assetModules = /** @type {StatsModule[]} */ (
rootAssetModules.get(statAsset)
);
}

const asset = (result[statAsset.name] = /** @type {Asset} */ ({
Expand Down
Loading
Loading