electron/lib/common/reset-search-paths.ts

43 lines
1.4 KiB
TypeScript
Raw Normal View History

import * as path from 'path'
2016-03-25 19:57:17 +00:00
const Module = require('module')
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Clear Node's global search paths.
2016-03-25 19:57:17 +00:00
Module.globalPaths.length = 0
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Prevent Node from adding paths outside this app to search paths.
2016-06-23 22:56:29 +00:00
const resourcesPathWithTrailingSlash = process.resourcesPath + path.sep
const originalNodeModulePaths = Module._nodeModulePaths
Module._nodeModulePaths = function (from: string) {
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)) {
return paths.filter(function (candidate) {
2016-06-23 22:56:29 +00:00
return candidate.startsWith(resourcesPathWithTrailingSlash)
})
2016-06-23 22:56:29 +00:00
} else {
return paths
2016-01-12 02:40:23 +00:00
}
2016-03-25 19:57:17 +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
const electronModule = new Module('electron', null)
electronModule.id = 'electron'
electronModule.loaded = true
electronModule.filename = 'electron'
Object.defineProperty(electronModule, 'exports', {
get: () => require('electron')
})
Module._cache['electron'] = electronModule
2016-05-23 17:56:49 +00:00
const originalResolveFilename = Module._resolveFilename
Module._resolveFilename = function (request: string, parent: NodeModule, isMain: boolean) {
2016-05-23 17:56:49 +00:00
if (request === 'electron') {
2019-06-02 20:03:03 +00:00
return 'electron'
2016-05-23 17:56:49 +00:00
} else {
return originalResolveFilename(request, parent, isMain)
}
}