Webpack: Do not emit errors if devtools open

This commit is contained in:
Vendicated 2022-11-03 20:36:17 +01:00
parent cb7469afad
commit 65620f4976
No known key found for this signature in database
GPG key ID: EC781ADFB93EFFA3
4 changed files with 76 additions and 18 deletions

View file

@ -23,6 +23,7 @@ import { dirname, join } from "path";
import { initIpc } from "./ipcMain"; import { initIpc } from "./ipcMain";
import { installExt } from "./ipcMain/extensions"; import { installExt } from "./ipcMain/extensions";
import { readSettings } from "./ipcMain/index"; import { readSettings } from "./ipcMain/index";
import { onceDefined } from "./utils/onceDefined";
console.log("[Vencord] Starting up..."); console.log("[Vencord] Starting up...");
@ -74,15 +75,9 @@ require.cache[electronPath]!.exports = {
}; };
// Patch appSettings to force enable devtools // Patch appSettings to force enable devtools
Object.defineProperty(global, "appSettings", { onceDefined(global, "appSettings", s =>
set: (v: typeof global.appSettings) => { s.set("DANGEROUS_ENABLE_DEVTOOLS_ONLY_ENABLE_IF_YOU_KNOW_WHAT_YOURE_DOING", true)
v.set("DANGEROUS_ENABLE_DEVTOOLS_ONLY_ENABLE_IF_YOU_KNOW_WHAT_YOURE_DOING", true); );
// @ts-ignore
delete global.appSettings;
global.appSettings = v;
},
configurable: true
});
process.env.DATA_DIR = join(app.getPath("userData"), "..", "Vencord"); process.env.DATA_DIR = join(app.getPath("userData"), "..", "Vencord");

View file

@ -24,4 +24,6 @@ export { default as IpcEvents } from "./IpcEvents";
export { default as Logger } from "./logger"; export { default as Logger } from "./logger";
export * from "./misc"; export * from "./misc";
export * as Modals from "./modal"; export * as Modals from "./modal";
export * from "./onceDefined";
export * from "./proxyLazy"; export * from "./proxyLazy";
export * from "./Queue";

47
src/utils/onceDefined.ts Normal file
View file

@ -0,0 +1,47 @@
/*
* Vencord, a modification for Discord's desktop app
* Copyright (c) 2022 Vendicated and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import type { LiteralUnion } from "type-fest";
/**
* Wait for a property to be defined on the target, then call the callback with
* the value
* @param target Object
* @param property Property to be defined
* @param callback Callback
*
* @example onceDefined(window, "webpackChunkdiscord_app", wpInstance => wpInstance.push(...));
*/
export function onceDefined<T, P extends LiteralUnion<keyof T, PropertyKey>>(
target: T, property: P, callback: (v: P extends keyof T ? T[P] : any) => void
): void {
const propertyAsAny = property as any;
if (property in target)
return void callback(target[propertyAsAny]);
Object.defineProperty(target, property, {
set(v) {
delete target[propertyAsAny];
target[propertyAsAny] = v;
callback(v);
},
configurable: true,
enumerable: false
});
}

View file

@ -66,6 +66,14 @@ export function _initWebpack(instance: typeof window.webpackChunkdiscord_app) {
instance.pop(); instance.pop();
} }
if (IS_DEV && !IS_WEB) {
var devToolsOpen = false;
// At this point in time, DiscordNative has not been exposed yet, so setImmediate is needed
setTimeout(() => {
DiscordNative/* just to make sure */?.window.setDevtoolsCallbacks(() => devToolsOpen = true, () => devToolsOpen = false);
}, 0);
}
export function find(filter: FilterFn, getDefault = true, isWaitFor = false) { export function find(filter: FilterFn, getDefault = true, isWaitFor = false) {
if (typeof filter !== "function") if (typeof filter !== "function")
throw new Error("Invalid filter. Expected a function got " + typeof filter); throw new Error("Invalid filter. Expected a function got " + typeof filter);
@ -92,11 +100,13 @@ export function find(filter: FilterFn, getDefault = true, isWaitFor = false) {
if (!isWaitFor) { if (!isWaitFor) {
const err = new Error("Didn't find module matching this filter"); const err = new Error("Didn't find module matching this filter");
if (IS_DEV) { if (IS_DEV) {
if (!devToolsOpen)
// Strict behaviour in DevBuilds to fail early and make sure the issue is found // Strict behaviour in DevBuilds to fail early and make sure the issue is found
throw err; throw err;
} } else {
logger.warn(err); logger.warn(err);
} }
}
return null; return null;
} }
@ -196,11 +206,13 @@ export function bulk(...filterFns: FilterFn[]) {
if (found !== length) { if (found !== length) {
const err = new Error(`Got ${length} filters, but only found ${found} modules!`); const err = new Error(`Got ${length} filters, but only found ${found} modules!`);
if (IS_DEV) { if (IS_DEV) {
if (!devToolsOpen)
// Strict behaviour in DevBuilds to fail early and make sure the issue is found // Strict behaviour in DevBuilds to fail early and make sure the issue is found
throw err; throw err;
} } else {
logger.warn(err); logger.warn(err);
} }
}
return results; return results;
} }
@ -219,10 +231,12 @@ export function findModuleId(code: string) {
const err = new Error("Didn't find module with code:\n" + code); const err = new Error("Didn't find module with code:\n" + code);
if (IS_DEV) { if (IS_DEV) {
if (!devToolsOpen)
// Strict behaviour in DevBuilds to fail early and make sure the issue is found // Strict behaviour in DevBuilds to fail early and make sure the issue is found
throw err; throw err;
} } else {
logger.warn(err); logger.warn(err);
}
return null; return null;
} }