Skip to content

Commit

Permalink
feat: extract webpackJsonp chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
j4k0xb committed Jul 23, 2023
1 parent b46716a commit ce3199c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/extractor/webpack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,40 @@ export const unpackWebpack = {
)
);

// Examples: self.webpackChunk_N_E, window.webpackJsonp
const jsonpGlobal = m.capture(
constMemberExpression(
m.identifier(m.or('self', 'window')),
m.matcher(s => (s as string).startsWith('webpack'))
)
);
// (window.webpackJsonp = window.webpackJsonp || []).push([[0], {...}])
const jsonpMatcher = m.callExpression(
constMemberExpression(
m.assignmentExpression(
'=',
jsonpGlobal,
m.logicalExpression('||', jsonpGlobal, m.arrayExpression([]))
),
'push'
),
[
m.arrayExpression(
m.anyList(
m.arrayExpression([m.numericLiteral()]), // chunkId
moduleFunctionsMatcher,
m.slice({ max: 1 }) // optional argument like [[188, 17, 16, 18]]
)
),
]
);

return {
CallExpression(path) {
if (
!webpack4Matcher.match(path.node) &&
!webpack5Matcher.match(path.node)
!webpack5Matcher.match(path.node) &&
!jsonpMatcher.match(path.node)
)
return;
path.stop();
Expand Down
21 changes: 21 additions & 0 deletions test/__snapshots__/extractor.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,27 @@ export default 1;,
}
`;

exports[`extract webpack-jsonp-chunk.js 1`] = `
WebpackBundle {
"entryId": "",
"modules": Map {
"2wwy" => WebpackModule {
"ast": const a = require("E8gZ");,
"id": "2wwy",
"isEntry": false,
"path": "./2wwy.js",
},
"E8gZ" => WebpackModule {
"ast": module.exports = 4;,
"id": "E8gZ",
"isEntry": false,
"path": "./E8gZ.js",
},
},
"type": "webpack",
}
`;

exports[`extract webpack-object.js 1`] = `
WebpackBundle {
"entryId": "386",
Expand Down
1 change: 1 addition & 0 deletions test/extractor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ test.each([
'webpack-object.js',
'webpack-esm.js',
'webpack-var-injection.js',
'webpack-jsonp-chunk.js',
'webpack-0.11.x.js',
'webpack5-object.js',
'webpack5-esm.js',
Expand Down
11 changes: 11 additions & 0 deletions test/samples/webpack-jsonp-chunk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(window.webpackJsonp = window.webpackJsonp || []).push([
[15],
{
'2wwy': function (e, t, i) {
const a = i('E8gZ');
},
E8gZ: function (e, t, i) {
e.exports = 4;
},
},
]);

0 comments on commit ce3199c

Please sign in to comment.