Skip to content

Commit 7bbe89f

Browse files
committed
Properly parse Webpack 5 bundle format (except concatenated entry module)
1 parent b34b249 commit 7bbe89f

6 files changed

+78
-1
lines changed

src/parseUtils.js

+60-1
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,54 @@ function parseBundle(bundlePath) {
1818
});
1919

2020
const walkState = {
21-
locations: null
21+
locations: null,
22+
expressionStatementDepth: 0
2223
};
2324

2425
walk.recursive(
2526
ast,
2627
walkState,
2728
{
29+
ExpressionStatement(node, state, c) {
30+
if (state.locations) return;
31+
32+
state.expressionStatementDepth++;
33+
34+
if (
35+
// Webpack 5 stores modules in the the top-level IIFE
36+
state.expressionStatementDepth === 1 &&
37+
ast.body.includes(node) &&
38+
isIIFE(node)
39+
) {
40+
const fn = getIIFECallExpression(node);
41+
42+
if (
43+
// It should not contain neither arguments
44+
fn.arguments.length === 0 &&
45+
// ...nor parameters
46+
fn.callee.params.length === 0
47+
) {
48+
// Modules are stored in the very first variable as hash
49+
const {body} = fn.callee.body;
50+
51+
if (
52+
body.length &&
53+
body[0].type === 'VariableDeclaration' &&
54+
body[0].declarations.length &&
55+
body[0].declarations[0].type === 'VariableDeclarator' &&
56+
body[0].declarations[0].init.type === 'ObjectExpression'
57+
) {
58+
state.locations = getModulesLocations(body[0].declarations[0].init);
59+
}
60+
}
61+
}
62+
63+
if (!state.locations) {
64+
c(node.expression, state);
65+
}
66+
67+
state.expressionStatementDepth--;
68+
},
2869
AssignmentExpression(node, state) {
2970
if (state.locations) return;
3071

@@ -111,6 +152,24 @@ function parseBundle(bundlePath) {
111152
};
112153
}
113154

155+
function isIIFE(node) {
156+
return (
157+
node.type === 'ExpressionStatement' &&
158+
(
159+
node.expression.type === 'CallExpression' ||
160+
(node.expression.type === 'UnaryExpression' && node.expression.argument.type === 'CallExpression')
161+
)
162+
);
163+
}
164+
165+
function getIIFECallExpression(node) {
166+
if (node.expression.type === 'UnaryExpression') {
167+
return node.expression.argument;
168+
} else {
169+
return node.expression;
170+
}
171+
}
172+
114173
function isModulesList(node) {
115174
return (
116175
isSimpleModulesList(node) ||

test/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ output
22

33
# Sandbox config
44
/webpack.config.js
5+
# Output of sandbox config
6+
/dist

test/bundles/validWebpack5LegacyBundle.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"modules": {
3+
"631": "function(o){o.exports=\"module a\"}",
4+
"85": "function(o){o.exports=\"module a\"}",
5+
"326": "function(o){o.exports=\"module b\"}"
6+
}
7+
}

test/bundles/validWebpack5ModernBundle.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"modules": {
3+
"631": "r=>{r.exports=\"module a\"}",
4+
"85": "r=>{r.exports=\"module a\"}",
5+
"326": "r=>{r.exports=\"module b\"}"
6+
}
7+
}

0 commit comments

Comments
 (0)