Skip to content

Commit

Permalink
Properly parse Webpack 5 bundle format (except concatenated entry mod…
Browse files Browse the repository at this point in the history
…ule)
  • Loading branch information
th0r committed Nov 3, 2020
1 parent b34b249 commit 7bbe89f
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 1 deletion.
61 changes: 60 additions & 1 deletion src/parseUtils.js
Expand Up @@ -18,13 +18,54 @@ function parseBundle(bundlePath) {
});

const walkState = {
locations: null
locations: null,
expressionStatementDepth: 0
};

walk.recursive(
ast,
walkState,
{
ExpressionStatement(node, state, c) {
if (state.locations) return;

state.expressionStatementDepth++;

if (
// Webpack 5 stores modules in the the top-level IIFE
state.expressionStatementDepth === 1 &&
ast.body.includes(node) &&
isIIFE(node)
) {
const fn = getIIFECallExpression(node);

if (
// It should not contain neither arguments
fn.arguments.length === 0 &&
// ...nor parameters
fn.callee.params.length === 0
) {
// Modules are stored in the very first variable as hash
const {body} = fn.callee.body;

if (
body.length &&
body[0].type === 'VariableDeclaration' &&
body[0].declarations.length &&
body[0].declarations[0].type === 'VariableDeclarator' &&
body[0].declarations[0].init.type === 'ObjectExpression'
) {
state.locations = getModulesLocations(body[0].declarations[0].init);
}
}
}

if (!state.locations) {
c(node.expression, state);
}

state.expressionStatementDepth--;
},
AssignmentExpression(node, state) {
if (state.locations) return;

Expand Down Expand Up @@ -111,6 +152,24 @@ function parseBundle(bundlePath) {
};
}

function isIIFE(node) {
return (
node.type === 'ExpressionStatement' &&
(
node.expression.type === 'CallExpression' ||
(node.expression.type === 'UnaryExpression' && node.expression.argument.type === 'CallExpression')
)
);
}

function getIIFECallExpression(node) {
if (node.expression.type === 'UnaryExpression') {
return node.expression.argument;
} else {
return node.expression;
}
}

function isModulesList(node) {
return (
isSimpleModulesList(node) ||
Expand Down
2 changes: 2 additions & 0 deletions test/.gitignore
Expand Up @@ -2,3 +2,5 @@ output

# Sandbox config
/webpack.config.js
# Output of sandbox config
/dist
1 change: 1 addition & 0 deletions test/bundles/validWebpack5LegacyBundle.js

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

7 changes: 7 additions & 0 deletions test/bundles/validWebpack5LegacyBundle.modules.json
@@ -0,0 +1,7 @@
{
"modules": {
"631": "function(o){o.exports=\"module a\"}",
"85": "function(o){o.exports=\"module a\"}",
"326": "function(o){o.exports=\"module b\"}"
}
}
1 change: 1 addition & 0 deletions test/bundles/validWebpack5ModernBundle.js

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

7 changes: 7 additions & 0 deletions test/bundles/validWebpack5ModernBundle.modules.json
@@ -0,0 +1,7 @@
{
"modules": {
"631": "r=>{r.exports=\"module a\"}",
"85": "r=>{r.exports=\"module a\"}",
"326": "r=>{r.exports=\"module b\"}"
}
}

0 comments on commit 7bbe89f

Please sign in to comment.