2020-03-20 20:28:31 +00:00
|
|
|
import * as path from 'path';
|
2018-09-22 12:28:50 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const Module = require('module');
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2020-10-23 18:49:52 +00:00
|
|
|
// We do not want to allow use of the VM module in the renderer process as
|
|
|
|
// it conflicts with Blink's V8::Context internal logic.
|
|
|
|
if (process.type === 'renderer') {
|
|
|
|
const _load = Module._load;
|
|
|
|
Module._load = function (request: string) {
|
|
|
|
if (request === 'vm') {
|
|
|
|
console.warn('The vm module of Node.js is deprecated in the renderer process and will be removed.');
|
|
|
|
}
|
|
|
|
return _load.apply(this, arguments);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Prevent Node from adding paths outside this app to search paths.
|
2020-03-20 20:28:31 +00:00
|
|
|
const resourcesPathWithTrailingSlash = process.resourcesPath + path.sep;
|
|
|
|
const originalNodeModulePaths = Module._nodeModulePaths;
|
2019-02-12 14:22:33 +00:00
|
|
|
Module._nodeModulePaths = function (from: string) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const paths: string[] = originalNodeModulePaths(from);
|
|
|
|
const fromPath = path.resolve(from) + path.sep;
|
2016-06-23 22:56:29 +00:00
|
|
|
// If "from" is outside the app then we do nothing.
|
|
|
|
if (fromPath.startsWith(resourcesPathWithTrailingSlash)) {
|
2016-06-23 22:27:45 +00:00
|
|
|
return paths.filter(function (candidate) {
|
2020-03-20 20:28:31 +00:00
|
|
|
return candidate.startsWith(resourcesPathWithTrailingSlash);
|
|
|
|
});
|
2016-06-23 22:56:29 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
return paths;
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2016-05-23 17:56:49 +00:00
|
|
|
|
2019-06-02 20:03:03 +00:00
|
|
|
// Make a fake Electron module that we will insert into the module cache
|
2020-04-07 00:04:09 +00:00
|
|
|
const makeElectronModule = (name: string) => {
|
|
|
|
const electronModule = new Module('electron', null);
|
|
|
|
electronModule.id = 'electron';
|
|
|
|
electronModule.loaded = true;
|
|
|
|
electronModule.filename = name;
|
|
|
|
Object.defineProperty(electronModule, 'exports', {
|
|
|
|
get: () => require('electron')
|
|
|
|
});
|
|
|
|
Module._cache[name] = electronModule;
|
|
|
|
};
|
2019-06-02 20:03:03 +00:00
|
|
|
|
2020-04-07 00:04:09 +00:00
|
|
|
makeElectronModule('electron');
|
|
|
|
makeElectronModule('electron/common');
|
|
|
|
if (process.type === 'browser') {
|
|
|
|
makeElectronModule('electron/main');
|
|
|
|
}
|
|
|
|
if (process.type === 'renderer') {
|
|
|
|
makeElectronModule('electron/renderer');
|
|
|
|
}
|
2018-09-20 03:43:26 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const originalResolveFilename = Module._resolveFilename;
|
2022-10-06 10:14:03 +00:00
|
|
|
const electronModuleNames = new Set(['electron', 'electron/main', 'electron/renderer', 'electron/common']);
|
2020-10-14 17:13:45 +00:00
|
|
|
Module._resolveFilename = function (request: string, parent: NodeModule, isMain: boolean, options?: { paths: Array<string>}) {
|
2022-10-06 10:14:03 +00:00
|
|
|
if (electronModuleNames.has(request)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
return 'electron';
|
2016-05-23 17:56:49 +00:00
|
|
|
} else {
|
2020-10-14 17:13:45 +00:00
|
|
|
return originalResolveFilename(request, parent, isMain, options);
|
2016-05-23 17:56:49 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|