Fix require monkey-patching in electron build

Summary:
In D39311893 (094c5bdfdd) we started monkey-patching `require` to resolve global dependencies in the plugins. Apparently, patching `globalThis.require` did not work in the electron env. On my local machine it kept working because I had the experimental `flipper-server` feature enabled which embeds flipper-server into the electron build. In flipper-server we properly patch `require` via `Module.prototype.require` which affected the global require in electron.
With this fix we now properly patch require in electron via Module.prototype.require all the time

Changelog: Fix plugin loading with experimental flipper-server disabled

Reviewed By: nikoant

Differential Revision: D39633821

fbshipit-source-id: 9554f643c625620d116075ae87f573d8447850f6
This commit is contained in:
Andrey Goncharov
2022-09-20 01:36:56 -07:00
committed by Facebook GitHub Bot
parent d600203627
commit 8716761cb3
6 changed files with 32 additions and 21 deletions

View File

@@ -7,9 +7,29 @@
* @format
*/
import {FlipperServer, FlipperServerConfig, isProduction} from 'flipper-common';
import {
FlipperServer,
FlipperServerConfig,
isProduction,
wrapRequire,
} from 'flipper-common';
import type {RenderHost} from 'flipper-ui-core';
declare module globalThis {
let require: any;
}
// Whenever we bundle plugins, we assume that they are going to share some modules - React, React-DOM, ant design and etc.
// It allows us to decrease the bundle size and not to create separate React roots for every plugin
// To tell a plugin that a module is going to be provided externally, we add the module to the list of externals (see https://esbuild.github.io/api/#external).
// As a result, esbuild does not bundle hte contents of the module. Instead, it wraps the module name with `require(...)`.
// `require` does not exist ion the browser environment, so we substitute it here to feed the plugin our global module.
globalThis.require = wrapRequire((module: string) => {
throw new Error(
`Dynamic require is not supported in browser envs. Tried to require: ${module}`,
);
});
export function initializeRenderHost(
flipperServer: FlipperServer,
flipperServerConfig: FlipperServerConfig,