improve webpack find error messages

This commit is contained in:
V 2023-09-25 18:27:18 +02:00
parent 376aaf39ce
commit c6b1b9463c
No known key found for this signature in database
GPG Key ID: A1DC0CFB5615D905

@ -75,10 +75,19 @@ if (IS_DEV && IS_DISCORD_DESKTOP) {
}, 0); }, 0);
} }
function handleModuleNotFound(method: string, ...filter: unknown[]) {
const err = new Error(`webpack.${method} found no module`);
logger.error(err, "Filter:", filter);
// Strict behaviour in DevBuilds to fail early and make sure the issue is found
if (IS_DEV && !devToolsOpen)
throw err;
}
/** /**
* Find the first module that matches the filter * Find the first module that matches the filter
*/ */
export const find = traceFunction("find", function find(filter: FilterFn, getDefault = true, isWaitFor = false) { export const find = traceFunction("find", function find(filter: FilterFn, { isIndirect = false, isWaitFor = false }: { isIndirect?: boolean; isWaitFor?: boolean; } = {}) {
if (typeof filter !== "function") if (typeof filter !== "function")
throw new Error("Invalid filter. Expected a function got " + typeof filter); throw new Error("Invalid filter. Expected a function got " + typeof filter);
@ -93,7 +102,7 @@ export const find = traceFunction("find", function find(filter: FilterFn, getDef
if (typeof mod.exports !== "object") continue; if (typeof mod.exports !== "object") continue;
if (mod.exports.default && filter(mod.exports.default)) { if (mod.exports.default && filter(mod.exports.default)) {
const found = getDefault ? mod.exports.default : mod.exports; const found = mod.exports.default;
return isWaitFor ? [found, Number(key)] : found; return isWaitFor ? [found, Number(key)] : found;
} }
@ -106,17 +115,8 @@ export const find = traceFunction("find", function find(filter: FilterFn, getDef
} }
} }
if (!isWaitFor) { if (!isIndirect) {
const err = new Error("Didn't find module matching this filter"); handleModuleNotFound("find", filter);
if (IS_DEV) {
logger.error(err);
logger.error(filter);
if (!devToolsOpen)
// Strict behaviour in DevBuilds to fail early and make sure the issue is found
throw err;
} else {
logger.warn(err);
}
} }
return isWaitFor ? [null, null] : null; return isWaitFor ? [null, null] : null;
@ -125,11 +125,11 @@ export const find = traceFunction("find", function find(filter: FilterFn, getDef
/** /**
* find but lazy * find but lazy
*/ */
export function findLazy(filter: FilterFn, getDefault = true) { export function findLazy(filter: FilterFn) {
return proxyLazy(() => find(filter, getDefault)); return proxyLazy(() => find(filter));
} }
export function findAll(filter: FilterFn, getDefault = true) { export function findAll(filter: FilterFn) {
if (typeof filter !== "function") if (typeof filter !== "function")
throw new Error("Invalid filter. Expected a function got " + typeof filter); throw new Error("Invalid filter. Expected a function got " + typeof filter);
@ -144,7 +144,7 @@ export function findAll(filter: FilterFn, getDefault = true) {
continue; continue;
if (mod.exports.default && filter(mod.exports.default)) if (mod.exports.default && filter(mod.exports.default))
ret.push(getDefault ? mod.exports.default : mod.exports); ret.push(mod.exports.default);
else for (const nestedMod in mod.exports) if (nestedMod.length <= 3) { else for (const nestedMod in mod.exports) if (nestedMod.length <= 3) {
const nested = mod.exports[nestedMod]; const nested = mod.exports[nestedMod];
if (nested && filter(nested)) ret.push(nested); if (nested && filter(nested)) ret.push(nested);
@ -304,42 +304,51 @@ export function mapMangledModuleLazy<S extends string>(code: string, mappers: Re
* Find the first module that has the specified properties * Find the first module that has the specified properties
*/ */
export function findByProps(...props: string[]) { export function findByProps(...props: string[]) {
return find(filters.byProps(...props)); const res = find(filters.byProps(...props), { isIndirect: true });
if (!res)
handleModuleNotFound("findByProps", ...props);
return res;
} }
/** /**
* findByProps but lazy * findByProps but lazy
*/ */
export function findByPropsLazy(...props: string[]) { export function findByPropsLazy(...props: string[]) {
return findLazy(filters.byProps(...props)); return proxyLazy(() => findByProps(...props));
} }
/** /**
* Find a function by its code * Find a function by its code
*/ */
export function findByCode(...code: string[]) { export function findByCode(...code: string[]) {
return find(filters.byCode(...code)); const res = find(filters.byCode(...code), { isIndirect: true });
if (!res)
handleModuleNotFound("findByCode", ...code);
return res;
} }
/** /**
* findByCode but lazy * findByCode but lazy
*/ */
export function findByCodeLazy(...code: string[]) { export function findByCodeLazy(...code: string[]) {
return findLazy(filters.byCode(...code)); return proxyLazy(() => findByCode(...code));
} }
/** /**
* Find a store by its displayName * Find a store by its displayName
*/ */
export function findStore(name: string) { export function findStore(name: string) {
return find(filters.byStoreName(name)); const res = find(filters.byStoreName(name), { isIndirect: true });
if (!res)
handleModuleNotFound("findStore", name);
return res;
} }
/** /**
* findByDisplayName but lazy * findByDisplayName but lazy
*/ */
export function findStoreLazy(name: string) { export function findStoreLazy(name: string) {
return findLazy(filters.byStoreName(name)); return proxyLazy(() => findStore(name));
} }
/** /**
@ -354,7 +363,7 @@ export function waitFor(filter: string | string[] | FilterFn, callback: Callback
else if (typeof filter !== "function") else if (typeof filter !== "function")
throw new Error("filter must be a string, string[] or function, got " + typeof filter); throw new Error("filter must be a string, string[] or function, got " + typeof filter);
const [existing, id] = find(filter!, true, true); const [existing, id] = find(filter!, { isIndirect: true, isWaitFor: true });
if (existing) return void callback(existing, id); if (existing) return void callback(existing, id);
subscriptions.set(filter, callback); subscriptions.set(filter, callback);