Provide external modules to plugins

Summary: esbuild references external modules via `require`. We wrap `require` to point the references to built-in modules to global variables

Reviewed By: lblasa

Differential Revision: D39311893

fbshipit-source-id: a99480161c082f4095d78c22271f114532f32c16
This commit is contained in:
Andrey Goncharov
2022-09-15 10:02:19 -07:00
committed by Facebook GitHub Bot
parent 650ff4bcfb
commit 094c5bdfdd
5 changed files with 62 additions and 2 deletions

View File

@@ -23,6 +23,7 @@ export {
export * from './server-types';
export * from './companion-types';
export * from './ServerAddOn';
export * from './plugin-external-modules';
export {sleep} from './utils/sleep';
export {timeout} from './utils/timeout';
export {isTest} from './utils/isTest';

View File

@@ -0,0 +1,38 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
// This list should match `dispatcher/plugins.tsx` and `builtInModules` in `desktop/.eslintrc.js`
export const pluginExternalModules = {
flipper: 'Flipper',
'flipper-plugin': 'FlipperPlugin',
react: 'React',
'react-dom': 'ReactDOM',
'react-dom/client': 'ReactDOMClient',
'react-is': 'ReactIs',
antd: 'antd',
immer: 'Immer',
'@emotion/styled': 'emotion_styled',
'@ant-design/icons': 'antdesign_icons',
};
export const wrapRequire = <T extends (path: string) => any>(require: T): T =>
new Proxy(require, {
apply(target, thisArg, argumentsList) {
const moduleName = argumentsList[0];
const replacementName = (
pluginExternalModules as Record<string, string | undefined>
)[moduleName];
if (replacementName && replacementName in globalThis) {
return (globalThis as any)[replacementName];
}
return Reflect.apply(target, thisArg, argumentsList);
},
});