Skip to content

Commit

Permalink
Revert "refactor: separate webpack 4 and 5 matching"
Browse files Browse the repository at this point in the history
This reverts commit 1d14033.
  • Loading branch information
j4k0xb committed Jan 13, 2024
1 parent 1f137d6 commit d8a2381
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
loaded: false,
exports: {}
};
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
__webpack_modules__[moduleId].call(module.exports, module, module.exports, __webpack_require__);
module.loaded = true;
return module.exports;
}
Expand Down
50 changes: 48 additions & 2 deletions packages/webcrack/src/unpack/webpack/common-matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,56 @@ export type FunctionPath = NodePath<
| (t.ArrowFunctionExpression & { body: t.BlockStatement })
>;

/**
* @returns
* - `webpackRequire`: A Matcher for `function __webpack_require__(moduleId) { ... }`
* - `containerId`: A matcher for e.g. `__webpack_modules__` that has to be captured before `webpackRequire` is matched
*/
export function webpackRequireFunctionMatcher() {
// Example: __webpack_modules__
const containerId = m.capture(m.identifier());
const webpackRequire = m.capture(
m.functionDeclaration(
m.identifier(), // __webpack_require__
[m.identifier()], // moduleId
m.blockStatement(
m.anyList(
m.zeroOrMore(),
m.expressionStatement(
m.callExpression(
m.or(
// Example (webpack 0.11.x): __webpack_modules__[moduleId].call(null, module, module.exports, __webpack_require__);
// Example (webpack 4): __webpack_modules__[moduleId].call(module.exports, module, module.exports, __webpack_require__);
constMemberExpression(
m.memberExpression(
m.fromCapture(containerId),
m.identifier(),
true,
),
'call',
),
// Example (webpack 5): __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
m.memberExpression(
m.fromCapture(containerId),
m.identifier(),
true,
),
),
),
),
m.zeroOrMore(),
),
),
),
);

return { webpackRequire, containerId };
}

/**
* Matches
* - `[,,function (module, exports, require) {...}, ...]` where the indexes are the module ids
* - or `{0: function (module, exports, require) {...}, ...}` where the keys are the module ids
* - `[,,function (module, exports, require) {...}, ...]` where the index is the module ID
* - or `{0: function (module, exports, require) {...}, ...}` where the key is the module ID
*/
export function modulesContainerMatcher(): m.CapturedMatcher<
t.ArrayExpression | t.ObjectExpression
Expand Down
32 changes: 3 additions & 29 deletions packages/webcrack/src/unpack/webpack/unpack-webpack-4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import type * as t from '@babel/types';
import * as m from '@codemod/matchers';
import type { Bundle } from '..';
import type { Transform } from '../../ast-utils';
import { constMemberExpression, renameFast } from '../../ast-utils';
import { renameFast } from '../../ast-utils';
import { WebpackBundle } from './bundle';
import {
findAssignedEntryId,
findRequiredEntryId,
getModuleFunctions,
modulesContainerMatcher,
webpackRequireFunctionMatcher,
} from './common-matchers';
import { WebpackModule } from './module';

Expand All @@ -25,34 +26,7 @@ export default {
name: 'unpack-webpack-4',
tags: ['unsafe'],
visitor(options = { bundle: undefined }) {
// Example: __webpack_modules__
const containerId = m.capture(m.identifier());
const webpackRequire = m.capture(
m.functionDeclaration(
m.identifier(), // __webpack_require__
[m.identifier()], // moduleId
m.blockStatement(
m.anyList(
m.zeroOrMore(),
// Example (webpack 0.11.x): __webpack_modules__[moduleId].call(null, module, module.exports, __webpack_require__);
// Example (webpack 4): __webpack_modules__[moduleId].call(module.exports, module, module.exports, __webpack_require__);
m.expressionStatement(
m.callExpression(
constMemberExpression(
m.memberExpression(
m.fromCapture(containerId),
m.identifier(),
true,
),
'call',
),
),
),
m.zeroOrMore(),
),
),
),
);
const { webpackRequire, containerId } = webpackRequireFunctionMatcher();
const container = modulesContainerMatcher();

const matcher = m.callExpression(
Expand Down
28 changes: 3 additions & 25 deletions packages/webcrack/src/unpack/webpack/unpack-webpack-5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
findAssignedEntryId,
getModuleFunctions,
modulesContainerMatcher,
webpackRequireFunctionMatcher,
} from './common-matchers';
import { WebpackModule } from './module';

Expand All @@ -28,38 +29,15 @@ export default {
tags: ['unsafe'],
scope: true,
visitor(options = { bundle: undefined }) {
// Example: __webpack_modules__
const modulesId = m.capture(m.identifier());
const webpackRequire = m.capture(
m.functionDeclaration(
m.identifier(), // __webpack_require__
[m.identifier()], // moduleId
m.blockStatement(
m.anyList(
m.zeroOrMore(),
// Example: __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
m.expressionStatement(
m.callExpression(
m.memberExpression(
m.fromCapture(modulesId),
m.identifier(),
true,
),
),
),
m.zeroOrMore(),
),
),
),
);
const { webpackRequire, containerId } = webpackRequireFunctionMatcher();
const container = modulesContainerMatcher();

const matcher = m.blockStatement(
m.anyList(
m.zeroOrMore(),
// Example: var __webpack_modules__ = { ... };
m.variableDeclaration(undefined, [
m.variableDeclarator(modulesId, container),
m.variableDeclarator(containerId, container),
]),
m.zeroOrMore(),
webpackRequire,
Expand Down

0 comments on commit d8a2381

Please sign in to comment.