From daea448b6138b1e80315f1105e41f2c3e060c2c6 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 23 May 2016 10:28:32 -0700 Subject: [PATCH 1/8] .coffee -> .js --- lib/common/asar_init.js | 2 +- lib/common/reset-search-paths.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/common/asar_init.js b/lib/common/asar_init.js index 13ee449a42c..b8488aefb3c 100644 --- a/lib/common/asar_init.js +++ b/lib/common/asar_init.js @@ -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. diff --git a/lib/common/reset-search-paths.js b/lib/common/reset-search-paths.js index 30709b56a78..24cbf142666 100644 --- a/lib/common/reset-search-paths.js +++ b/lib/common/reset-search-paths.js @@ -4,7 +4,7 @@ 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 = [] From 566b0676bce5f53fd4c0572518462bd36a0e3c61 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 23 May 2016 10:56:49 -0700 Subject: [PATCH 2/8] Pre-resolve electron require path --- lib/common/reset-search-paths.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/common/reset-search-paths.js b/lib/common/reset-search-paths.js index 24cbf142666..c53364a2a14 100644 --- a/lib/common/reset-search-paths.js +++ b/lib/common/reset-search-paths.js @@ -17,7 +17,7 @@ Module._nodeModulePaths = function (from) { // If "from" is outside the app then we do nothing. skipOutsidePaths = from.startsWith(process.resourcesPath) - // Following logoic is copied from module.js. + // Following logic is copied from module.js. splitRe = process.platform === 'win32' ? /[\/\\]/ : /\// paths = [] parts = from.split(splitRe) @@ -34,3 +34,13 @@ Module._nodeModulePaths = function (from) { } return paths } + +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) + } +} From 19fd841c3060953166533bdbf22d0ea6750618a9 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 23 May 2016 10:58:33 -0700 Subject: [PATCH 3/8] :art: --- lib/common/reset-search-paths.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/common/reset-search-paths.js b/lib/common/reset-search-paths.js index c53364a2a14..bdd2125d5b4 100644 --- a/lib/common/reset-search-paths.js +++ b/lib/common/reset-search-paths.js @@ -6,27 +6,26 @@ Module.globalPaths.length = 0 // 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 logic is copied from module.js. - splitRe = process.platform === 'win32' ? /[\/\\]/ : /\// - paths = [] - parts = from.split(splitRe) - for (tip = i = parts.length - 1; i >= 0; tip = i += -1) { - part = parts[tip] + const splitRe = process.platform === 'win32' ? /[\/\\]/ : /\// + const paths = [] + const parts = from.split(splitRe) + + for (let tip = i = parts.length - 1; i >= 0; tip = i += -1) { + 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 } From fe0ec676239589f0af6e65a633d303705447a717 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 23 May 2016 11:19:33 -0700 Subject: [PATCH 4/8] Add spec for electron module require --- .../api/electron-module-app/index.html | 13 ++++++++ .../node_modules/electron/index.js | 0 .../node_modules/electron/package.json | 4 +++ spec/node-spec.js | 32 ++++++++++++++++++- 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/api/electron-module-app/index.html create mode 100644 spec/fixtures/api/electron-module-app/node_modules/electron/index.js create mode 100644 spec/fixtures/api/electron-module-app/node_modules/electron/package.json diff --git a/spec/fixtures/api/electron-module-app/index.html b/spec/fixtures/api/electron-module-app/index.html new file mode 100644 index 00000000000..1394125534a --- /dev/null +++ b/spec/fixtures/api/electron-module-app/index.html @@ -0,0 +1,13 @@ + + + + + + + + + + + diff --git a/spec/fixtures/api/electron-module-app/node_modules/electron/index.js b/spec/fixtures/api/electron-module-app/node_modules/electron/index.js new file mode 100644 index 00000000000..e69de29bb2d diff --git a/spec/fixtures/api/electron-module-app/node_modules/electron/package.json b/spec/fixtures/api/electron-module-app/node_modules/electron/package.json new file mode 100644 index 00000000000..07e3804ebb1 --- /dev/null +++ b/spec/fixtures/api/electron-module-app/node_modules/electron/package.json @@ -0,0 +1,4 @@ +{ + "name": "electron", + "main": "index.js" +} diff --git a/spec/node-spec.js b/spec/node-spec.js index 0ab98aa67ab..344017e47ea 100644 --- a/spec/node-spec.js +++ b/spec/node-spec.js @@ -3,7 +3,8 @@ 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') +const {BrowserWindow, ipcMain} = remote describe('node feature', function () { var fixtures = path.join(__dirname, 'fixtures') @@ -228,4 +229,33 @@ describe('node feature', function () { require('vm').runInNewContext('') }) }) + + describe.only('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')) + }) + }) }) From 6ac0151e6594853be58092ba6f49a9ad77e3e0ed Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 23 May 2016 11:31:56 -0700 Subject: [PATCH 5/8] Remove lint warnings --- lib/common/reset-search-paths.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/common/reset-search-paths.js b/lib/common/reset-search-paths.js index bdd2125d5b4..72449559b22 100644 --- a/lib/common/reset-search-paths.js +++ b/lib/common/reset-search-paths.js @@ -20,7 +20,9 @@ Module._nodeModulePaths = function (from) { const paths = [] const parts = from.split(splitRe) - for (let tip = i = parts.length - 1; i >= 0; tip = i += -1) { + let tip + let i + for (tip = i = parts.length - 1; i >= 0; tip = i += -1) { const part = parts[tip] if (part === 'node_modules') { continue From 4a413114092c539d9d3ece90ce2b536bafe835a5 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 23 May 2016 13:09:39 -0700 Subject: [PATCH 6/8] Test that calling regular module succeeds --- spec/fixtures/api/electron-module-app/index.html | 1 + .../api/electron-module-app/node_modules/foo/index.js | 1 + .../api/electron-module-app/node_modules/foo/package.json | 4 ++++ spec/node-spec.js | 2 +- 4 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/api/electron-module-app/node_modules/foo/index.js create mode 100644 spec/fixtures/api/electron-module-app/node_modules/foo/package.json diff --git a/spec/fixtures/api/electron-module-app/index.html b/spec/fixtures/api/electron-module-app/index.html index 1394125534a..02bfee95874 100644 --- a/spec/fixtures/api/electron-module-app/index.html +++ b/spec/fixtures/api/electron-module-app/index.html @@ -4,6 +4,7 @@ diff --git a/spec/fixtures/api/electron-module-app/node_modules/foo/index.js b/spec/fixtures/api/electron-module-app/node_modules/foo/index.js new file mode 100644 index 00000000000..11d763e5174 --- /dev/null +++ b/spec/fixtures/api/electron-module-app/node_modules/foo/index.js @@ -0,0 +1 @@ +exports.bar = function () {} diff --git a/spec/fixtures/api/electron-module-app/node_modules/foo/package.json b/spec/fixtures/api/electron-module-app/node_modules/foo/package.json new file mode 100644 index 00000000000..596ac286eca --- /dev/null +++ b/spec/fixtures/api/electron-module-app/node_modules/foo/package.json @@ -0,0 +1,4 @@ +{ + "name": "foo", + "main": "index.js" +} diff --git a/spec/node-spec.js b/spec/node-spec.js index 344017e47ea..a164288f790 100644 --- a/spec/node-spec.js +++ b/spec/node-spec.js @@ -230,7 +230,7 @@ describe('node feature', function () { }) }) - describe.only('require("electron")', function () { + describe('require("electron")', function () { let window = null beforeEach(function () { From 5e2f36387f09e53b07e365a3baa47637704e22a5 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 23 May 2016 13:12:02 -0700 Subject: [PATCH 7/8] Move require spec to api-app-spec --- spec/api-app-spec.js | 35 ++++++++++++++++++++++++++++++++--- spec/node-spec.js | 30 ------------------------------ 2 files changed, 32 insertions(+), 33 deletions(-) diff --git a/spec/api-app-spec.js b/spec/api-app-spec.js index c0469cc3f68..df356ecb905 100644 --- a/spec/api-app-spec.js +++ b/spec/api-app-spec.js @@ -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 () { diff --git a/spec/node-spec.js b/spec/node-spec.js index a164288f790..71fdbac9866 100644 --- a/spec/node-spec.js +++ b/spec/node-spec.js @@ -4,7 +4,6 @@ const fs = require('fs') const path = require('path') const os = require('os') const {remote} = require('electron') -const {BrowserWindow, ipcMain} = remote describe('node feature', function () { var fixtures = path.join(__dirname, 'fixtures') @@ -229,33 +228,4 @@ describe('node feature', function () { require('vm').runInNewContext('') }) }) - - 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')) - }) - }) }) From a4f94b89b4791ff42df88d4a869f0ee47b2255dc Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 23 May 2016 15:15:39 -0700 Subject: [PATCH 8/8] Add comment about Module path --- lib/common/reset-search-paths.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/common/reset-search-paths.js b/lib/common/reset-search-paths.js index 72449559b22..4d09f6c89ca 100644 --- a/lib/common/reset-search-paths.js +++ b/lib/common/reset-search-paths.js @@ -36,6 +36,8 @@ 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) {