2016-03-25 12:57:17 -07:00
|
|
|
const path = require('path')
|
|
|
|
const Module = require('module')
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-01-14 10:35:29 -08:00
|
|
|
// Clear Node's global search paths.
|
2016-03-25 12:57:17 -07:00
|
|
|
Module.globalPaths.length = 0
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-05-23 10:28:32 -07:00
|
|
|
// Clear current and parent(init.js)'s search paths.
|
2016-03-25 12:57:17 -07:00
|
|
|
module.paths = []
|
|
|
|
module.parent.paths = []
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-01-14 10:35:29 -08:00
|
|
|
// Prevent Node from adding paths outside this app to search paths.
|
2016-06-23 15:56:29 -07:00
|
|
|
const resourcesPathWithTrailingSlash = process.resourcesPath + path.sep
|
2016-06-23 15:27:45 -07:00
|
|
|
const originalNodeModulePaths = Module._nodeModulePaths
|
2016-03-25 12:57:17 -07:00
|
|
|
Module._nodeModulePaths = function (from) {
|
2016-06-23 15:27:45 -07:00
|
|
|
const paths = originalNodeModulePaths(from)
|
2016-06-23 15:45:08 -07:00
|
|
|
const fromPath = path.resolve(from) + path.sep
|
2016-06-23 15:56:29 -07:00
|
|
|
// If "from" is outside the app then we do nothing.
|
|
|
|
if (fromPath.startsWith(resourcesPathWithTrailingSlash)) {
|
2016-06-23 15:27:45 -07:00
|
|
|
return paths.filter(function (candidate) {
|
2016-06-23 15:56:29 -07:00
|
|
|
return candidate.startsWith(resourcesPathWithTrailingSlash)
|
2016-06-23 15:27:45 -07:00
|
|
|
})
|
2016-06-23 15:56:29 -07:00
|
|
|
} else {
|
|
|
|
return paths
|
2016-01-11 18:40:23 -08:00
|
|
|
}
|
2016-03-25 12:57:17 -07:00
|
|
|
}
|
2016-05-23 10:56:49 -07:00
|
|
|
|
2016-05-23 15:15:39 -07:00
|
|
|
// Patch Module._resolveFilename to always require the Electron API when
|
|
|
|
// require('electron') is done.
|
2016-05-23 10:56:49 -07:00
|
|
|
const electronPath = path.join(__dirname, '..', process.type, 'api', 'exports', 'electron.js')
|
|
|
|
const originalResolveFilename = Module._resolveFilename
|
|
|
|
Module._resolveFilename = function (request, parent, isMain) {
|
|
|
|
if (request === 'electron') {
|
|
|
|
return electronPath
|
|
|
|
} else {
|
|
|
|
return originalResolveFilename(request, parent, isMain)
|
|
|
|
}
|
|
|
|
}
|