Ignore more modules on webpack searching
This commit is contained in:
parent
8fccda4a24
commit
e280ed2683
2 changed files with 60 additions and 65 deletions
|
@ -24,7 +24,7 @@ import { WebpackInstance } from "discord-types/other";
|
||||||
|
|
||||||
import { traceFunction } from "../debug/Tracer";
|
import { traceFunction } from "../debug/Tracer";
|
||||||
import { patches } from "../plugins";
|
import { patches } from "../plugins";
|
||||||
import { _initWebpack, beforeInitListeners, factoryListeners, moduleListeners, subscriptions, wreq } from ".";
|
import { _initWebpack, _shouldIgnoreModule, beforeInitListeners, factoryListeners, moduleListeners, subscriptions, wreq } from ".";
|
||||||
|
|
||||||
const logger = new Logger("WebpackInterceptor", "#8caaee");
|
const logger = new Logger("WebpackInterceptor", "#8caaee");
|
||||||
|
|
||||||
|
@ -173,35 +173,9 @@ function patchFactories(factories: Record<string, (module: any, exports: any, re
|
||||||
if (!exports) return;
|
if (!exports) return;
|
||||||
|
|
||||||
if (require.c) {
|
if (require.c) {
|
||||||
let shouldMakeNonEnumerable = false;
|
const shouldIgnoreModule = _shouldIgnoreModule(exports);
|
||||||
|
|
||||||
nonEnumerableChecking: {
|
if (shouldIgnoreModule) {
|
||||||
// There are (at the time of writing) 11 modules exporting the window,
|
|
||||||
// and also modules exporting DOMTokenList, which breaks webpack finding
|
|
||||||
// Make these non enumerable to improve search performance and avoid erros
|
|
||||||
if (exports === window || exports[Symbol.toStringTag] === "DOMTokenList") {
|
|
||||||
shouldMakeNonEnumerable = true;
|
|
||||||
break nonEnumerableChecking;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof exports !== "object") {
|
|
||||||
break nonEnumerableChecking;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (exports.default === window || exports.default?.[Symbol.toStringTag] === "DOMTokenList") {
|
|
||||||
shouldMakeNonEnumerable = true;
|
|
||||||
break nonEnumerableChecking;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const nested in exports) {
|
|
||||||
if (exports[nested] === window || exports[nested]?.[Symbol.toStringTag] === "DOMTokenList") {
|
|
||||||
shouldMakeNonEnumerable = true;
|
|
||||||
break nonEnumerableChecking;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (shouldMakeNonEnumerable) {
|
|
||||||
Object.defineProperty(require.c, id, {
|
Object.defineProperty(require.c, id, {
|
||||||
value: require.c[id],
|
value: require.c[id],
|
||||||
enumerable: false,
|
enumerable: false,
|
||||||
|
@ -226,17 +200,16 @@ function patchFactories(factories: Record<string, (module: any, exports: any, re
|
||||||
if (exports && filter(exports)) {
|
if (exports && filter(exports)) {
|
||||||
subscriptions.delete(filter);
|
subscriptions.delete(filter);
|
||||||
callback(exports, id);
|
callback(exports, id);
|
||||||
} else if (typeof exports === "object") {
|
}
|
||||||
if (exports.default && filter(exports.default)) {
|
|
||||||
|
if (typeof exports !== "object") {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const exportKey in exports) {
|
||||||
|
if (exports[exportKey] && filter(exports[exportKey])) {
|
||||||
subscriptions.delete(filter);
|
subscriptions.delete(filter);
|
||||||
callback(exports.default, id);
|
callback(exports[exportKey], id);
|
||||||
} else {
|
|
||||||
for (const nested in exports) {
|
|
||||||
if (exports[nested] && filter(exports[nested])) {
|
|
||||||
subscriptions.delete(filter);
|
|
||||||
callback(exports[nested], id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
|
@ -71,13 +71,16 @@ export const filters = {
|
||||||
componentByCode: (...code: CodeFilter): FilterFn => {
|
componentByCode: (...code: CodeFilter): FilterFn => {
|
||||||
const filter = filters.byCode(...code);
|
const filter = filters.byCode(...code);
|
||||||
return m => {
|
return m => {
|
||||||
if (filter(m)) return true;
|
let inner = m;
|
||||||
if (!m.$$typeof) return false;
|
|
||||||
if (m.type)
|
while (inner != null) {
|
||||||
return m.type.render
|
if (filter(inner)) return true;
|
||||||
? filter(m.type.render) // memo + forwardRef
|
else if (!inner.$$typeof) return false;
|
||||||
: filter(m.type); // memo
|
else if (inner.type) inner = inner.type; // memos
|
||||||
if (m.render) return filter(m.render); // forwardRef
|
else if (inner.render) inner = inner.render; // forwardRefs
|
||||||
|
else return false;
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -95,6 +98,38 @@ export function _initWebpack(webpackRequire: WebpackInstance) {
|
||||||
cache = webpackRequire.c;
|
cache = webpackRequire.c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Credits to Zerebos for implementing this in BD, thus giving the idea for us to implement it too
|
||||||
|
const TypedArray = Object.getPrototypeOf(Int8Array);
|
||||||
|
|
||||||
|
function _shouldIgnoreValue(value: any) {
|
||||||
|
if (value == null) return true;
|
||||||
|
if (value === window) return true;
|
||||||
|
if (value === document || value === document.documentElement) return true;
|
||||||
|
if (value[Symbol.toStringTag] === "DOMTokenList") return true;
|
||||||
|
if (value instanceof TypedArray) return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function _shouldIgnoreModule(exports: any) {
|
||||||
|
if (_shouldIgnoreValue(exports)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof exports !== "object") {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
let allNonEnumerable = true;
|
||||||
|
for (const exportKey in exports) {
|
||||||
|
if (!_shouldIgnoreValue(exports[exportKey])) {
|
||||||
|
allNonEnumerable = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return allNonEnumerable;
|
||||||
|
}
|
||||||
|
|
||||||
let devToolsOpen = false;
|
let devToolsOpen = false;
|
||||||
if (IS_DEV && IS_DISCORD_DESKTOP) {
|
if (IS_DEV && IS_DISCORD_DESKTOP) {
|
||||||
// At this point in time, DiscordNative has not been exposed yet, so setImmediate is needed
|
// At this point in time, DiscordNative has not been exposed yet, so setImmediate is needed
|
||||||
|
@ -121,7 +156,7 @@ export const find = traceFunction("find", function find(filter: FilterFn, { isIn
|
||||||
|
|
||||||
for (const key in cache) {
|
for (const key in cache) {
|
||||||
const mod = cache[key];
|
const mod = cache[key];
|
||||||
if (!mod.loaded || !mod?.exports) continue;
|
if (!mod?.loaded || mod.exports == null) continue;
|
||||||
|
|
||||||
if (filter(mod.exports)) {
|
if (filter(mod.exports)) {
|
||||||
return isWaitFor ? [mod.exports, key] : mod.exports;
|
return isWaitFor ? [mod.exports, key] : mod.exports;
|
||||||
|
@ -129,11 +164,6 @@ export const find = traceFunction("find", function find(filter: FilterFn, { isIn
|
||||||
|
|
||||||
if (typeof mod.exports !== "object") continue;
|
if (typeof mod.exports !== "object") continue;
|
||||||
|
|
||||||
if (mod.exports.default && filter(mod.exports.default)) {
|
|
||||||
const found = mod.exports.default;
|
|
||||||
return isWaitFor ? [found, key] : found;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const nestedMod in mod.exports) {
|
for (const nestedMod in mod.exports) {
|
||||||
const nested = mod.exports[nestedMod];
|
const nested = mod.exports[nestedMod];
|
||||||
if (nested && filter(nested)) {
|
if (nested && filter(nested)) {
|
||||||
|
@ -156,16 +186,15 @@ export function findAll(filter: FilterFn) {
|
||||||
const ret = [] as any[];
|
const ret = [] as any[];
|
||||||
for (const key in cache) {
|
for (const key in cache) {
|
||||||
const mod = cache[key];
|
const mod = cache[key];
|
||||||
if (!mod.loaded || !mod?.exports) continue;
|
if (!mod?.loaded || mod.exports == null) continue;
|
||||||
|
|
||||||
if (filter(mod.exports))
|
if (filter(mod.exports))
|
||||||
ret.push(mod.exports);
|
ret.push(mod.exports);
|
||||||
else if (typeof mod.exports !== "object")
|
|
||||||
|
if (typeof mod.exports !== "object")
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (mod.exports.default && filter(mod.exports.default))
|
for (const nestedMod in mod.exports) {
|
||||||
ret.push(mod.exports.default);
|
|
||||||
else for (const nestedMod in mod.exports) {
|
|
||||||
const nested = mod.exports[nestedMod];
|
const nested = mod.exports[nestedMod];
|
||||||
if (nested && filter(nested)) ret.push(nested);
|
if (nested && filter(nested)) ret.push(nested);
|
||||||
}
|
}
|
||||||
|
@ -204,7 +233,7 @@ export const findBulk = traceFunction("findBulk", function findBulk(...filterFns
|
||||||
outer:
|
outer:
|
||||||
for (const key in cache) {
|
for (const key in cache) {
|
||||||
const mod = cache[key];
|
const mod = cache[key];
|
||||||
if (!mod.loaded || !mod?.exports) continue;
|
if (!mod?.loaded || mod.exports == null) continue;
|
||||||
|
|
||||||
for (let j = 0; j < length; j++) {
|
for (let j = 0; j < length; j++) {
|
||||||
const filter = filters[j];
|
const filter = filters[j];
|
||||||
|
@ -221,13 +250,6 @@ export const findBulk = traceFunction("findBulk", function findBulk(...filterFns
|
||||||
if (typeof mod.exports !== "object")
|
if (typeof mod.exports !== "object")
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (mod.exports.default && filter(mod.exports.default)) {
|
|
||||||
results[j] = mod.exports.default;
|
|
||||||
filters[j] = undefined;
|
|
||||||
if (++found === length) break outer;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const nestedMod in mod.exports) {
|
for (const nestedMod in mod.exports) {
|
||||||
const nested = mod.exports[nestedMod];
|
const nested = mod.exports[nestedMod];
|
||||||
if (nested && filter(nested)) {
|
if (nested && filter(nested)) {
|
||||||
|
|
Loading…
Reference in a new issue