this repo has no description

Use remapped path for source URL when applicable

Changed files
+50 -26
packages
core
src
core-extensions
src
moonbase
webpackModules
+15
packages/core-extensions/src/moonbase/webpackModules/crashScreen.tsx
···
}
for (const [, , id] of state.info.componentStack.matchAll(MODULE_REGEX))
for (const ext of moonlight.patched.get(id) ?? []) causes.add(ext);
+
+
for (const [path, id] of Object.entries(moonlight.moonmap.modules)) {
+
const MAPPING_REGEX = new RegExp(
+
// @ts-expect-error Only Firefox has RegExp.escape
+
`(${RegExp.escape ? RegExp.escape(path) : path.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")})`
+
);
+
+
if (state.error.stack) {
+
for (const match of state.error.stack.matchAll(MAPPING_REGEX))
+
if (match) for (const ext of moonlight.patched.get(id) ?? []) causes.add(ext);
+
}
+
for (const match of state.info.componentStack.matchAll(MAPPING_REGEX))
+
if (match) for (const ext of moonlight.patched.get(id) ?? []) causes.add(ext);
+
}
+
return [...causes];
}, []);
+35 -26
packages/core/src/patch.ts
···
const moduleCache: Record<string, string> = {};
const patched: Record<string, Array<string>> = {};
-
function patchModules(entry: WebpackJsonpEntry[1]) {
-
function patchModule(id: string, patchId: string, replaced: string) {
-
// Store what extensions patched what modules for easier debugging
-
patched[id] = patched[id] || [];
-
patched[id].push(patchId);
+
function createSourceURL(id: string) {
+
const remapped = Object.entries(moonlight.moonmap.modules).find((m) => m[1] === id)?.[0];
+
+
if (remapped) {
+
return `// Webpack Module: ${id}\n//# sourceURL=${remapped}`;
+
}
+
+
return `//# sourceURL=Webpack-Module/${id.slice(0, 3)}/${id}`;
+
}
+
+
function patchModule(id: string, patchId: string, replaced: string, entry: WebpackJsonpEntry[1]) {
+
// Store what extensions patched what modules for easier debugging
+
patched[id] = patched[id] ?? [];
+
patched[id].push(patchId);
-
// Webpack module arguments are minified, so we replace them with consistent names
-
// We have to wrap it so things don't break, though
-
const patchedStr = patched[id].sort().join(", ");
+
// Webpack module arguments are minified, so we replace them with consistent names
+
// We have to wrap it so things don't break, though
+
const patchedStr = patched[id].sort().join(", ");
-
const wrapped =
-
`(${replaced}).apply(this, arguments)\n` +
-
`// Patched by moonlight: ${patchedStr}\n` +
-
`//# sourceURL=Webpack-Module/${id.slice(0, 3)}/${id}`;
+
const wrapped =
+
`(${replaced}).apply(this, arguments)\n` + `// Patched by moonlight: ${patchedStr}\n` + createSourceURL(id);
-
try {
-
const func = new Function("module", "exports", "require", wrapped) as WebpackModuleFunc;
-
entry[id] = func;
-
entry[id].__moonlight = true;
-
return true;
-
} catch (e) {
-
logger.warn("Error constructing function for patch", patchId, e);
-
patched[id].pop();
-
return false;
-
}
+
try {
+
const func = new Function("module", "exports", "require", wrapped) as WebpackModuleFunc;
+
entry[id] = func;
+
entry[id].__moonlight = true;
+
return true;
+
} catch (e) {
+
logger.warn("Error constructing function for patch", patchId, e);
+
patched[id].pop();
+
return false;
}
+
}
+
function patchModules(entry: WebpackJsonpEntry[1]) {
// Populate the module cache
for (const [id, func] of Object.entries(entry)) {
if (!Object.hasOwn(moduleCache, id) && func.__moonlight !== true) {
···
}
if (modified) {
-
if (!swappedModule) patchModule(id, patchedStr.join(", "), moduleString);
+
if (!swappedModule) patchModule(id, patchedStr.join(", "), moduleString, entry);
moduleCache[id] = moduleString;
moonlight.patched.set(id, exts);
}
···
const parsed = moonlight.lunast.parseScript(id, moduleString);
if (parsed != null) {
for (const [parsedId, parsedScript] of Object.entries(parsed)) {
-
if (patchModule(parsedId, "lunast", parsedScript)) {
+
if (patchModule(parsedId, "lunast", parsedScript, entry)) {
moduleCache[parsedId] = parsedScript;
}
}
···
if (moonlightNode.config.patchAll === true) {
if ((typeof id !== "string" || !id.includes("_")) && !entry[id].__moonlight) {
-
const wrapped =
-
`(${moduleCache[id]}).apply(this, arguments)\n` + `//# sourceURL=Webpack-Module/${id.slice(0, 3)}/${id}`;
+
const wrapped = `(${moduleCache[id]}).apply(this, arguments)\n` + createSourceURL(id);
entry[id] = new Function("module", "exports", "require", wrapped) as WebpackModuleFunc;
entry[id].__moonlight = true;
}
···
}
for (const [name, func] of Object.entries(moonlight.moonmap.getWebpackModules("window.moonlight.moonmap"))) {
+
// @ts-expect-error probably should fix the type on this idk
+
func.__moonlight = true;
injectedWpModules.push({ id: name, run: func });
modules[name] = func;
inject = true;