Clean up globals

Summary: This diff is some boyscouting on cleaning up our globals, and stop using them inconsistently icmw global or window

Reviewed By: aigoncharov

Differential Revision: D33171108

fbshipit-source-id: 400893e5f31523631a7ab6fda428524c751901f1
This commit is contained in:
Michel Weststrate
2021-12-17 10:02:57 -08:00
committed by Facebook GitHub Bot
parent c42c54f69d
commit cea7be7fde
13 changed files with 13 additions and 47 deletions

View File

@@ -29,12 +29,6 @@ import type {Icon, RenderHost} from 'flipper-ui-core';
import {getLocalIconUrl} from '../utils/icons'; import {getLocalIconUrl} from '../utils/icons';
import {getCPUUsage} from 'process'; import {getCPUUsage} from 'process';
declare const electronRequire: {
(name: string): any;
resolve: (module: string) => string;
cache: {[module: string]: any};
};
export function initializeElectron( export function initializeElectron(
flipperServer: FlipperServer, flipperServer: FlipperServer,
flipperServerConfig: FlipperServerConfig, flipperServerConfig: FlipperServerConfig,
@@ -64,7 +58,7 @@ export function initializeElectron(
} }
} }
window.FlipperRenderHostInstance = { FlipperRenderHostInstance = {
processId: remote.process.pid, processId: remote.process.pid,
isProduction, isProduction,
readTextFromClipboard() { readTextFromClipboard() {

View File

@@ -42,20 +42,6 @@ import fs from 'fs';
enableMapSet(); enableMapSet();
declare global {
interface Window {
// We store this as a global, to make sure the renderHost is available
// before flipper-ui-core is loaded and needs those during module initialisation
FlipperRenderHostInstance: RenderHost;
}
}
declare const electronRequire: {
(name: string): any;
resolve: (module: string) => string;
cache: {[module: string]: any};
};
if (process.env.NODE_ENV === 'development' && os.platform() === 'darwin') { if (process.env.NODE_ENV === 'development' && os.platform() === 'darwin') {
// By default Node.JS has its internal certificate storage and doesn't use // By default Node.JS has its internal certificate storage and doesn't use
// the system store. Because of this, it's impossible to access ondemand / devserver // the system store. Because of this, it's impossible to access ondemand / devserver

View File

@@ -58,7 +58,7 @@ type ClientCsrQuery = {
csr_path?: string | undefined; csr_path?: string | undefined;
}; };
declare interface ServerController { interface ServerController {
on(event: 'error', callback: (err: Error) => void): this; on(event: 'error', callback: (err: Error) => void): this;
} }

View File

@@ -70,7 +70,7 @@ export async function startWebServerDev(
'electron-requires.js', 'electron-requires.js',
); );
const stubModules = new Set<string>( const stubModules = new Set<string>(
(global as any).electronRequire(electronRequires).BUILTINS, electronRequire(electronRequires).BUILTINS,
); );
if (!stubModules.size) { if (!stubModules.size) {
throw new Error('Failed to load list of Node builtins'); throw new Error('Failed to load list of Node builtins');

View File

@@ -17,8 +17,6 @@ declare global {
debug: boolean; debug: boolean;
}; };
FlipperRenderHostInstance: RenderHost;
flipperShowError?(error: string): void; flipperShowError?(error: string): void;
flipperHideError?(): void; flipperHideError?(): void;
} }

View File

@@ -15,7 +15,7 @@ document.getElementById('root')!.innerText = 'flipper-ui-browser started';
async function start() { async function start() {
// @ts-ignore // @ts-ignore
(global as any).electronRequire = function (path: string) { electronRequire = function (path: string) {
console.error( console.error(
`[decapitate] Tried to electronRequire ${path}, this module is not available in the browser and will be stubbed`, `[decapitate] Tried to electronRequire ${path}, this module is not available in the browser and will be stubbed`,
); );

View File

@@ -14,7 +14,7 @@ export function initializeRenderHost(
flipperServer: FlipperServer, flipperServer: FlipperServer,
flipperServerConfig: FlipperServerConfig, flipperServerConfig: FlipperServerConfig,
) { ) {
window.FlipperRenderHostInstance = { FlipperRenderHostInstance = {
readTextFromClipboard() { readTextFromClipboard() {
// TODO: // TODO:
return undefined; return undefined;

View File

@@ -3,7 +3,8 @@
"compilerOptions": { "compilerOptions": {
"outDir": "lib", "outDir": "lib",
"rootDir": "src", "rootDir": "src",
"lib": ["dom", "ES2019"] "lib": ["dom", "ES2019"],
"types": ["../types/flipperGlobals"]
}, },
"references": [ "references": [
{ {

View File

@@ -141,8 +141,8 @@ export interface RenderHost {
} }
export function getRenderHostInstance(): RenderHost { export function getRenderHostInstance(): RenderHost {
if (!window.FlipperRenderHostInstance) { if (!FlipperRenderHostInstance) {
throw new Error('global FlipperRenderHostInstance was never set'); throw new Error('global FlipperRenderHostInstance was never set');
} }
return window.FlipperRenderHostInstance; return FlipperRenderHostInstance;
} }

View File

@@ -19,7 +19,5 @@ declare global {
__REDUX_DEVTOOLS_EXTENSION__: __REDUX_DEVTOOLS_EXTENSION__:
| undefined | undefined
| (StoreEnhancerStoreCreator & StoreEnhancerStateSanitizer); | (StoreEnhancerStoreCreator & StoreEnhancerStateSanitizer);
FlipperRenderHostInstance: RenderHost;
} }
} }

View File

@@ -6,7 +6,7 @@
"esModuleInterop": true, "esModuleInterop": true,
"emitDeclarationOnly": true, "emitDeclarationOnly": true,
"lib": ["dom", "es2019"], "lib": ["dom", "es2019"],
"types": ["../types/ReactDebounceRender"] "types": ["../types/ReactDebounceRender", "../types/flipperGlobals"]
}, },
"references": [ "references": [
{ {

View File

@@ -125,14 +125,14 @@ async function generateDefaultPluginEntryPoints(
const pluginRequres = bundledPlugins const pluginRequres = bundledPlugins
.map( .map(
(x) => (x) =>
` '${x.name}': tryRequire('${x.name}', () => require('${x.name}'))`, ` '${x.name}': tryRequire('${x.name}', () => require('${x.name}'))`,
) )
.join(',\n'); .join(',\n');
const generatedIndex = ` const generatedIndex = `
/* eslint-disable */ /* eslint-disable */
// THIS FILE IS AUTO-GENERATED by function "generateDefaultPluginEntryPoints" in "build-utils.ts". // THIS FILE IS AUTO-GENERATED by function "generateDefaultPluginEntryPoints" in "build-utils.ts".
declare var require: any; declare const require: any;
// This function exists to make sure that if one require fails in its module initialisation, not everything fails // This function exists to make sure that if one require fails in its module initialisation, not everything fails
function tryRequire(module: string, fn: () => any): any { function tryRequire(module: string, fn: () => any): any {

View File

@@ -15,15 +15,4 @@ declare const electronRequire: {
cache: {[module: string]: any}; cache: {[module: string]: any};
}; };
// For Electron declare let FlipperRenderHostInstance: any /* RenderHost */;
declare module NodeJS {
interface Global {
__REVISION__: string | undefined;
__VERSION__: string;
electronRequire: {
(name: string): any;
resolve: (module: string) => string;
cache: {[module: string]: any};
};
}
}