fix(unpack-webpack): fix TypeError in extractByRegex and broaden IIFE regex coverage for more bundle forms

Former-commit-id: a5d44f8ace6fdd9c5440af3896b5c2bd0ed0ef0e
This commit is contained in:
反编译工作区
2026-04-29 11:55:42 +08:00
parent 438a323038
commit 7ba980f935
+11 -4
View File
@@ -17,12 +17,17 @@ function unpackWebpackBundle(bundlePath, outputDir) {
// 模式1: __webpack_modules__ = { 123: function(module, exports, __webpack_require__) {...}, ... }
const modulesAssignMatch = code.match(/__webpack_modules__\s*=\s*(\{[\s\S]*?\});/);
// 模式2: (function(modules) { ... })({ 123: function(...) {...}, ... })
const iifeMatch = code.match(/\(function\(\s*\w+\s*\)\s*\{[\s\S]*?\}\s*\)\s*\(\s*(\{[\s\S]*?\})\s*\)\s*;/);
// Use more flexible matching to cover variations in the bundle.
const iifeMatch = code.match(/\(function\(\s*\w+\s*\)\s*\{[\s\S]*?\}\s*\)\s*\(\s*(\{[\s\S]*?\})\s*\)\s*;?/);
// Pattern 3: (function(e){var t={}; ...})({123:function(...)...}) - more compressed IIFE forms
const compressedIifeMatch = code.match(/\(function\s*\(\w+\)\s*\{[\s\S]*?\}\s*\)\s*\(\s*(\{[\s\S]*?\d+\s*:\s*function[\s\S]*?\})\s*\)/);
if (modulesAssignMatch) {
modules = eval('(' + modulesAssignMatch[1] + ')');
} else if (iifeMatch) {
modules = eval('(' + iifeMatch[1] + ')');
} else if (compressedIifeMatch) {
modules = eval('(' + compressedIifeMatch[1] + ')');
} else {
console.error('未识别到 webpack 模块结构,尝试按 function(module, exports) 正则提取');
modules = extractByRegex(code);
@@ -58,9 +63,10 @@ function extractByRegex(code) {
const matches = [];
while ((match = re.exec(code)) !== null) {
matches.push({ id: match[1], start: match.index });
// Store match length to avoid treating object as array later
matches.push({ id: match[1], start: match.index, matchLength: match[0].length });
}
for (let i = 0; i < matches.length; i++) {
const current = matches[i];
const next = matches[i + 1];
@@ -68,7 +74,8 @@ function extractByRegex(code) {
const endIdx = next ? next.start : code.length;
let depth = 1;
let pos = startIdx + current[0].length;
// Use stored matchLength for correct offset
let pos = startIdx + (current.matchLength || 0);
while (depth > 0 && pos < endIdx) {
if (code[pos] === '{') depth++;
if (code[pos] === '}') depth--;