this repo has no description

Merge pull request #216 from moonlight-mod/cyn/remapped-source-urls

Use remapped path for source URL when applicable

authored by notnite.com and committed by GitHub b7167104 cb34ba0a

Changed files
+50 -26
packages
core
src
core-extensions
src
moonbase
webpackModules
+15
packages/core-extensions/src/moonbase/webpackModules/crashScreen.tsx
··· 144 144 } 145 145 for (const [, , id] of state.info.componentStack.matchAll(MODULE_REGEX)) 146 146 for (const ext of moonlight.patched.get(id) ?? []) causes.add(ext); 147 + 148 + for (const [path, id] of Object.entries(moonlight.moonmap.modules)) { 149 + const MAPPING_REGEX = new RegExp( 150 + // @ts-expect-error Only Firefox has RegExp.escape 151 + `(${RegExp.escape ? RegExp.escape(path) : path.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")})` 152 + ); 153 + 154 + if (state.error.stack) { 155 + for (const match of state.error.stack.matchAll(MAPPING_REGEX)) 156 + if (match) for (const ext of moonlight.patched.get(id) ?? []) causes.add(ext); 157 + } 158 + for (const match of state.info.componentStack.matchAll(MAPPING_REGEX)) 159 + if (match) for (const ext of moonlight.patched.get(id) ?? []) causes.add(ext); 160 + } 161 + 147 162 return [...causes]; 148 163 }, []); 149 164
+35 -26
packages/core/src/patch.ts
··· 66 66 const moduleCache: Record<string, string> = {}; 67 67 const patched: Record<string, Array<string>> = {}; 68 68 69 - function patchModules(entry: WebpackJsonpEntry[1]) { 70 - function patchModule(id: string, patchId: string, replaced: string) { 71 - // Store what extensions patched what modules for easier debugging 72 - patched[id] = patched[id] || []; 73 - patched[id].push(patchId); 69 + function createSourceURL(id: string) { 70 + const remapped = Object.entries(moonlight.moonmap.modules).find((m) => m[1] === id)?.[0]; 71 + 72 + if (remapped) { 73 + return `// Webpack Module: ${id}\n//# sourceURL=${remapped}`; 74 + } 75 + 76 + return `//# sourceURL=Webpack-Module/${id.slice(0, 3)}/${id}`; 77 + } 78 + 79 + function patchModule(id: string, patchId: string, replaced: string, entry: WebpackJsonpEntry[1]) { 80 + // Store what extensions patched what modules for easier debugging 81 + patched[id] = patched[id] ?? []; 82 + patched[id].push(patchId); 74 83 75 - // Webpack module arguments are minified, so we replace them with consistent names 76 - // We have to wrap it so things don't break, though 77 - const patchedStr = patched[id].sort().join(", "); 84 + // Webpack module arguments are minified, so we replace them with consistent names 85 + // We have to wrap it so things don't break, though 86 + const patchedStr = patched[id].sort().join(", "); 78 87 79 - const wrapped = 80 - `(${replaced}).apply(this, arguments)\n` + 81 - `// Patched by moonlight: ${patchedStr}\n` + 82 - `//# sourceURL=Webpack-Module/${id.slice(0, 3)}/${id}`; 88 + const wrapped = 89 + `(${replaced}).apply(this, arguments)\n` + `// Patched by moonlight: ${patchedStr}\n` + createSourceURL(id); 83 90 84 - try { 85 - const func = new Function("module", "exports", "require", wrapped) as WebpackModuleFunc; 86 - entry[id] = func; 87 - entry[id].__moonlight = true; 88 - return true; 89 - } catch (e) { 90 - logger.warn("Error constructing function for patch", patchId, e); 91 - patched[id].pop(); 92 - return false; 93 - } 91 + try { 92 + const func = new Function("module", "exports", "require", wrapped) as WebpackModuleFunc; 93 + entry[id] = func; 94 + entry[id].__moonlight = true; 95 + return true; 96 + } catch (e) { 97 + logger.warn("Error constructing function for patch", patchId, e); 98 + patched[id].pop(); 99 + return false; 94 100 } 101 + } 95 102 103 + function patchModules(entry: WebpackJsonpEntry[1]) { 96 104 // Populate the module cache 97 105 for (const [id, func] of Object.entries(entry)) { 98 106 if (!Object.hasOwn(moduleCache, id) && func.__moonlight !== true) { ··· 185 193 } 186 194 187 195 if (modified) { 188 - if (!swappedModule) patchModule(id, patchedStr.join(", "), moduleString); 196 + if (!swappedModule) patchModule(id, patchedStr.join(", "), moduleString, entry); 189 197 moduleCache[id] = moduleString; 190 198 moonlight.patched.set(id, exts); 191 199 } ··· 194 202 const parsed = moonlight.lunast.parseScript(id, moduleString); 195 203 if (parsed != null) { 196 204 for (const [parsedId, parsedScript] of Object.entries(parsed)) { 197 - if (patchModule(parsedId, "lunast", parsedScript)) { 205 + if (patchModule(parsedId, "lunast", parsedScript, entry)) { 198 206 moduleCache[parsedId] = parsedScript; 199 207 } 200 208 } ··· 205 213 206 214 if (moonlightNode.config.patchAll === true) { 207 215 if ((typeof id !== "string" || !id.includes("_")) && !entry[id].__moonlight) { 208 - const wrapped = 209 - `(${moduleCache[id]}).apply(this, arguments)\n` + `//# sourceURL=Webpack-Module/${id.slice(0, 3)}/${id}`; 216 + const wrapped = `(${moduleCache[id]}).apply(this, arguments)\n` + createSourceURL(id); 210 217 entry[id] = new Function("module", "exports", "require", wrapped) as WebpackModuleFunc; 211 218 entry[id].__moonlight = true; 212 219 } ··· 329 336 } 330 337 331 338 for (const [name, func] of Object.entries(moonlight.moonmap.getWebpackModules("window.moonlight.moonmap"))) { 339 + // @ts-expect-error probably should fix the type on this idk 340 + func.__moonlight = true; 332 341 injectedWpModules.push({ id: name, run: func }); 333 342 modules[name] = func; 334 343 inject = true;