Intergrate ServerAddOn with PluginManager

Reviewed By: lblasa

Differential Revision: D34042338

fbshipit-source-id: 2ae3992738e647075f19c4dba0ebfb01e6c12b8f
This commit is contained in:
Andrey Goncharov
2022-02-28 03:50:34 -08:00
committed by Facebook GitHub Bot
parent 016299b374
commit a60865f0be
3 changed files with 41 additions and 4 deletions

View File

@@ -15,6 +15,13 @@ interface ServerAddOnModule {
serverAddOn?: () => Promise<ServerAddOnCleanup>;
}
// TODO: Metro does not like dynamic requires. Figure out how to properly configure metro to handle them.
// https://github.com/webpack/webpack/issues/4175#issuecomment-323023911
function requireDynamically(path: string) {
// eslint-disable-next-line no-eval
return eval(`require('${path}');`); // Ensure Metro does not analyze the require statement
}
// TODO: Fix potential race conditions when starting/stopping concurrently
export class ServerAddOn {
private owners: Set<string>;
@@ -30,7 +37,7 @@ export class ServerAddOn {
static async start(path: string, initialOwner: string): Promise<ServerAddOn> {
console.info('ServerAddOn.start', path);
const {serverAddOn} = require(path) as ServerAddOnModule;
const {serverAddOn} = requireDynamically(path) as ServerAddOnModule;
assertNotNull(serverAddOn);
assert(
typeof serverAddOn === 'function',
@@ -66,6 +73,10 @@ export class ServerAddOn {
private async stop() {
console.info('ServerAddOn.stop', this.path);
await this.cleanup();
try {
await this.cleanup();
} catch (e) {
console.error('ServerAddOn.stop -> failed to clean up', this.path);
}
}
}