Merge remote-tracking branch 'upstream/dev' into dev

This commit is contained in:
thororen1234 2025-02-07 19:23:32 -05:00
commit dc4e9cf775
22 changed files with 921 additions and 379 deletions

View file

@ -20,9 +20,9 @@ import { makeLazy, proxyLazy } from "@utils/lazy";
import { LazyComponent } from "@utils/lazyReact";
import { Logger } from "@utils/Logger";
import { canonicalizeMatch } from "@utils/patches";
import type { WebpackInstance } from "discord-types/other";
import { traceFunction } from "../debug/Tracer";
import { AnyModuleFactory, ModuleExports, WebpackRequire } from "./wreq";
const logger = new Logger("Webpack");
@ -33,8 +33,8 @@ export let _resolveReady: () => void;
*/
export const onceReady = new Promise<void>(r => _resolveReady = r);
export let wreq: WebpackInstance;
export let cache: WebpackInstance["c"];
export let wreq: WebpackRequire;
export let cache: WebpackRequire["c"];
export type FilterFn = (mod: any) => boolean;
@ -89,16 +89,27 @@ export const filters = {
}
};
export type CallbackFn = (mod: any, id: string) => void;
export type CallbackFn = (module: ModuleExports, id: PropertyKey) => void;
export type FactoryListernFn = (factory: AnyModuleFactory) => void;
export const subscriptions = new Map<FilterFn, CallbackFn>();
export const waitForSubscriptions = new Map<FilterFn, CallbackFn>();
export const moduleListeners = new Set<CallbackFn>();
export const factoryListeners = new Set<(factory: (module: any, exports: any, require: WebpackInstance) => void) => void>();
export const beforeInitListeners = new Set<(wreq: WebpackInstance) => void>();
export const factoryListeners = new Set<FactoryListernFn>();
export function _initWebpack(webpackRequire: WebpackRequire) {
if (webpackRequire.c == null) {
return;
}
export function _initWebpack(webpackRequire: WebpackInstance) {
wreq = webpackRequire;
cache = webpackRequire.c;
Reflect.defineProperty(webpackRequire.c, Symbol.toStringTag, {
value: "ModuleCache",
configurable: true,
writable: true,
enumerable: false
});
}
// Credits to Zerebos for implementing this in BD, thus giving the idea for us to implement it too
@ -533,7 +544,7 @@ export const ChunkIdsRegex = /\("([^"]+?)"\)/g;
* @param matcher A RegExp that returns the chunk ids array as the first capture group and the entry point id as the second. Defaults to a matcher that captures the first lazy chunk loading found in the module factory
* @returns A promise that resolves with a boolean whether the chunks were loaded
*/
export async function extractAndLoadChunks(code: CodeFilter, matcher: RegExp = DefaultExtractAndLoadChunksRegex) {
export async function extractAndLoadChunks(code: CodeFilter, matcher = DefaultExtractAndLoadChunksRegex) {
const module = findModuleFactory(...code);
if (!module) {
const err = new Error("extractAndLoadChunks: Couldn't find module factory");
@ -546,7 +557,7 @@ export async function extractAndLoadChunks(code: CodeFilter, matcher: RegExp = D
return false;
}
const match = module.toString().match(canonicalizeMatch(matcher));
const match = String(module).match(canonicalizeMatch(matcher));
if (!match) {
const err = new Error("extractAndLoadChunks: Couldn't find chunk loading in module factory code");
logger.warn(err, "Code:", code, "Matcher:", matcher);
@ -559,8 +570,9 @@ export async function extractAndLoadChunks(code: CodeFilter, matcher: RegExp = D
}
const [, rawChunkIds, entryPointId] = match;
if (Number.isNaN(Number(entryPointId))) {
const err = new Error("extractAndLoadChunks: Matcher didn't return a capturing group with the chunk ids array, or the entry point id returned as the second group wasn't a number");
if (entryPointId == null) {
const err = new Error("extractAndLoadChunks: Matcher didn't return a capturing group with the chunk ids array or the entry point id");
logger.warn(err, "Code:", code, "Matcher:", matcher);
// Strict behaviour in DevBuilds to fail early and make sure the issue is found
@ -570,12 +582,19 @@ export async function extractAndLoadChunks(code: CodeFilter, matcher: RegExp = D
return false;
}
const numEntryPoint = Number(entryPointId);
const entryPoint = Number.isNaN(numEntryPoint) ? entryPointId : numEntryPoint;
if (rawChunkIds) {
const chunkIds = Array.from(rawChunkIds.matchAll(ChunkIdsRegex)).map((m: any) => Number(m[1]));
const chunkIds = Array.from(rawChunkIds.matchAll(ChunkIdsRegex)).map(m => {
const numChunkId = Number(m[1]);
return Number.isNaN(numChunkId) ? m[1] : numChunkId;
});
await Promise.all(chunkIds.map(id => wreq.e(id)));
}
if (wreq.m[entryPointId] == null) {
if (wreq.m[entryPoint] == null) {
const err = new Error("extractAndLoadChunks: Entry point is not loaded in the module factories, perhaps one of the chunks failed to load");
logger.warn(err, "Code:", code, "Matcher:", matcher);
@ -586,7 +605,7 @@ export async function extractAndLoadChunks(code: CodeFilter, matcher: RegExp = D
return false;
}
wreq(Number(entryPointId));
wreq(entryPoint);
return true;
}
@ -623,7 +642,7 @@ export function waitFor(filter: string | PropsFilter | FilterFn, callback: Callb
if (existing) return void callback(existing, id);
}
subscriptions.set(filter, callback);
waitForSubscriptions.set(filter, callback);
}
/**
@ -639,7 +658,7 @@ export function search(...code: CodeFilter) {
const factories = wreq.m;
for (const id in factories) {
const factory = factories[id].original ?? factories[id];
const factory = factories[id];
if (stringMatches(factory.toString(), code))
results[id] = factory;