refactor: Port renderer/init to TypeScript (#17027)
* refactor: Port renderer/init to TypeScript * Update lib/renderer/init.ts Co-Authored-By: felixrieseberg <felix@felixrieseberg.com> * refactor: Type this a bit more loosely * refactor: Type parseOption strictly
This commit is contained in:
parent
2223114f20
commit
91f81b4b72
4 changed files with 38 additions and 12 deletions
|
@ -65,7 +65,7 @@ filenames = {
|
||||||
"lib/renderer/callbacks-registry.js",
|
"lib/renderer/callbacks-registry.js",
|
||||||
"lib/renderer/chrome-api.ts",
|
"lib/renderer/chrome-api.ts",
|
||||||
"lib/renderer/content-scripts-injector.ts",
|
"lib/renderer/content-scripts-injector.ts",
|
||||||
"lib/renderer/init.js",
|
"lib/renderer/init.ts",
|
||||||
"lib/renderer/inspector.js",
|
"lib/renderer/inspector.js",
|
||||||
"lib/renderer/ipc-renderer-internal-utils.ts",
|
"lib/renderer/ipc-renderer-internal-utils.ts",
|
||||||
"lib/renderer/ipc-renderer-internal.ts",
|
"lib/renderer/ipc-renderer-internal.ts",
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
'use strict'
|
import { EventEmitter } from 'events'
|
||||||
|
import * as path from 'path'
|
||||||
|
|
||||||
const { EventEmitter } = require('events')
|
|
||||||
const path = require('path')
|
|
||||||
const Module = require('module')
|
const Module = require('module')
|
||||||
|
|
||||||
// We modified the original process.argv to let node.js load the
|
// We modified the original process.argv to let node.js load the
|
||||||
|
@ -34,8 +33,16 @@ webFrameInit()
|
||||||
// Process command line arguments.
|
// Process command line arguments.
|
||||||
const { hasSwitch, getSwitchValue } = process.atomBinding('command_line')
|
const { hasSwitch, getSwitchValue } = process.atomBinding('command_line')
|
||||||
|
|
||||||
const parseOption = function (name, defaultValue, converter = value => value) {
|
const parseOption = function<T> (
|
||||||
return hasSwitch(name) ? converter(getSwitchValue(name)) : defaultValue
|
name: string, defaultValue: T, converter?: (value: string) => T
|
||||||
|
) {
|
||||||
|
return hasSwitch(name)
|
||||||
|
? (
|
||||||
|
converter
|
||||||
|
? converter(getSwitchValue(name))
|
||||||
|
: getSwitchValue(name)
|
||||||
|
)
|
||||||
|
: defaultValue
|
||||||
}
|
}
|
||||||
|
|
||||||
const contextIsolation = hasSwitch('context-isolation')
|
const contextIsolation = hasSwitch('context-isolation')
|
||||||
|
@ -46,7 +53,7 @@ const isBackgroundPage = hasSwitch('background-page')
|
||||||
const usesNativeWindowOpen = hasSwitch('native-window-open')
|
const usesNativeWindowOpen = hasSwitch('native-window-open')
|
||||||
|
|
||||||
const preloadScript = parseOption('preload', null)
|
const preloadScript = parseOption('preload', null)
|
||||||
const preloadScripts = parseOption('preload-scripts', [], value => value.split(path.delimiter))
|
const preloadScripts = parseOption('preload-scripts', [], value => value.split(path.delimiter)) as string[]
|
||||||
const appPath = parseOption('app-path', null)
|
const appPath = parseOption('app-path', null)
|
||||||
const guestInstanceId = parseOption('guest-instance-id', null, value => parseInt(value))
|
const guestInstanceId = parseOption('guest-instance-id', null, value => parseInt(value))
|
||||||
const openerId = parseOption('opener-id', null, value => parseInt(value))
|
const openerId = parseOption('opener-id', null, value => parseInt(value))
|
||||||
|
@ -132,9 +139,12 @@ if (nodeIntegration) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Redirect window.onerror to uncaughtException.
|
// Redirect window.onerror to uncaughtException.
|
||||||
window.onerror = function (message, filename, lineno, colno, error) {
|
window.onerror = function (_message, _filename, _lineno, _colno, error) {
|
||||||
if (global.process.listeners('uncaughtException').length > 0) {
|
if (global.process.listeners('uncaughtException').length > 0) {
|
||||||
global.process.emit('uncaughtException', error)
|
// We do not want to add `uncaughtException` to our definitions
|
||||||
|
// because we don't want anyone else (anywhere) to throw that kind
|
||||||
|
// of error.
|
||||||
|
global.process.emit('uncaughtException' as any, error as any)
|
||||||
return true
|
return true
|
||||||
} else {
|
} else {
|
||||||
return false
|
return false
|
15
typings/internal-ambient.d.ts
vendored
15
typings/internal-ambient.d.ts
vendored
|
@ -36,7 +36,16 @@ declare namespace NodeJS {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
declare interface Window {
|
declare module NodeJS {
|
||||||
ELECTRON_DISABLE_SECURITY_WARNINGS?: boolean
|
interface Global {
|
||||||
ELECTRON_ENABLE_SECURITY_WARNINGS?: boolean
|
require: NodeRequire;
|
||||||
|
module: NodeModule;
|
||||||
|
__filename: string;
|
||||||
|
__dirname: string;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
declare interface Window {
|
||||||
|
ELECTRON_DISABLE_SECURITY_WARNINGS?: boolean;
|
||||||
|
ELECTRON_ENABLE_SECURITY_WARNINGS?: boolean;
|
||||||
}
|
}
|
||||||
|
|
7
typings/internal-electron.d.ts
vendored
7
typings/internal-electron.d.ts
vendored
|
@ -103,3 +103,10 @@ declare namespace Chrome {
|
||||||
type SendMessageCallback = (result: any) => void;
|
type SendMessageCallback = (result: any) => void;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface Global extends NodeJS.Global {
|
||||||
|
require: NodeRequire;
|
||||||
|
module: NodeModule;
|
||||||
|
__filename: string;
|
||||||
|
__dirname: string;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue