feat: expose electron/{process} typed modules (#22937)

* feat: expose electron/{process} typed modules

* chore: update imports for common modules

* chore: update typescript generator

* chore: remap electron/* to the internal packages
This commit is contained in:
Samuel Attard 2020-04-06 17:04:09 -07:00 коммит произвёл GitHub
Родитель 5b53f1a39c
Коммит 0d2e967960
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
56 изменённых файлов: 97 добавлений и 83 удалений

Просмотреть файл

@ -111,7 +111,11 @@ A `Boolean` that controls whether or not process warnings printed to `stderr` in
### `process.type` _Readonly_
A `String` representing the current process's type, can be `"browser"` (i.e. main process), `"renderer"`, or `"worker"` (i.e. web worker).
A `String` representing the current process's type, can be:
* `browser` - The main process
* `renderer` - A renderer process
* `worker` - In a web worker
### `process.versions.chrome` _Readonly_

Просмотреть файл

@ -22,19 +22,29 @@ Module._nodeModulePaths = function (from: string) {
};
// Make a fake Electron module that we will insert into the module cache
const electronModule = new Module('electron', null);
electronModule.id = 'electron';
electronModule.loaded = true;
electronModule.filename = 'electron';
Object.defineProperty(electronModule, 'exports', {
get: () => require('electron')
});
const makeElectronModule = (name: string) => {
const electronModule = new Module('electron', null);
electronModule.id = 'electron';
electronModule.loaded = true;
electronModule.filename = name;
Object.defineProperty(electronModule, 'exports', {
get: () => require('electron')
});
Module._cache[name] = electronModule;
};
Module._cache.electron = electronModule;
makeElectronModule('electron');
makeElectronModule('electron/common');
if (process.type === 'browser') {
makeElectronModule('electron/main');
}
if (process.type === 'renderer') {
makeElectronModule('electron/renderer');
}
const originalResolveFilename = Module._resolveFilename;
Module._resolveFilename = function (request: string, parent: NodeModule, isMain: boolean) {
if (request === 'electron') {
if (request === 'electron' || request.startsWith('electron/')) {
return 'electron';
} else {
return originalResolveFilename(request, parent, isMain);

Просмотреть файл

@ -5,7 +5,7 @@
"description": "Build cross platform desktop apps with JavaScript, HTML, and CSS",
"devDependencies": {
"@electron/docs-parser": "^0.7.2",
"@electron/typescript-definitions": "^8.6.4",
"@electron/typescript-definitions": "^8.7.2",
"@octokit/rest": "^16.3.2",
"@primer/octicons": "^9.1.1",
"@types/basic-auth": "^1.1.3",

6
spec-main/ambient.d.ts поставляемый
Просмотреть файл

@ -29,6 +29,12 @@ declare namespace Electron {
class WebContentsView {
constructor(webContents: WebContents)
}
namespace Main {
class TopLevelWindow extends Electron.TopLevelWindow {}
class View extends Electron.View {}
class WebContentsView extends Electron.WebContentsView {}
}
}
declare module 'dbus-native';

Просмотреть файл

@ -5,7 +5,7 @@ import * as http from 'http';
import * as net from 'net';
import * as fs from 'fs';
import * as path from 'path';
import { app, BrowserWindow, Menu, session } from 'electron';
import { app, BrowserWindow, Menu, session } from 'electron/main';
import { emittedOnce } from './events-helpers';
import { closeWindow, closeAllWindows } from './window-helpers';
import { ifdescribe } from './spec-helpers';

Просмотреть файл

@ -1,4 +1,4 @@
import { autoUpdater } from 'electron';
import { autoUpdater } from 'electron/main';
import { expect } from 'chai';
import { ifit, ifdescribe } from './spec-helpers';

Просмотреть файл

@ -2,7 +2,7 @@ import { expect } from 'chai';
import * as ChildProcess from 'child_process';
import * as path from 'path';
import { emittedOnce } from './events-helpers';
import { BrowserView, BrowserWindow } from 'electron';
import { BrowserView, BrowserWindow } from 'electron/main';
import { closeWindow } from './window-helpers';
describe('BrowserView module', () => {

Просмотреть файл

@ -1,7 +1,7 @@
import { expect } from 'chai';
import * as path from 'path';
import { ipcMain, BrowserWindow, WebPreferences, app } from 'electron';
import { ipcMain, BrowserWindow, WebPreferences, app } from 'electron/main';
import { closeWindow } from './window-helpers';
describe('BrowserWindow with affinity module', () => {

Просмотреть файл

@ -5,7 +5,7 @@ import * as os from 'os';
import * as qs from 'querystring';
import * as http from 'http';
import { AddressInfo } from 'net';
import { app, BrowserWindow, BrowserView, ipcMain, OnBeforeSendHeadersListenerDetails, protocol, screen, webContents, session, WebContents } from 'electron';
import { app, BrowserWindow, BrowserView, ipcMain, OnBeforeSendHeadersListenerDetails, protocol, screen, webContents, session, WebContents } from 'electron/main';
import { emittedOnce } from './events-helpers';
import { ifit, ifdescribe } from './spec-helpers';

Просмотреть файл

@ -1,5 +1,5 @@
import { expect } from 'chai';
import { app, contentTracing, TraceConfig, TraceCategoriesAndOptions } from 'electron';
import { app, contentTracing, TraceConfig, TraceCategoriesAndOptions } from 'electron/main';
import * as fs from 'fs';
import * as path from 'path';
import { ifdescribe } from './spec-helpers';

Просмотреть файл

@ -1,4 +1,5 @@
import { contextBridge, BrowserWindow, ipcMain } from 'electron';
import { BrowserWindow, ipcMain } from 'electron/main';
import { contextBridge } from 'electron/renderer';
import { expect } from 'chai';
import * as fs from 'fs-extra';
import * as os from 'os';
@ -45,10 +46,10 @@ describe('contextBridge', () => {
const generateTests = (useSandbox: boolean) => {
describe(`with sandbox=${useSandbox}`, () => {
const makeBindingWindow = async (bindingCreator: Function) => {
const preloadContent = `const electron_1 = require('electron');
const preloadContent = `const renderer_1 = require('electron');
${useSandbox ? '' : `require('v8').setFlagsFromString('--expose_gc');
const gc=require('vm').runInNewContext('gc');
electron_1.contextBridge.exposeInMainWorld('GCRunner', {
renderer_1.contextBridge.exposeInMainWorld('GCRunner', {
run: () => gc()
});`}
(${bindingCreator.toString()})();`;

Просмотреть файл

@ -7,7 +7,8 @@ import * as path from 'path';
import { ifdescribe, ifit } from './spec-helpers';
import * as temp from 'temp';
import * as url from 'url';
import { ipcMain, app, BrowserWindow, crashReporter, BrowserWindowConstructorOptions } from 'electron';
import { ipcMain, app, BrowserWindow, BrowserWindowConstructorOptions } from 'electron/main';
import { crashReporter } from 'electron/common';
import { AddressInfo } from 'net';
import { closeWindow, closeAllWindows } from './window-helpers';
import { EventEmitter } from 'events';

Просмотреть файл

@ -2,7 +2,7 @@ import { expect } from 'chai';
import * as http from 'http';
import * as path from 'path';
import { AddressInfo } from 'net';
import { BrowserWindow } from 'electron';
import { BrowserWindow } from 'electron/main';
import { closeAllWindows } from './window-helpers';
import { emittedOnce } from './events-helpers';

Просмотреть файл

@ -1,5 +1,5 @@
import { expect } from 'chai';
import { deprecate } from 'electron';
import { deprecate } from 'electron/main';
describe('deprecate', () => {
let throwing: boolean;

Просмотреть файл

@ -1,5 +1,6 @@
import { expect } from 'chai';
import { desktopCapturer, screen, BrowserWindow, SourcesOptions } from 'electron';
import { screen, BrowserWindow, SourcesOptions } from 'electron/main';
import { desktopCapturer } from 'electron/renderer';
import { emittedOnce } from './events-helpers';
import { ifdescribe, ifit } from './spec-helpers';
import { closeAllWindows } from './window-helpers';

Просмотреть файл

@ -1,5 +1,5 @@
import { expect } from 'chai';
import { dialog, BrowserWindow } from 'electron';
import { dialog, BrowserWindow } from 'electron/main';
import { closeAllWindows } from './window-helpers';
import { ifit } from './spec-helpers';

Просмотреть файл

@ -1,5 +1,5 @@
import { expect } from 'chai';
import { globalShortcut } from 'electron';
import { globalShortcut } from 'electron/main';
import { ifdescribe } from './spec-helpers';
ifdescribe(process.platform !== 'win32')('globalShortcut module', () => {

Просмотреть файл

@ -1,5 +1,5 @@
import { expect } from 'chai';
import { inAppPurchase } from 'electron';
import { inAppPurchase } from 'electron/main';
describe('inAppPurchase module', function () {
if (process.platform !== 'darwin') return;

Просмотреть файл

@ -3,7 +3,7 @@ import * as path from 'path';
import * as cp from 'child_process';
import { closeAllWindows } from './window-helpers';
import { emittedOnce } from './events-helpers';
import { ipcMain, BrowserWindow } from 'electron';
import { ipcMain, BrowserWindow } from 'electron/main';
describe('ipc main module', () => {
const fixtures = path.join(__dirname, 'fixtures');

Просмотреть файл

@ -1,6 +1,6 @@
import { expect } from 'chai';
import * as path from 'path';
import { ipcMain, BrowserWindow, WebContents, WebPreferences, webContents } from 'electron';
import { ipcMain, BrowserWindow, WebContents, WebPreferences, webContents } from 'electron/main';
import { emittedOnce } from './events-helpers';
import { closeWindow } from './window-helpers';

Просмотреть файл

@ -1,6 +1,6 @@
import { EventEmitter } from 'events';
import { expect } from 'chai';
import { BrowserWindow, ipcMain, IpcMainInvokeEvent, MessageChannelMain } from 'electron';
import { BrowserWindow, ipcMain, IpcMainInvokeEvent, MessageChannelMain } from 'electron/main';
import { closeAllWindows } from './window-helpers';
import { emittedOnce } from './events-helpers';

Просмотреть файл

@ -1,4 +1,4 @@
import { BrowserWindow, app, Menu, MenuItem, MenuItemConstructorOptions } from 'electron';
import { BrowserWindow, app, Menu, MenuItem, MenuItemConstructorOptions } from 'electron/main';
import { expect } from 'chai';
import { closeAllWindows } from './window-helpers';
const { roleList, execute } = require('../lib/browser/api/menu-item-roles');

Просмотреть файл

@ -1,7 +1,7 @@
import * as cp from 'child_process';
import * as path from 'path';
import { expect } from 'chai';
import { BrowserWindow, Menu, MenuItem } from 'electron';
import { BrowserWindow, Menu, MenuItem } from 'electron/main';
import { sortMenuItems } from '../lib/browser/api/menu-utils';
import { emittedOnce } from './events-helpers';
import { ifit } from './spec-helpers';

Просмотреть файл

@ -1,5 +1,5 @@
import { expect } from 'chai';
import { nativeTheme, systemPreferences, BrowserWindow } from 'electron';
import { nativeTheme, systemPreferences, BrowserWindow } from 'electron/main';
import * as os from 'os';
import * as path from 'path';
import * as semver from 'semver';

Просмотреть файл

@ -4,7 +4,7 @@ import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as ChildProcess from 'child_process';
import { session, net } from 'electron';
import { session, net } from 'electron/main';
import { Socket, AddressInfo } from 'net';
import { ifit } from './spec-helpers';
import { emittedOnce } from './events-helpers';

Просмотреть файл

@ -1,5 +1,5 @@
import { expect } from 'chai';
import { net, session, ClientRequest, BrowserWindow } from 'electron';
import { net, session, ClientRequest, BrowserWindow } from 'electron/main';
import * as http from 'http';
import * as url from 'url';
import { AddressInfo, Socket } from 'net';

Просмотреть файл

@ -8,7 +8,7 @@
import { expect } from 'chai';
import * as dbus from 'dbus-native';
import { app } from 'electron';
import { app } from 'electron/main';
import { ifdescribe } from './spec-helpers';
import { promisify } from 'util';

Просмотреть файл

@ -1,5 +1,5 @@
import { expect } from 'chai';
import { Notification } from 'electron';
import { Notification } from 'electron/main';
import { emittedOnce } from './events-helpers';
import { ifit } from './spec-helpers';

Просмотреть файл

@ -1,5 +1,5 @@
import { expect } from 'chai';
import { powerSaveBlocker } from 'electron';
import { powerSaveBlocker } from 'electron/main';
describe('powerSaveBlocker module', () => {
it('can be started and stopped', () => {

Просмотреть файл

@ -1,5 +1,5 @@
import { expect } from 'chai';
import { protocol, webContents, WebContents, session, BrowserWindow, ipcMain } from 'electron';
import { protocol, webContents, WebContents, session, BrowserWindow, ipcMain } from 'electron/main';
import { promisify } from 'util';
import { AddressInfo } from 'net';
import * as ChildProcess from 'child_process';

Просмотреть файл

@ -3,7 +3,7 @@ import { expect } from 'chai';
import { closeAllWindows } from './window-helpers';
import { ifdescribe } from './spec-helpers';
import { ipcMain, BrowserWindow } from 'electron';
import { ipcMain, BrowserWindow } from 'electron/main';
import { emittedOnce } from './events-helpers';
const features = process.electronBinding('features');

Просмотреть файл

@ -1,5 +1,5 @@
import { expect } from 'chai';
import { screen } from 'electron';
import { screen } from 'electron/main';
describe('screen module', () => {
describe('screen.getCursorScreenPoint()', () => {

Просмотреть файл

@ -1,7 +1,7 @@
import * as fs from 'fs';
import * as http from 'http';
import * as path from 'path';
import { session, BrowserWindow } from 'electron';
import { session, BrowserWindow } from 'electron/main';
import { expect } from 'chai';
import { v4 } from 'uuid';
import { AddressInfo } from 'net';

Просмотреть файл

@ -4,7 +4,7 @@ import * as https from 'https';
import * as path from 'path';
import * as fs from 'fs';
import * as ChildProcess from 'child_process';
import { session, BrowserWindow, net, ipcMain, Session } from 'electron';
import { session, BrowserWindow, net, ipcMain, Session } from 'electron/main';
import * as send from 'send';
import * as auth from 'basic-auth';
import { closeAllWindows } from './window-helpers';

Просмотреть файл

@ -1,4 +1,5 @@
import { BrowserWindow, shell } from 'electron';
import { BrowserWindow } from 'electron/main';
import { shell } from 'electron/common';
import { closeAllWindows } from './window-helpers';
import { emittedOnce } from './events-helpers';
import * as http from 'http';

Просмотреть файл

@ -3,7 +3,7 @@ import * as path from 'path';
import * as http from 'http';
import { emittedNTimes, emittedOnce } from './events-helpers';
import { closeWindow } from './window-helpers';
import { app, BrowserWindow, ipcMain } from 'electron';
import { app, BrowserWindow, ipcMain } from 'electron/main';
import { AddressInfo } from 'net';
import { ifdescribe } from './spec-helpers';

Просмотреть файл

@ -1,5 +1,5 @@
import { expect } from 'chai';
import { systemPreferences } from 'electron';
import { systemPreferences } from 'electron/main';
import { ifdescribe } from './spec-helpers';
describe('systemPreferences module', () => {

Просмотреть файл

@ -1,5 +1,5 @@
import * as path from 'path';
import { BrowserWindow, TouchBar } from 'electron';
import { BrowserWindow, TouchBar } from 'electron/main';
import { closeWindow } from './window-helpers';
import { expect } from 'chai';

Просмотреть файл

@ -1,5 +1,6 @@
import { expect } from 'chai';
import { Menu, Tray, nativeImage } from 'electron';
import { Menu, Tray } from 'electron/main';
import { nativeImage } from 'electron/common';
import { ifdescribe, ifit } from './spec-helpers';
import * as path from 'path';

Просмотреть файл

@ -1,5 +1,5 @@
import { closeWindow } from './window-helpers';
import { TopLevelWindow, View } from 'electron';
import { TopLevelWindow, View } from 'electron/main';
describe('View', () => {
let w: TopLevelWindow;

Просмотреть файл

@ -4,7 +4,8 @@ import * as path from 'path';
import * as fs from 'fs';
import * as http from 'http';
import * as ChildProcess from 'child_process';
import { BrowserWindow, ipcMain, webContents, session, WebContents, app, clipboard } from 'electron';
import { BrowserWindow, ipcMain, webContents, session, WebContents, app } from 'electron/main';
import { clipboard } from 'electron/common';
import { emittedOnce } from './events-helpers';
import { closeAllWindows } from './window-helpers';
import { ifdescribe, ifit } from './spec-helpers';

Просмотреть файл

@ -4,7 +4,7 @@ import * as path from 'path';
import { emittedOnce } from './events-helpers';
import { closeWindow } from './window-helpers';
import { webContents, TopLevelWindow, WebContentsView } from 'electron';
import { webContents, TopLevelWindow, WebContentsView } from 'electron/main';
describe('WebContentsView', () => {
let w: TopLevelWindow;

Просмотреть файл

@ -1,6 +1,6 @@
import { expect } from 'chai';
import * as path from 'path';
import { BrowserWindow, ipcMain } from 'electron';
import { BrowserWindow, ipcMain } from 'electron/main';
import { closeAllWindows } from './window-helpers';
describe('webFrame module', () => {

Просмотреть файл

@ -4,7 +4,7 @@ import * as qs from 'querystring';
import * as path from 'path';
import * as url from 'url';
import * as WebSocket from 'ws';
import { ipcMain, protocol, session, WebContents, webContents } from 'electron';
import { ipcMain, protocol, session, WebContents, webContents } from 'electron/main';
import { AddressInfo } from 'net';
import { emittedOnce } from './events-helpers';

Просмотреть файл

@ -1,6 +1,6 @@
import { expect } from 'chai';
import * as path from 'path';
import { BrowserWindow, ipcMain } from 'electron';
import { BrowserWindow, ipcMain } from 'electron/main';
import { closeAllWindows } from './window-helpers';
describe('asar package', () => {

Просмотреть файл

@ -1,5 +1,5 @@
import { expect } from 'chai';
import { BrowserWindow, WebContents, session, ipcMain, app, protocol, webContents } from 'electron';
import { BrowserWindow, WebContents, session, ipcMain, app, protocol, webContents } from 'electron/main';
import { emittedOnce } from './events-helpers';
import { closeAllWindows } from './window-helpers';
import * as https from 'https';

Просмотреть файл

@ -1,5 +1,5 @@
import { expect } from 'chai';
import { app, session, BrowserWindow, ipcMain, WebContents, Extension } from 'electron';
import { app, session, BrowserWindow, ipcMain, WebContents, Extension } from 'electron/main';
import { closeAllWindows, closeWindow } from './window-helpers';
import * as http from 'http';
import { AddressInfo } from 'net';

Просмотреть файл

@ -1,7 +1,7 @@
import { expect } from 'chai';
import * as path from 'path';
import * as fs from 'fs';
import { BrowserWindow } from 'electron';
import { BrowserWindow } from 'electron/main';
import { ifdescribe, ifit } from './spec-helpers';
import { closeAllWindows } from './window-helpers';
import * as childProcess from 'child_process';

Просмотреть файл

@ -4,7 +4,7 @@ import * as path from 'path';
import * as util from 'util';
import { emittedOnce } from './events-helpers';
import { ifdescribe, ifit } from './spec-helpers';
import { webContents, WebContents } from 'electron';
import { webContents, WebContents } from 'electron/main';
const features = process.electronBinding('features');

Просмотреть файл

@ -4,7 +4,7 @@ import * as fs from 'fs';
import * as path from 'path';
import * as url from 'url';
import { BrowserWindow, WebPreferences } from 'electron';
import { BrowserWindow, WebPreferences } from 'electron/main';
import { closeWindow } from './window-helpers';
import { AddressInfo } from 'net';

Просмотреть файл

@ -1,4 +1,4 @@
import { BrowserWindow, Session, session } from 'electron';
import { BrowserWindow, Session, session } from 'electron/main';
import { expect } from 'chai';
import * as path from 'path';

Просмотреть файл

@ -1,6 +1,6 @@
import { expect } from 'chai';
import * as cp from 'child_process';
import { BrowserWindow, BrowserWindowConstructorOptions, ipcMain } from 'electron';
import { BrowserWindow, BrowserWindowConstructorOptions, ipcMain } from 'electron/main';
import * as path from 'path';
import { emittedOnce } from './events-helpers';

Просмотреть файл

@ -1,5 +1,5 @@
import * as path from 'path';
import { BrowserWindow, session, ipcMain, app, WebContents } from 'electron';
import { BrowserWindow, session, ipcMain, app, WebContents } from 'electron/main';
import { closeAllWindows } from './window-helpers';
import { emittedOnce } from './events-helpers';
import { expect } from 'chai';

Просмотреть файл

@ -1,5 +1,5 @@
import { expect } from 'chai';
import { BrowserWindow } from 'electron';
import { BrowserWindow } from 'electron/main';
import { emittedOnce } from './events-helpers';
async function ensureWindowIsClosed (window: BrowserWindow | null) {

4
typings/internal-electron.d.ts поставляемый
Просмотреть файл

@ -73,6 +73,10 @@ declare namespace Electron {
}
const deprecate: ElectronInternal.DeprecationUtil;
namespace Main {
const deprecate: ElectronInternal.DeprecationUtil;
}
}
declare namespace ElectronInternal {

Просмотреть файл

@ -25,21 +25,6 @@
dependencies:
regenerator-runtime "^0.13.2"
"@electron/docs-parser@^0.4.1":
version "0.4.4"
resolved "https://registry.yarnpkg.com/@electron/docs-parser/-/docs-parser-0.4.4.tgz#4b22be74aa9073315241d2d4b06b22e7ff487106"
integrity sha512-EGQMXBIkW6JtwaxbmuWrmJivmrR420dX1cEy5I5Az71aVIz/E4gxlLmlT5L/jOzWwJ+1rbpri9LKVWPl0Bnasw==
dependencies:
"@types/markdown-it" "^0.0.9"
chai "^4.2.0"
chalk "^3.0.0"
fs-extra "^8.1.0"
lodash.camelcase "^4.3.0"
markdown-it "^10.0.0"
minimist "^1.2.0"
ora "^4.0.3"
pretty-ms "^5.1.0"
"@electron/docs-parser@^0.7.2":
version "0.7.2"
resolved "https://registry.yarnpkg.com/@electron/docs-parser/-/docs-parser-0.7.2.tgz#f0d5b9f314db519ac1f83359c07c6ec42d3123d1"
@ -55,12 +40,11 @@
ora "^4.0.3"
pretty-ms "^5.1.0"
"@electron/typescript-definitions@^8.6.4":
version "8.6.4"
resolved "https://registry.yarnpkg.com/@electron/typescript-definitions/-/typescript-definitions-8.6.4.tgz#d4727c825afcebf6ff00a7d18416878288cac632"
integrity sha512-Fb4o56fikSE3id61U0MbPVBPcweVw8YwzO+JtT9aOorJR4baJZGmpsGxuaHeLSGv+D0Zd5XiWk13cEM/nv9fFw==
"@electron/typescript-definitions@^8.7.2":
version "8.7.2"
resolved "https://registry.yarnpkg.com/@electron/typescript-definitions/-/typescript-definitions-8.7.2.tgz#a653859975c6a19721b3f47f4f7953876498a8ed"
integrity sha512-AE1NjiP2nZsUGYMU78+W6QPCZCRKQe54Tee6qJnBW42rZktZ0hwYXglfpIavpo9dRp4pWncgCo56PXj2rKwCSA==
dependencies:
"@electron/docs-parser" "^0.4.1"
"@types/node" "^11.13.7"
chalk "^2.4.2"
colors "^1.1.2"