From 44bd93589a5ee3732d5a51432830970a433a0674 Mon Sep 17 00:00:00 2001 From: Thiago de Arruda Date: Mon, 6 Mar 2017 11:58:10 -0300 Subject: [PATCH] Refactor electron exports to keep module list in separate files --- filenames.gypi | 3 + lib/browser/api/exports/electron.js | 134 ++------------------------- lib/browser/api/module-list.js | 24 +++++ lib/common/api/exports/electron.js | 58 ++---------- lib/common/api/module-list.js | 12 +++ lib/renderer/api/exports/electron.js | 40 ++------ lib/renderer/api/module-list.js | 8 ++ lib/renderer/api/remote.js | 36 ++----- 8 files changed, 79 insertions(+), 236 deletions(-) create mode 100644 lib/browser/api/module-list.js create mode 100644 lib/common/api/module-list.js create mode 100644 lib/renderer/api/module-list.js diff --git a/filenames.gypi b/filenames.gypi index 6ac732c24a30..c7d3adb36940 100644 --- a/filenames.gypi +++ b/filenames.gypi @@ -22,6 +22,7 @@ 'lib/browser/api/menu.js', 'lib/browser/api/menu-item.js', 'lib/browser/api/menu-item-roles.js', + 'lib/browser/api/module-list.js', 'lib/browser/api/navigation-controller.js', 'lib/browser/api/net.js', 'lib/browser/api/power-monitor.js', @@ -47,6 +48,7 @@ 'lib/common/api/deprecations.js', 'lib/common/api/is-promise.js', 'lib/common/api/exports/electron.js', + 'lib/common/api/module-list.js', 'lib/common/api/native-image.js', 'lib/common/api/shell.js', 'lib/common/init.js', @@ -66,6 +68,7 @@ 'lib/renderer/api/exports/electron.js', 'lib/renderer/api/ipc-renderer.js', 'lib/renderer/api/ipc-renderer-setup.js', + 'lib/renderer/api/module-list.js', 'lib/renderer/api/remote.js', 'lib/renderer/api/screen.js', 'lib/renderer/api/web-frame.js', diff --git a/lib/browser/api/exports/electron.js b/lib/browser/api/exports/electron.js index 3f4059521752..753aa30fd5b0 100644 --- a/lib/browser/api/exports/electron.js +++ b/lib/browser/api/exports/electron.js @@ -1,131 +1,13 @@ const common = require('../../../common/api/exports/electron') +// since browser module list is also used in renderer, keep it separate. +const moduleList = require('../module-list') // Import common modules. common.defineProperties(exports) -Object.defineProperties(exports, { - // Browser side modules, please sort alphabetically. - // Any modules added here must also be added to the browserModules array - // in remote.js - app: { - enumerable: true, - get: function () { - return require('../app') - } - }, - autoUpdater: { - enumerable: true, - get: function () { - return require('../auto-updater') - } - }, - BrowserWindow: { - enumerable: true, - get: function () { - return require('../browser-window') - } - }, - contentTracing: { - enumerable: true, - get: function () { - return require('../content-tracing') - } - }, - dialog: { - enumerable: true, - get: function () { - return require('../dialog') - } - }, - globalShortcut: { - enumerable: true, - get: function () { - return require('../global-shortcut') - } - }, - ipcMain: { - enumerable: true, - get: function () { - return require('../ipc-main') - } - }, - Menu: { - enumerable: true, - get: function () { - return require('../menu') - } - }, - MenuItem: { - enumerable: true, - get: function () { - return require('../menu-item') - } - }, - net: { - enumerable: true, - get: function () { - return require('../net') - } - }, - powerMonitor: { - enumerable: true, - get: function () { - return require('../power-monitor') - } - }, - powerSaveBlocker: { - enumerable: true, - get: function () { - return require('../power-save-blocker') - } - }, - protocol: { - enumerable: true, - get: function () { - return require('../protocol') - } - }, - screen: { - enumerable: true, - get: function () { - return require('../screen') - } - }, - session: { - enumerable: true, - get: function () { - return require('../session') - } - }, - systemPreferences: { - enumerable: true, - get: function () { - return require('../system-preferences') - } - }, - TouchBar: { - enumerable: true, - get: function () { - return require('../touch-bar') - } - }, - Tray: { - enumerable: true, - get: function () { - return require('../tray') - } - }, - webContents: { - enumerable: true, - get: function () { - return require('../web-contents') - } - }, - - // The internal modules, invisible unless you know their names. - NavigationController: { - get: function () { - return require('../navigation-controller') - } - } -}) +for (const module of moduleList) { + Object.defineProperty(exports, module.name, { + enumerable: !module.private, + get: () => require(`../${module.file}`) + }) +} diff --git a/lib/browser/api/module-list.js b/lib/browser/api/module-list.js new file mode 100644 index 000000000000..3274f0b6d4fb --- /dev/null +++ b/lib/browser/api/module-list.js @@ -0,0 +1,24 @@ +// Browser side modules, please sort alphabetically. +module.exports = [ + {name: 'app', file: 'app'}, + {name: 'autoUpdater', file: 'auto-updater'}, + {name: 'BrowserWindow', file: 'browser-window'}, + {name: 'contentTracing', file: 'content-tracing'}, + {name: 'dialog', file: 'dialog'}, + {name: 'globalShortcut', file: 'global-shortcut'}, + {name: 'ipcMain', file: 'ipc-main'}, + {name: 'Menu', file: 'menu'}, + {name: 'MenuItem', file: 'menu-item'}, + {name: 'net', file: 'net'}, + {name: 'powerMonitor', file: 'power-monitor'}, + {name: 'powerSaveBlocker', file: 'power-save-blocker'}, + {name: 'protocol', file: 'protocol'}, + {name: 'screen', file: 'screen'}, + {name: 'session', file: 'session'}, + {name: 'systemPreferences', file: 'system-preferences'}, + {name: 'TouchBar', file: 'touch-bar'}, + {name: 'Tray', file: 'tray'}, + {name: 'webContents', file: 'web-contents'}, + // The internal modules, invisible unless you know their names. + {name: 'NavigationController', file: 'navigation-controller', private: true} +] diff --git a/lib/common/api/exports/electron.js b/lib/common/api/exports/electron.js index efa44d452cc6..88b1f785878b 100644 --- a/lib/common/api/exports/electron.js +++ b/lib/common/api/exports/electron.js @@ -1,53 +1,13 @@ +const moduleList = require('../module-list') + // Attaches properties to |exports|. exports.defineProperties = function (exports) { - return Object.defineProperties(exports, { - // Common modules, please sort with alphabet order. - clipboard: { - // Must be enumerable, otherwise it woulde be invisible to remote module. - enumerable: true, - get: function () { - return require('../clipboard') - } - }, - crashReporter: { - enumerable: true, - get: function () { - return require('../crash-reporter') - } - }, - nativeImage: { - enumerable: true, - get: function () { - return require('../native-image') - } - }, - shell: { - enumerable: true, - get: function () { - return require('../shell') - } - }, - - // The internal modules, invisible unless you know their names. - CallbacksRegistry: { - get: function () { - return require('../callbacks-registry') - } - }, - deprecate: { - get: function () { - return require('../deprecate') - } - }, - deprecations: { - get: function () { - return require('../deprecations') - } - }, - isPromise: { - get: function () { - return require('../is-promise') - } + const descriptors = {} + for (const module of moduleList) { + descriptors[module.name] = { + enumerable: !module.private, + get: () => require(`../${module.file}`) } - }) + } + return Object.defineProperties(exports, descriptors) } diff --git a/lib/common/api/module-list.js b/lib/common/api/module-list.js new file mode 100644 index 000000000000..00c208c80574 --- /dev/null +++ b/lib/common/api/module-list.js @@ -0,0 +1,12 @@ +// Common modules, please sort alphabetically +module.exports = [ + {name: 'clipboard', file: 'clipboard'}, + {name: 'crashReporter', file: 'crash-reporter'}, + {name: 'nativeImage', file: 'native-image'}, + {name: 'shell', file: 'shell'}, + // The internal modules, invisible unless you know their names. + {name: 'CallbacksRegistry', file: 'callbacks-registry', private: true}, + {name: 'deprecate', file: 'deprecate', private: true}, + {name: 'deprecations', file: 'deprecations', private: true}, + {name: 'isPromise', file: 'is-promise', private: true} +] diff --git a/lib/renderer/api/exports/electron.js b/lib/renderer/api/exports/electron.js index 8e05adc980c4..d9b377a985ab 100644 --- a/lib/renderer/api/exports/electron.js +++ b/lib/renderer/api/exports/electron.js @@ -1,38 +1,12 @@ const common = require('../../../common/api/exports/electron') +const moduleList = require('../module-list') // Import common modules. common.defineProperties(exports) -Object.defineProperties(exports, { - // Renderer side modules, please sort with alphabet order. - desktopCapturer: { - enumerable: true, - get: function () { - return require('../desktop-capturer') - } - }, - ipcRenderer: { - enumerable: true, - get: function () { - return require('../ipc-renderer') - } - }, - remote: { - enumerable: true, - get: function () { - return require('../remote') - } - }, - screen: { - enumerable: true, - get: function () { - return require('../screen') - } - }, - webFrame: { - enumerable: true, - get: function () { - return require('../web-frame') - } - } -}) +for (const module of moduleList) { + Object.defineProperty(exports, module.name, { + enumerable: !module.private, + get: () => require(`../${module.file}`) + }) +} diff --git a/lib/renderer/api/module-list.js b/lib/renderer/api/module-list.js new file mode 100644 index 000000000000..4d0e1959def8 --- /dev/null +++ b/lib/renderer/api/module-list.js @@ -0,0 +1,8 @@ +// Renderer side modules, please sort alphabetically. +module.exports = [ + {name: 'desktopCapturer', file: 'desktop-capturer'}, + {name: 'ipcRenderer', file: 'ipc-renderer'}, + {name: 'remote', file: 'remote'}, + {name: 'screen', file: 'screen'}, + {name: 'webFrame', file: 'web-frame'} +] diff --git a/lib/renderer/api/remote.js b/lib/renderer/api/remote.js index fd94390d7725..8277e16c2182 100644 --- a/lib/renderer/api/remote.js +++ b/lib/renderer/api/remote.js @@ -340,32 +340,12 @@ const addBuiltinProperty = (name) => { }) } -// Add each browser module name as a property -// This list should match the exports in browser/api/exports/electron.js -const browserModules = [ - 'app', - 'autoUpdater', - 'BrowserWindow', - 'contentTracing', - 'dialog', - 'globalShortcut', - 'ipcMain', - 'Menu', - 'MenuItem', - 'net', - 'powerMonitor', - 'powerSaveBlocker', - 'protocol', - 'screen', - 'session', - 'systemPreferences', - 'TouchBar', - 'Tray', - 'webContents' -] -browserModules.forEach(addBuiltinProperty) +const browserModules = + require('../../common/api/module-list').concat( + require('../../browser/api/module-list')) -// Add each common module name as a property -const commonModules = {} -require('../../common/api/exports/electron').defineProperties(commonModules) -Object.getOwnPropertyNames(commonModules).forEach(addBuiltinProperty) +// And add a helper receiver for each one. +browserModules + .filter((m) => !m.private) + .map((m) => m.name) + .forEach(addBuiltinProperty)