···144144 }
145145 for (const [, , id] of state.info.componentStack.matchAll(MODULE_REGEX))
146146 for (const ext of moonlight.patched.get(id) ?? []) causes.add(ext);
147147+148148+ for (const [path, id] of Object.entries(moonlight.moonmap.modules)) {
149149+ const MAPPING_REGEX = new RegExp(
150150+ // @ts-expect-error Only Firefox has RegExp.escape
151151+ `(${RegExp.escape ? RegExp.escape(path) : path.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")})`
152152+ );
153153+154154+ if (state.error.stack) {
155155+ for (const match of state.error.stack.matchAll(MAPPING_REGEX))
156156+ if (match) for (const ext of moonlight.patched.get(id) ?? []) causes.add(ext);
157157+ }
158158+ for (const match of state.info.componentStack.matchAll(MAPPING_REGEX))
159159+ if (match) for (const ext of moonlight.patched.get(id) ?? []) causes.add(ext);
160160+ }
161161+147162 return [...causes];
148163 }, []);
149164
+35-26
packages/core/src/patch.ts
···6666const moduleCache: Record<string, string> = {};
6767const patched: Record<string, Array<string>> = {};
68686969-function patchModules(entry: WebpackJsonpEntry[1]) {
7070- function patchModule(id: string, patchId: string, replaced: string) {
7171- // Store what extensions patched what modules for easier debugging
7272- patched[id] = patched[id] || [];
7373- patched[id].push(patchId);
6969+function createSourceURL(id: string) {
7070+ const remapped = Object.entries(moonlight.moonmap.modules).find((m) => m[1] === id)?.[0];
7171+7272+ if (remapped) {
7373+ return `// Webpack Module: ${id}\n//# sourceURL=${remapped}`;
7474+ }
7575+7676+ return `//# sourceURL=Webpack-Module/${id.slice(0, 3)}/${id}`;
7777+}
7878+7979+function patchModule(id: string, patchId: string, replaced: string, entry: WebpackJsonpEntry[1]) {
8080+ // Store what extensions patched what modules for easier debugging
8181+ patched[id] = patched[id] ?? [];
8282+ patched[id].push(patchId);
74837575- // Webpack module arguments are minified, so we replace them with consistent names
7676- // We have to wrap it so things don't break, though
7777- const patchedStr = patched[id].sort().join(", ");
8484+ // Webpack module arguments are minified, so we replace them with consistent names
8585+ // We have to wrap it so things don't break, though
8686+ const patchedStr = patched[id].sort().join(", ");
78877979- const wrapped =
8080- `(${replaced}).apply(this, arguments)\n` +
8181- `// Patched by moonlight: ${patchedStr}\n` +
8282- `//# sourceURL=Webpack-Module/${id.slice(0, 3)}/${id}`;
8888+ const wrapped =
8989+ `(${replaced}).apply(this, arguments)\n` + `// Patched by moonlight: ${patchedStr}\n` + createSourceURL(id);
83908484- try {
8585- const func = new Function("module", "exports", "require", wrapped) as WebpackModuleFunc;
8686- entry[id] = func;
8787- entry[id].__moonlight = true;
8888- return true;
8989- } catch (e) {
9090- logger.warn("Error constructing function for patch", patchId, e);
9191- patched[id].pop();
9292- return false;
9393- }
9191+ try {
9292+ const func = new Function("module", "exports", "require", wrapped) as WebpackModuleFunc;
9393+ entry[id] = func;
9494+ entry[id].__moonlight = true;
9595+ return true;
9696+ } catch (e) {
9797+ logger.warn("Error constructing function for patch", patchId, e);
9898+ patched[id].pop();
9999+ return false;
94100 }
101101+}
95102103103+function patchModules(entry: WebpackJsonpEntry[1]) {
96104 // Populate the module cache
97105 for (const [id, func] of Object.entries(entry)) {
98106 if (!Object.hasOwn(moduleCache, id) && func.__moonlight !== true) {
···185193 }
186194187195 if (modified) {
188188- if (!swappedModule) patchModule(id, patchedStr.join(", "), moduleString);
196196+ if (!swappedModule) patchModule(id, patchedStr.join(", "), moduleString, entry);
189197 moduleCache[id] = moduleString;
190198 moonlight.patched.set(id, exts);
191199 }
···194202 const parsed = moonlight.lunast.parseScript(id, moduleString);
195203 if (parsed != null) {
196204 for (const [parsedId, parsedScript] of Object.entries(parsed)) {
197197- if (patchModule(parsedId, "lunast", parsedScript)) {
205205+ if (patchModule(parsedId, "lunast", parsedScript, entry)) {
198206 moduleCache[parsedId] = parsedScript;
199207 }
200208 }
···205213206214 if (moonlightNode.config.patchAll === true) {
207215 if ((typeof id !== "string" || !id.includes("_")) && !entry[id].__moonlight) {
208208- const wrapped =
209209- `(${moduleCache[id]}).apply(this, arguments)\n` + `//# sourceURL=Webpack-Module/${id.slice(0, 3)}/${id}`;
216216+ const wrapped = `(${moduleCache[id]}).apply(this, arguments)\n` + createSourceURL(id);
210217 entry[id] = new Function("module", "exports", "require", wrapped) as WebpackModuleFunc;
211218 entry[id].__moonlight = true;
212219 }
···329336 }
330337331338 for (const [name, func] of Object.entries(moonlight.moonmap.getWebpackModules("window.moonlight.moonmap"))) {
339339+ // @ts-expect-error probably should fix the type on this idk
340340+ func.__moonlight = true;
332341 injectedWpModules.push({ id: name, run: func });
333342 modules[name] = func;
334343 inject = true;