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

Fix parsing webpack 4's chunks #159

Merged
merged 3 commits into from
Mar 1, 2018
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ _Note: Gaps between patch versions are faulty, broken or test releases._

<!-- Add changelog entries for new changes under this section -->

* **Improvement**
* Add support for parsing Webpack 4's chunked modules ([#159](https://github.com/webpack-contrib/webpack-bundle-analyzer/pull/159), [@jdelStrother](https://github.com/jdelStrother))

## 2.11.0

* **Improvement**
Expand Down
30 changes: 30 additions & 0 deletions src/parseUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ function parseBundle(bundlePath) {
return;
}

// Additional bundles with webpack 4 are loaded with:
// (window.webpackJsonp=window.webpackJsonp||[]).push([[chunkId], [<module>, <module>], [[optional_entries]]]);
if (
isWindowPropertyPushExpression(node) &&
args.length === 1 &&
isArgumentContainingChunkIdsAndModulesList(args[0])
) {
state.locations = getModulesLocationFromFunctionArgument(args[0].elements[1]);
return;
}

// Walking into arguments because some of plugins (e.g. `DedupePlugin`) or some Webpack
// features (e.g. `umd` library output) can wrap modules list into additional IIFE.
_.each(args, arg => c(arg, state));
Expand Down Expand Up @@ -115,6 +126,18 @@ function isArgumentContainsModulesList(arg) {
return false;
}

function isArgumentContainingChunkIdsAndModulesList(arg) {
if (
arg.type === 'ArrayExpression' &&
arg.elements.length >= 2 &&
isArgumentContainsChunkIds(arg.elements[0]) &&
isArgumentContainsModulesList(arg.elements[1])
) {
return true;
}
return false;
}

function isArgumentArrayConcatContainingChunks(arg) {
if (
arg.type === 'CallExpression' &&
Expand All @@ -141,6 +164,13 @@ function isArgumentArrayConcatContainingChunks(arg) {
return false;
}

function isWindowPropertyPushExpression(node) {
return node.callee.type === 'MemberExpression' &&
node.callee.property.name === 'push' &&
node.callee.object.type === 'AssignmentExpression' &&
node.callee.object.left.object.name === 'window';
}

function isModuleWrapper(node) {
return (
// It's an anonymous function expression that wraps module
Expand Down
1 change: 1 addition & 0 deletions test/bundles/validWebpack4Chunk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(window.webpackJsonp=window.webpackJsonp||[]).push([[27],{"57iH":function(e,n,t){console.log("hello world")}}]);
5 changes: 5 additions & 0 deletions test/bundles/validWebpack4Chunk.modules.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"modules": {
"57iH": "function(e,n,t){console.log(\"hello world\")}"
}
}
1 change: 1 addition & 0 deletions test/bundles/validWebpack4ChunkAndEntryPoint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(window.webpackJsonp=window.webpackJsonp||[]).push([[27],{"57iH":function(e,n,t){console.log("hello world")}},[["57iH",19,24,25]]]);
5 changes: 5 additions & 0 deletions test/bundles/validWebpack4ChunkAndEntryPoint.modules.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"modules": {
"57iH": "function(e,n,t){console.log(\"hello world\")}"
}
}