2019-02-12 14:22:33 +00:00
|
|
|
import * as path from 'path'
|
2018-09-22 12:28:50 +00:00
|
|
|
|
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-05-23 17:28:32 +00:00
|
|
|
// Clear current and parent(init.js)'s search paths.
|
2016-03-25 19:57:17 +00:00
|
|
|
module.paths = []
|
2019-02-12 14:22:33 +00:00
|
|
|
module.parent!.paths = []
|
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
|
2016-06-23 22:27:45 +00:00
|
|
|
const originalNodeModulePaths = Module._nodeModulePaths
|
2019-02-12 14:22:33 +00:00
|
|
|
Module._nodeModulePaths = function (from: string) {
|
|
|
|
const paths: string[] = originalNodeModulePaths(from)
|
2016-06-23 22:45:08 +00:00
|
|
|
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) {
|
2016-06-23 22:56:29 +00:00
|
|
|
return candidate.startsWith(resourcesPathWithTrailingSlash)
|
2016-06-23 22:27:45 +00:00
|
|
|
})
|
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
|
|
|
|
2018-09-20 03:43:26 +00:00
|
|
|
const BASE_INTERNAL_PATH = path.resolve(__dirname, '..')
|
|
|
|
const INTERNAL_MODULE_PREFIX = '@electron/internal/'
|
|
|
|
|
2016-05-23 22:15:39 +00:00
|
|
|
// Patch Module._resolveFilename to always require the Electron API when
|
|
|
|
// require('electron') is done.
|
2019-02-12 14:22:33 +00:00
|
|
|
const electronPath = path.join(__dirname, '..', process.type!, 'api', 'exports', 'electron.js')
|
2016-05-23 17:56:49 +00:00
|
|
|
const originalResolveFilename = Module._resolveFilename
|
2019-02-12 14:22:33 +00:00
|
|
|
Module._resolveFilename = function (request: string, parent: NodeModule, isMain: boolean) {
|
2016-05-23 17:56:49 +00:00
|
|
|
if (request === 'electron') {
|
|
|
|
return electronPath
|
2018-09-20 03:43:26 +00:00
|
|
|
} else if (request.startsWith(INTERNAL_MODULE_PREFIX) && request.length > INTERNAL_MODULE_PREFIX.length) {
|
|
|
|
const slicedRequest = request.slice(INTERNAL_MODULE_PREFIX.length)
|
|
|
|
return path.resolve(BASE_INTERNAL_PATH, `${slicedRequest}${slicedRequest.endsWith('.js') ? '' : '.js'}`)
|
2016-05-23 17:56:49 +00:00
|
|
|
} else {
|
|
|
|
return originalResolveFilename(request, parent, isMain)
|
|
|
|
}
|
|
|
|
}
|