Webpack: Expose cache and require

This commit is contained in:
Vendicated 2022-09-05 19:54:02 +02:00
parent 9aed4d275f
commit 83cdd86926
No known key found for this signature in database
GPG key ID: EC781ADFB93EFFA3

View file

@ -1,4 +1,7 @@
let webpackCache: typeof window.webpackChunkdiscord_app; import type { WebpackInstance } from "discord-types/other";
export let wreq: WebpackInstance;
export let cache: WebpackInstance["c"];
export type FilterFn = (mod: any) => boolean; export type FilterFn = (mod: any) => boolean;
@ -16,9 +19,10 @@ export const listeners = new Set<CallbackFn>();
export type CallbackFn = (mod: any) => void; export type CallbackFn = (mod: any) => void;
export function _initWebpack(instance: typeof window.webpackChunkdiscord_app) { export function _initWebpack(instance: typeof window.webpackChunkdiscord_app) {
if (webpackCache !== void 0) throw "no."; if (cache !== void 0) throw "no.";
webpackCache = instance.push([[Symbol()], {}, (r) => r.c]); wreq = instance.push([[Symbol()], {}, (r) => r]);
cache = wreq.c;
instance.pop(); instance.pop();
} }
@ -26,8 +30,8 @@ export function find(filter: FilterFn, getDefault = true) {
if (typeof filter !== "function") if (typeof filter !== "function")
throw new Error("Invalid filter. Expected a function got", filter); throw new Error("Invalid filter. Expected a function got", filter);
for (const key in webpackCache) { for (const key in cache) {
const mod = webpackCache[key]; const mod = cache[key];
if (mod?.exports && filter(mod.exports)) if (mod?.exports && filter(mod.exports))
return mod.exports; return mod.exports;
if (mod?.exports?.default && filter(mod.exports.default)) if (mod?.exports?.default && filter(mod.exports.default))
@ -41,8 +45,8 @@ export function findAll(filter: FilterFn, getDefault = true) {
if (typeof filter !== "function") throw new Error("Invalid filter. Expected a function got", filter); if (typeof filter !== "function") throw new Error("Invalid filter. Expected a function got", filter);
const ret = [] as any[]; const ret = [] as any[];
for (const key in webpackCache) { for (const key in cache) {
const mod = webpackCache[key]; const mod = cache[key];
if (mod?.exports && filter(mod.exports)) ret.push(mod.exports); if (mod?.exports && filter(mod.exports)) ret.push(mod.exports);
if (mod?.exports?.default && filter(mod.exports.default)) ret.push(getDefault ? mod.exports.default : mod.exports); if (mod?.exports?.default && filter(mod.exports.default)) ret.push(getDefault ? mod.exports.default : mod.exports);
} }