Reduce the amount of preloaded webpack chunks

This commit is contained in:
Jade Macho 2024-05-13 00:48:49 +02:00
parent e8c3154a79
commit 95da3db49f
Signed by: 0x0ade
GPG Key ID: E1960710FE4FBEEF

View File

@ -95,23 +95,14 @@ export default JadefinIntegrity("Jadefin", import.meta.url, () => window["Jadefi
}; };
/** @type {(id: any) => string} */ /** @type {(id: any) => string} */
this.webpackIdToChunkJS = Object.values(this.webpackLoad).find(v => typeof(v) == "function" && v.toString().indexOf(`+".chunk.js"`) != -1); this.webpackChunkIdToJS = Object.values(this.webpackLoad).find(v => typeof(v) == "function" && v.toString().indexOf(`+".chunk.js"`) != -1);
/** @type {(id: any) => string} */ /** @type {(id: any) => string} */
this.webpackIdToChunkCSS = Object.values(this.webpackLoad).find(v => typeof(v) == "function" && v.toString().indexOf(`+".css"`) != -1); this.webpackIdToCSS = Object.values(this.webpackLoad).find(v => typeof(v) == "function" && v.toString().indexOf(`+".css"`) != -1);
if (!this.webpackChunkIdToJS || !this.webpackIdToCSS) {
const webpackIdToChunkJSKey = Object.keys(this.webpackLoad).find(k => this.webpackLoad?.[k] == this.webpackIdToChunkJS); this.log.e("Couldn't obtain webpackChunkIdToJS or webpackIdToCSS");
if (!this.webpackIdToChunkJS || !webpackIdToChunkJSKey) {
this.log.e("Couldn't obtain webpackIdToChunkJS");
return; return;
} }
this.webpackLoad[webpackIdToChunkJSKey] = function(id) {
const rv = self.webpackIdToChunkJS?.(id);
self.log.v(`Webpack converted chunk ID to JS name: ${id} -> ${rv}`);
return rv;
};
/** @type {(name: any, cb: any, sid: any, id: any) => any} */ /** @type {(name: any, cb: any, sid: any, id: any) => any} */
this.webpackLoadChunkLowLevel = Object.values(this.webpackLoad).find(v => typeof(v) == "function" && v.toString().indexOf(`document.head.appendChild`) != -1); this.webpackLoadChunkLowLevel = Object.values(this.webpackLoad).find(v => typeof(v) == "function" && v.toString().indexOf(`document.head.appendChild`) != -1);
const webpackLoadChunkLowLevelKey = Object.keys(this.webpackLoad).find(k => this.webpackLoad?.[k] == this.webpackLoadChunkLowLevel); const webpackLoadChunkLowLevelKey = Object.keys(this.webpackLoad).find(k => this.webpackLoad?.[k] == this.webpackLoadChunkLowLevel);
@ -122,13 +113,11 @@ export default JadefinIntegrity("Jadefin", import.meta.url, () => window["Jadefi
this.webpackLoad[webpackLoadChunkLowLevelKey] = function(name, cb, sid, id) { this.webpackLoad[webpackLoadChunkLowLevelKey] = function(name, cb, sid, id) {
const _cb = cb; const _cb = cb;
self.log.v(`Webpack loading chunk ${id} (${sid}) from ${name}`); // self.log.v(`Webpack loading chunk ${id} (${sid}) from ${name}`);
/*
cb = (event) => { cb = (event) => {
self.log.v(`Webpack loaded chunk ${id} (${sid}) from ${name}: ${event.type}`); self.log.v(`Webpack loaded chunk ${id} (${sid}) from ${name}: ${event.type}`);
return _cb(event); return _cb(event);
}; };
*/
const rv = self.webpackLoadChunkLowLevel?.(name, cb, sid, id); const rv = self.webpackLoadChunkLowLevel?.(name, cb, sid, id);
return rv; return rv;
}; };
@ -136,12 +125,33 @@ export default JadefinIntegrity("Jadefin", import.meta.url, () => window["Jadefi
/** @type {(id: any) => Promise<any>} */ /** @type {(id: any) => Promise<any>} */
this.webpackLoadChunk = Object.values(this.webpackLoad).find(v => typeof(v) == "function" && v.toString().indexOf(`Object.keys(`) != -1 && v.toString().indexOf(`}),[])`) != -1); this.webpackLoadChunk = Object.values(this.webpackLoad).find(v => typeof(v) == "function" && v.toString().indexOf(`Object.keys(`) != -1 && v.toString().indexOf(`}),[])`) != -1);
// HACKFIX: Load all chunks when Jadefin initializes! // HACK: Load all chunks we could need when Jadefin initializes!
// webpackIdToChunkJS contains all IDs either ahead of === or : // webpackChunkIdToJS contains all IDs either ahead of === or :
// FIXME: This smells like race condition hell! We might be too late for this kind of hooking. // FIXME: Ideally, don't. This makes startup take ages and is unreliable!
// FIXME: Ideally, don't. This makes startup take ages. this.webpackChunkIds = [...this.webpackChunkIdToJS.toString().matchAll(/\d+(?=:|=)/g)].map(v => parseInt(v[0]));
const webpackChunks = [...this.webpackIdToChunkJS.toString().matchAll(/\d+(?=:|=)/g)].map(v => parseInt(v[0])); /** @type {any} */
await Promise.all(webpackChunks.map(id => this.webpackLoadChunk?.(id))); this.webpackChunkJSs = {};
for (const id of this.webpackChunkIds) {
this.webpackChunkJSs[this.webpackChunkIdToJS(id)] = id;
}
// Grabbed via JSON.stringify(webpackChunk.flatMap(c => c[0])) on the home screen.
const webpackChunkIdsWanted = JSON.parse("[1451,45642,23247,60815,81771,55931,1270,9886,12036,86897,59928,44965,56401,7495,17060,67224,67622,94048,36933,82363,36546,99377,82798,14577,78283,37658,94160,7184,83518,15277,71779,9911,82420,49398,32292,37821,74776,99994,87074,67942,60039,27017,65149,27182,4636,87530,4801,82255,99435,77077,71944,59874,21816,89409,79754,69285,43091,64380,40810,90186,87903,70118,64633,10905,68672,71318,86040,70555,41542,4836,30357,6270,49087,64706,19907,927,10672,2,56577,29593,55125,83354,49755,1680,39573,40394,60138,13151,65849,80835,14510,96307,16304,62155,84430,59258,37658,94160,7184,83518,15277,71779,9911,82420,49398,32292,39232,40465,18395,48979,68413,60039,27017,65149,27182,4636,87530,4801,82255,99435,77077,71944,59874,21816,80183,85500,7011,22940,33067,15434,39435,78938,94047,55802,18084,79617,1998,15605,57949,21857,91737,28567,22424,44184,45568,14510,96307,5617,96084,40367,73233,52011,40367,96084,56422,27962,96084,32762,35463,8372,28349,58782,29808,18119,24468,50777,84158,49275,35308,38965]");
// Grabbed through searching for module loads which still errored out.
webpackChunkIdsWanted.push(
...Object.keys(this.webpackChunkJSs).filter(js => {
return false ||
js.startsWith("user-display.") ||
js.startsWith("user-display-") ||
js.startsWith("53147.") ||
js.startsWith("activity.") ||
js.startsWith("node_modules.@juggle.") ||
false;
}).map(js => this.webpackChunkJSs[js])
);
await Promise.all(webpackChunkIdsWanted.map(id => this.webpackLoadChunk?.(id)));
// Wait until everything else is ready. // Wait until everything else is ready.
await JadefinUtils.waitUntil(() => this.webpackModuleFuncs); await JadefinUtils.waitUntil(() => this.webpackModuleFuncs);