diff --git a/frontend-source/scripts/unpack-webpack.js b/frontend-source/scripts/unpack-webpack.js index 3f30fef7..aada4559 100755 --- a/frontend-source/scripts/unpack-webpack.js +++ b/frontend-source/scripts/unpack-webpack.js @@ -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--;