Merge pull request #5662 from electron/electron-require
Always return internal module when requiring electron
This commit is contained in:
commit
9d924bb6d7
9 changed files with 80 additions and 15 deletions
|
@ -1,6 +1,6 @@
|
|||
;(function () {
|
||||
return function (process, require, asarSource) {
|
||||
// Make asar.coffee accessible via "require".
|
||||
// Make asar.js accessible via "require".
|
||||
process.binding('natives').ELECTRON_ASAR = asarSource
|
||||
|
||||
// Monkey-patch the fs module.
|
||||
|
|
|
@ -4,29 +4,30 @@ const Module = require('module')
|
|||
// Clear Node's global search paths.
|
||||
Module.globalPaths.length = 0
|
||||
|
||||
// Clear current and parent(init.coffee)'s search paths.
|
||||
// Clear current and parent(init.js)'s search paths.
|
||||
module.paths = []
|
||||
|
||||
module.parent.paths = []
|
||||
|
||||
// Prevent Node from adding paths outside this app to search paths.
|
||||
Module._nodeModulePaths = function (from) {
|
||||
var dir, i, part, parts, paths, skipOutsidePaths, splitRe, tip
|
||||
from = path.resolve(from)
|
||||
|
||||
// If "from" is outside the app then we do nothing.
|
||||
skipOutsidePaths = from.startsWith(process.resourcesPath)
|
||||
const skipOutsidePaths = from.startsWith(process.resourcesPath)
|
||||
|
||||
// Following logoic is copied from module.js.
|
||||
splitRe = process.platform === 'win32' ? /[\/\\]/ : /\//
|
||||
paths = []
|
||||
parts = from.split(splitRe)
|
||||
// Following logic is copied from module.js.
|
||||
const splitRe = process.platform === 'win32' ? /[\/\\]/ : /\//
|
||||
const paths = []
|
||||
const parts = from.split(splitRe)
|
||||
|
||||
let tip
|
||||
let i
|
||||
for (tip = i = parts.length - 1; i >= 0; tip = i += -1) {
|
||||
part = parts[tip]
|
||||
const part = parts[tip]
|
||||
if (part === 'node_modules') {
|
||||
continue
|
||||
}
|
||||
dir = parts.slice(0, tip + 1).join(path.sep)
|
||||
const dir = parts.slice(0, tip + 1).join(path.sep)
|
||||
if (skipOutsidePaths && !dir.startsWith(process.resourcesPath)) {
|
||||
break
|
||||
}
|
||||
|
@ -34,3 +35,15 @@ Module._nodeModulePaths = function (from) {
|
|||
}
|
||||
return paths
|
||||
}
|
||||
|
||||
// Patch Module._resolveFilename to always require the Electron API when
|
||||
// require('electron') is done.
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,10 +3,9 @@ const ChildProcess = require('child_process')
|
|||
const https = require('https')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const remote = require('electron').remote
|
||||
const {remote} = require('electron')
|
||||
|
||||
const app = remote.require('electron').app
|
||||
const BrowserWindow = remote.require('electron').BrowserWindow
|
||||
const {app, BrowserWindow, ipcMain} = remote
|
||||
const isCI = remote.getGlobal('isCi')
|
||||
|
||||
describe('electron module', function () {
|
||||
|
@ -15,6 +14,36 @@ describe('electron module', function () {
|
|||
require('clipboard')
|
||||
}, /Cannot find module 'clipboard'/)
|
||||
})
|
||||
|
||||
describe('require("electron")', function () {
|
||||
let window = null
|
||||
|
||||
beforeEach(function () {
|
||||
if (window != null) {
|
||||
window.destroy()
|
||||
}
|
||||
window = new BrowserWindow({
|
||||
show: false,
|
||||
width: 400,
|
||||
height: 400
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(function () {
|
||||
if (window != null) {
|
||||
window.destroy()
|
||||
}
|
||||
window = null
|
||||
})
|
||||
|
||||
it('always returns the internal electron module', function (done) {
|
||||
ipcMain.once('answer', function () {
|
||||
done()
|
||||
})
|
||||
window.loadURL('file://' + path.join(__dirname, 'fixtures', 'api', 'electron-module-app', 'index.html'))
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe('app module', function () {
|
||||
|
|
14
spec/fixtures/api/electron-module-app/index.html
vendored
Normal file
14
spec/fixtures/api/electron-module-app/index.html
vendored
Normal file
|
@ -0,0 +1,14 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title></title>
|
||||
<script>
|
||||
require('foo').bar()
|
||||
require('electron').ipcRenderer.send('answer')
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
</body>
|
||||
</html>
|
0
spec/fixtures/api/electron-module-app/node_modules/electron/index.js
generated
vendored
Normal file
0
spec/fixtures/api/electron-module-app/node_modules/electron/index.js
generated
vendored
Normal file
4
spec/fixtures/api/electron-module-app/node_modules/electron/package.json
generated
vendored
Normal file
4
spec/fixtures/api/electron-module-app/node_modules/electron/package.json
generated
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"name": "electron",
|
||||
"main": "index.js"
|
||||
}
|
1
spec/fixtures/api/electron-module-app/node_modules/foo/index.js
generated
vendored
Normal file
1
spec/fixtures/api/electron-module-app/node_modules/foo/index.js
generated
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
exports.bar = function () {}
|
4
spec/fixtures/api/electron-module-app/node_modules/foo/package.json
generated
vendored
Normal file
4
spec/fixtures/api/electron-module-app/node_modules/foo/package.json
generated
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"name": "foo",
|
||||
"main": "index.js"
|
||||
}
|
|
@ -3,7 +3,7 @@ const child_process = require('child_process')
|
|||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const os = require('os')
|
||||
const remote = require('electron').remote
|
||||
const {remote} = require('electron')
|
||||
|
||||
describe('node feature', function () {
|
||||
var fixtures = path.join(__dirname, 'fixtures')
|
||||
|
|
Loading…
Reference in a new issue