Remove newlines in webpack chunks

This commit is contained in:
Vendicated 2022-09-15 18:17:52 +02:00
parent b1d3f5e52f
commit 9bf28c0e7a
No known key found for this signature in database
GPG key ID: EC781ADFB93EFFA3
3 changed files with 11 additions and 8 deletions

View file

@ -7,7 +7,7 @@ export default definePlugin({
patches: [{ patches: [{
find: "setDevtoolsCallbacks", find: "setDevtoolsCallbacks",
replacement: { replacement: {
match: /\.default=function.+$/s, match: /\.default=function.+$/,
replace: ".default=function(){}}" replace: ".default=function(){}}"
} }
}] }]

View file

@ -16,7 +16,7 @@ export default definePlugin({
} }
}, },
{ {
match: /\w\.createElement.+?["']Host ["'].+?\):null/s, match: /\w\.createElement.+?["']Host ["'].+?\):null/,
replace: m => { replace: m => {
const idx = m.indexOf("Host") - 1; const idx = m.indexOf("Host") - 1;
const template = m.slice(0, idx); const template = m.slice(0, idx);

View file

@ -9,11 +9,7 @@ const logger = new Logger("WebpackInterceptor", "#8caaee");
Object.defineProperty(window, WEBPACK_CHUNK, { Object.defineProperty(window, WEBPACK_CHUNK, {
get: () => webpackChunk, get: () => webpackChunk,
set: (v) => { set: (v) => {
// There are two possible values for push. if (v?.push !== Array.prototype.push) {
// - Native push with toString result of function push() { [native code] }
// - Webpack's push with toString result of function() { [native code] }
// We don't want to override the native one, so check for "push"
if (v && !v.push.toString().includes("push")) {
logger.info(`Patching ${WEBPACK_CHUNK}.push`); logger.info(`Patching ${WEBPACK_CHUNK}.push`);
_initWebpack(v); _initWebpack(v);
patchPush(); patchPush();
@ -35,7 +31,14 @@ function patchPush() {
for (const id in modules) { for (const id in modules) {
let mod = modules[id]; let mod = modules[id];
let code = mod.toString(); // Discords Webpack chunks for some ungodly reason contain random
// newlines. Cyn recommended this workaround and it seems to work fine,
// however this could potentially break code, so if anything goes weird,
// this is probably why.
// Additionally, `[actual newline]` is one less char than "\n", so if Discord
// ever targets newer browsers, the minifier could potentially use this trick and
// cause issues.
let code = mod.toString().replaceAll("\n", "");
const originalMod = mod; const originalMod = mod;
const patchedBy = new Set(); const patchedBy = new Set();