rewrite: replace requireNative with globalPaths workaround

Added Module.globalPaths workaround (reintroduce native usage instead of our own util using it) is needed as Discord's overlay code depends on their own implementation of this. Also removed requireNative and replace with just require since no longer needed with this hack anyway.
This commit is contained in:
Ducko 2023-04-10 20:59:54 +01:00
parent d8035e14b7
commit 8cf5233e94
3 changed files with 8 additions and 8 deletions

2
src/bootstrap.js vendored
View File

@ -48,7 +48,7 @@ const startCore = () => {
});
});
desktopCore = require('./utils/requireNative')('discord_desktop_core');
desktopCore = require('discord_desktop_core');
desktopCore.startup({
splashScreen: splash,

View File

@ -26,9 +26,15 @@ if (process.platform === 'win32') try {
for (const m of require('fs').readdirSync(b)) M.globalPaths.push(join(b, m)); // For each module dir, add to globalPaths
} catch { log('Init', 'Failed to QS globalPaths') }
// inject Module.globalPaths into resolve lookups as it was removed in Electron >=17 and Discord depend on this workaround
const rlp = M._resolveLookupPaths;
M._resolveLookupPaths = (request, parent) => {
if (parent?.paths?.length > 0) parent.paths = parent.paths.concat(M.globalPaths);
return rlp(request, parent);
};
if (process.argv.includes('--overlay-host')) { // If overlay
require('./utils/requireNative')('discord_overlay2', 'standalone_host.js'); // Start overlay
require('discord_overlay2/standalone_host.js'); // Start overlay
} else {
require('./bootstrap')(); // Start bootstrap
}

View File

@ -1,6 +0,0 @@
// Custom requireNative as Electron >=17 breaks Module.globalPaths for some reason
// For Updater v2: get direct path in globalPaths (g[0])
// For Module Updater: get root path for all modules in globalPaths
const g = require('module').globalPaths.reverse();
module.exports = (n, e = '') => require(require('path').join(g.find(x => x.includes(n)) ?? g[0], n, e));