mirror of
https://github.com/hpd840321/starRiverProperty.git
synced 2026-06-09 08:20:31 +08:00
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:
@@ -17,12 +17,17 @@ function unpackWebpackBundle(bundlePath, outputDir) {
|
|||||||
// 模式1: __webpack_modules__ = { 123: function(module, exports, __webpack_require__) {...}, ... }
|
// 模式1: __webpack_modules__ = { 123: function(module, exports, __webpack_require__) {...}, ... }
|
||||||
const modulesAssignMatch = code.match(/__webpack_modules__\s*=\s*(\{[\s\S]*?\});/);
|
const modulesAssignMatch = code.match(/__webpack_modules__\s*=\s*(\{[\s\S]*?\});/);
|
||||||
// 模式2: (function(modules) { ... })({ 123: function(...) {...}, ... })
|
// 模式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) {
|
if (modulesAssignMatch) {
|
||||||
modules = eval('(' + modulesAssignMatch[1] + ')');
|
modules = eval('(' + modulesAssignMatch[1] + ')');
|
||||||
} else if (iifeMatch) {
|
} else if (iifeMatch) {
|
||||||
modules = eval('(' + iifeMatch[1] + ')');
|
modules = eval('(' + iifeMatch[1] + ')');
|
||||||
|
} else if (compressedIifeMatch) {
|
||||||
|
modules = eval('(' + compressedIifeMatch[1] + ')');
|
||||||
} else {
|
} else {
|
||||||
console.error('未识别到 webpack 模块结构,尝试按 function(module, exports) 正则提取');
|
console.error('未识别到 webpack 模块结构,尝试按 function(module, exports) 正则提取');
|
||||||
modules = extractByRegex(code);
|
modules = extractByRegex(code);
|
||||||
@@ -58,9 +63,10 @@ function extractByRegex(code) {
|
|||||||
const matches = [];
|
const matches = [];
|
||||||
|
|
||||||
while ((match = re.exec(code)) !== null) {
|
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++) {
|
for (let i = 0; i < matches.length; i++) {
|
||||||
const current = matches[i];
|
const current = matches[i];
|
||||||
const next = matches[i + 1];
|
const next = matches[i + 1];
|
||||||
@@ -68,7 +74,8 @@ function extractByRegex(code) {
|
|||||||
const endIdx = next ? next.start : code.length;
|
const endIdx = next ? next.start : code.length;
|
||||||
|
|
||||||
let depth = 1;
|
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) {
|
while (depth > 0 && pos < endIdx) {
|
||||||
if (code[pos] === '{') depth++;
|
if (code[pos] === '{') depth++;
|
||||||
if (code[pos] === '}') depth--;
|
if (code[pos] === '}') depth--;
|
||||||
|
|||||||
Reference in New Issue
Block a user