From a888e6affaf4285062a9cd5e8257358f6de9f285 Mon Sep 17 00:00:00 2001 From: Andrey Goncharov Date: Thu, 15 Sep 2022 10:02:19 -0700 Subject: [PATCH] Simplify bundled plugin setup Summary: Stop bundling plugins into Flipper Server bundles. In later diffs, we will start building all plugins even in dev mode which removes the need to bundle them. Reviewed By: lblasa Differential Revision: D39276249 fbshipit-source-id: 091405cfcf58aa7e1bd2b382da40f8d9841ae6b1 --- desktop/flipper-common/src/server-types.tsx | 2 -- desktop/flipper-frontend-core/src/plugins.tsx | 25 +++----------- .../src/HeadlessPluginInitializer.tsx | 6 ++-- .../src/initializeRenderHost.tsx | 7 +--- .../src/FlipperServerImpl.tsx | 1 - .../src/plugins/PluginManager.tsx | 23 ------------- .../src/plugins/loadDynamicPlugins.tsx | 22 ++---------- .../src/plugins/loadServerAddOn.tsx | 20 +---------- .../src/dispatcher/plugins.tsx | 2 +- .../scripts/build-flipper-server-release.tsx | 2 +- desktop/scripts/build-release.tsx | 4 +++ desktop/scripts/build-utils.tsx | 34 ++----------------- desktop/scripts/start-dev-server.tsx | 2 ++ desktop/scripts/start-flipper-server-dev.tsx | 3 +- 14 files changed, 24 insertions(+), 129 deletions(-) diff --git a/desktop/flipper-common/src/server-types.tsx b/desktop/flipper-common/src/server-types.tsx index 04c63e689..94bff7361 100644 --- a/desktop/flipper-common/src/server-types.tsx +++ b/desktop/flipper-common/src/server-types.tsx @@ -9,7 +9,6 @@ import {FlipperDoctor} from './doctor'; import { - BundledPluginDetails, DeviceSpec, DeviceType, DownloadablePluginDetails, @@ -254,7 +253,6 @@ export type FlipperServerCommands = { 'keychain-unset': (service: string) => Promise; 'plugins-load-dynamic-plugins': () => Promise; 'plugins-load-marketplace-plugins': () => Promise; - 'plugins-get-bundled-plugins': () => Promise; 'plugins-get-installed-plugins': () => Promise; 'plugins-get-updatable-plugins': ( query: string | undefined, diff --git a/desktop/flipper-frontend-core/src/plugins.tsx b/desktop/flipper-frontend-core/src/plugins.tsx index dd9ba8fef..feec2aaf5 100644 --- a/desktop/flipper-frontend-core/src/plugins.tsx +++ b/desktop/flipper-frontend-core/src/plugins.tsx @@ -75,12 +75,13 @@ export abstract class AbstractPluginInitializer { protected async loadAllLocalVersions( uninstalledPluginNames: Set, ): Promise<(BundledPluginDetails | InstalledPluginDetails)[]> { - const bundledPlugins = await getBundledPlugins(); - this.bundledPlugins = bundledPlugins; + this.bundledPlugins = Object.values(this.defaultPluginsIndex).map( + (defaultPluginEntry: any) => defaultPluginEntry.description, + ); const allLocalVersions = [ - ...bundledPlugins, ...(await getDynamicPlugins()), + ...this.bundledPlugins, ].filter((p) => !uninstalledPluginNames.has(p.name)); return allLocalVersions; @@ -147,24 +148,6 @@ export function getLatestCompatibleVersionOfEachPlugin< return Array.from(latestCompatibleVersions.values()); } -export async function getBundledPlugins(): Promise< - Array -> { - if (getRenderHostInstance().serverConfig.env.NODE_ENV === 'test') { - return []; - } - try { - // defaultPlugins that are included in the Flipper distributive. - // List of default bundled plugins is written at build time to defaultPlugins/bundled.json. - return await getRenderHostInstance().flipperServer!.exec( - 'plugins-get-bundled-plugins', - ); - } catch (e) { - console.error('Failed to load list of bundled plugins', e); - return []; - } -} - export async function getDynamicPlugins(): Promise { try { return await getRenderHostInstance().flipperServer!.exec( diff --git a/desktop/flipper-server-companion/src/HeadlessPluginInitializer.tsx b/desktop/flipper-server-companion/src/HeadlessPluginInitializer.tsx index 205eda55e..fc8bbcb8d 100644 --- a/desktop/flipper-server-companion/src/HeadlessPluginInitializer.tsx +++ b/desktop/flipper-server-companion/src/HeadlessPluginInitializer.tsx @@ -30,9 +30,9 @@ export class HeadlessPluginInitializer extends AbstractPluginInitializer { protected async requirePluginImpl( pluginDetails: ActivatablePluginDetails, ): Promise<_SandyPluginDefinition> { - const plugin = pluginDetails.isBundled - ? this.defaultPluginsIndex[pluginDetails.name] - : await getRenderHostInstance().requirePlugin(pluginDetails.entry); + const plugin = await getRenderHostInstance().requirePlugin( + (pluginDetails as InstalledPluginDetails).entry, + ); if (!plugin) { throw new Error( `Failed to obtain plugin source for: ${pluginDetails.name}`, diff --git a/desktop/flipper-server-companion/src/initializeRenderHost.tsx b/desktop/flipper-server-companion/src/initializeRenderHost.tsx index f9b4b08f6..d3078dbf5 100644 --- a/desktop/flipper-server-companion/src/initializeRenderHost.tsx +++ b/desktop/flipper-server-companion/src/initializeRenderHost.tsx @@ -46,7 +46,7 @@ export function initializeRenderHost( restartFlipper() { // TODO: }, - loadDefaultPlugins: getDefaultPluginsIndex, + loadDefaultPlugins: () => ({}), serverConfig: flipperServerConfig, GK(gatekeeper) { return flipperServerConfig.gatekeepers[gatekeeper] ?? false; @@ -81,8 +81,3 @@ export function initializeRenderHost( }, } as RenderHost; } - -function getDefaultPluginsIndex() { - // TODO: Fix me - return {}; -} diff --git a/desktop/flipper-server-core/src/FlipperServerImpl.tsx b/desktop/flipper-server-core/src/FlipperServerImpl.tsx index 63acd6422..e7014cc72 100644 --- a/desktop/flipper-server-core/src/FlipperServerImpl.tsx +++ b/desktop/flipper-server-core/src/FlipperServerImpl.tsx @@ -435,7 +435,6 @@ export class FlipperServerImpl implements FlipperServer { this.pluginManager.loadDynamicPlugins(), 'plugins-load-marketplace-plugins': () => this.pluginManager.loadMarketplacePlugins(), - 'plugins-get-bundled-plugins': () => this.pluginManager.getBundledPlugins(), 'plugins-get-installed-plugins': () => this.pluginManager.getInstalledPlugins(), 'plugins-remove-plugins': (plugins) => diff --git a/desktop/flipper-server-core/src/plugins/PluginManager.tsx b/desktop/flipper-server-core/src/plugins/PluginManager.tsx index 178b6d326..06a20d2a8 100644 --- a/desktop/flipper-server-core/src/plugins/PluginManager.tsx +++ b/desktop/flipper-server-core/src/plugins/PluginManager.tsx @@ -13,7 +13,6 @@ import tmp from 'tmp'; import {promisify} from 'util'; import {default as axios} from 'axios'; import { - BundledPluginDetails, DownloadablePluginDetails, ExecuteMessage, FlipperServerForServerAddOn, @@ -77,28 +76,6 @@ export class PluginManager { return await fs.readFile(path, 'utf8'); } - async getBundledPlugins(): Promise> { - if ( - process.env.NODE_ENV === 'test' || - process.env.FLIPPER_NO_BUNDLED_PLUGINS === 'true' - ) { - return []; - } - // defaultPlugins that are included in the Flipper distributive. - // List of default bundled plugins is written at build time to defaultPlugins/bundled.json. - const pluginPath = getStaticPath( - path.join('defaultPlugins', 'bundled.json'), - {asarUnpacked: true}, - ); - let bundledPlugins: Array = []; - try { - bundledPlugins = await fs.readJson(pluginPath); - } catch (e) { - console.error('Failed to load list of bundled plugins', e); - } - return bundledPlugins; - } - async loadMarketplacePlugins() { console.info('Load available plugins from marketplace'); return loadMarketplacePlugins(this.flipperServer, ''); diff --git a/desktop/flipper-server-core/src/plugins/loadDynamicPlugins.tsx b/desktop/flipper-server-core/src/plugins/loadDynamicPlugins.tsx index 11f1c935d..7fb23befe 100644 --- a/desktop/flipper-server-core/src/plugins/loadDynamicPlugins.tsx +++ b/desktop/flipper-server-core/src/plugins/loadDynamicPlugins.tsx @@ -19,7 +19,6 @@ import {InstalledPluginDetails} from 'flipper-common'; import {getStaticPath} from '../utils/pathUtils'; // Load "dynamic" plugins, e.g. those which are either pre-installed (default), installed or loaded from sources (for development). -// This opposed to "bundled" plugins which are included into Flipper bundle. export async function loadDynamicPlugins(): Promise { if (process.env.NODE_ENV === 'test') { return []; @@ -36,29 +35,14 @@ export async function loadDynamicPlugins(): Promise { ex, ), ); - const bundledPlugins = new Set( - ( - await fs.readJson( - getStaticPath(path.join('defaultPlugins', 'bundled.json'), { - asarUnpacked: true, - }), - ) - ).map((p: any) => p.name) as string[], - ); - console.log( - `✅ Detected ${bundledPlugins.size} bundled plugins: ${Array.from( - bundledPlugins, - ).join(', ')}.`, - ); - const [installedPlugins, unfilteredSourcePlugins] = await Promise.all([ + + const [installedPlugins, sourcePlugins] = await Promise.all([ process.env.FLIPPER_NO_PLUGIN_MARKETPLACE ? Promise.resolve([]) : getAllInstalledPluginVersions(), getSourcePlugins(), ]); - const sourcePlugins = unfilteredSourcePlugins.filter( - (p) => !bundledPlugins.has(p.name), - ); + const defaultPluginsDir = getStaticPath('defaultPlugins', { asarUnpacked: true, }); diff --git a/desktop/flipper-server-core/src/plugins/loadServerAddOn.tsx b/desktop/flipper-server-core/src/plugins/loadServerAddOn.tsx index 3ef657ae5..2ded150a9 100644 --- a/desktop/flipper-server-core/src/plugins/loadServerAddOn.tsx +++ b/desktop/flipper-server-core/src/plugins/loadServerAddOn.tsx @@ -20,15 +20,6 @@ declare global { } global.FlipperPlugin = FlipperPluginSDK; -// defaultPlugins has to be required after we set FlipperPlugin. -// In server add-ons, developers might import utilities from 'flipper-plugin' -// In babel-transformer/plugin-flipper-requires flipper-plugin is replaces with global.FlipperPlugin. -// If defaultPlugins is required before we set global.FlipperPlugin, -// then flipper-plugin replaced with global.FlipperPlugin is evaluated in server add-ons before we set it - to undefined. -// -// The file is generated automatically by "prepareDefaultPlugins" in "scripts" -const defaultPlugins = require('../defaultPlugins').default; - interface ServerAddOnModule { default: ServerAddOnFn; } @@ -39,18 +30,9 @@ export const loadServerAddOn = ( ): ServerAddOnModule => { console.debug('loadPlugin', pluginName, details); - if (details.isBundled) { - const bundledPlugin = defaultPlugins[pluginName]; - assertNotNull( - bundledPlugin, - `loadPlugin (isBundled = true) -> plugin ${pluginName} not found.`, - ); - return bundledPlugin; - } - assertNotNull( details.path, - `loadPlugin (isBundled = false) -> server add-on path is empty plugin ${pluginName}.`, + `loadPlugin -> server add-on path is empty plugin ${pluginName}.`, ); // eslint-disable-next-line no-eval diff --git a/desktop/flipper-ui-core/src/dispatcher/plugins.tsx b/desktop/flipper-ui-core/src/dispatcher/plugins.tsx index ffda1f1c5..7366e1e5f 100644 --- a/desktop/flipper-ui-core/src/dispatcher/plugins.tsx +++ b/desktop/flipper-ui-core/src/dispatcher/plugins.tsx @@ -127,7 +127,7 @@ export const requirePluginInternal = async ( pluginDetails: ActivatablePluginDetails, ): Promise => { let plugin = pluginDetails.isBundled - ? defaultPluginsIndex[pluginDetails.name] + ? defaultPluginsIndex[pluginDetails.name].source : await getRenderHostInstance().requirePlugin(pluginDetails.entry); if (!plugin) { throw new Error( diff --git a/desktop/scripts/build-flipper-server-release.tsx b/desktop/scripts/build-flipper-server-release.tsx index ea16733db..8c8f1363c 100644 --- a/desktop/scripts/build-flipper-server-release.tsx +++ b/desktop/scripts/build-flipper-server-release.tsx @@ -353,7 +353,7 @@ async function buildServerRelease() { // create plugin output dir await fs.mkdirp(path.join(dir, 'static', 'defaultPlugins')); - await prepareDefaultPlugins(argv.channel === 'insiders', true); + await prepareDefaultPlugins(argv.channel === 'insiders'); await buildServerAddOns(false); await buildHeadlessPlugins(false); await compileServerMain(false); diff --git a/desktop/scripts/build-release.tsx b/desktop/scripts/build-release.tsx index 54dbdabf5..6fe74abe5 100755 --- a/desktop/scripts/build-release.tsx +++ b/desktop/scripts/build-release.tsx @@ -27,6 +27,8 @@ import { genMercurialRevision, prepareDefaultPlugins, moveSourceMaps, + buildServerAddOns, + buildHeadlessPlugins, } from './build-utils'; import isFB from './isFB'; import copyPackageWithDependencies from './copy-package-with-dependencies'; @@ -311,6 +313,8 @@ async function copyStaticFolder(buildFolder: string) { await compileMain(); await prepareDefaultPlugins(argv.channel === 'insiders'); + await buildServerAddOns(false); + await buildHeadlessPlugins(false); await copyStaticFolder(dir); await downloadIcons(dir); await compileRenderer(dir); diff --git a/desktop/scripts/build-utils.tsx b/desktop/scripts/build-utils.tsx index 5603e6201..6a127dd9f 100644 --- a/desktop/scripts/build-utils.tsx +++ b/desktop/scripts/build-utils.tsx @@ -36,7 +36,6 @@ import { serverDir, rootDir, browserUiDir, - serverCoreDir, } from './paths'; import pFilter from 'p-filter'; import child from 'child_process'; @@ -44,7 +43,6 @@ import pMap from 'p-map'; // eslint-disable-next-line flipper/no-relative-imports-across-packages const {version} = require('../package.json'); - const dev = process.env.NODE_ENV !== 'production'; // For insiders builds we bundle top 5 popular device plugins, @@ -75,10 +73,7 @@ export function die(err: Error) { process.exit(1); } -export async function prepareDefaultPlugins( - isInsidersBuild: boolean = false, - flipperServerBuild = false, -) { +export async function prepareDefaultPlugins(isInsidersBuild: boolean = false) { console.log( `⚙️ Preparing default plugins (isInsidersBuild=${isInsidersBuild})...`, ); @@ -107,10 +102,7 @@ export async function prepareDefaultPlugins( await buildDefaultPlugins(defaultPlugins); await generateDefaultPluginEntryPoints([]); // calling it here just to generate empty indexes } else { - await generateDefaultPluginEntryPoints( - defaultPlugins, - flipperServerBuild, - ); + await generateDefaultPluginEntryPoints(defaultPlugins); } } console.log('✅ Prepared default plugins.'); @@ -155,7 +147,6 @@ function getGeneratedIndex(pluginRequires: string) { async function generateDefaultPluginEntryPoints( defaultPlugins: InstalledPluginDetails[], - flipperServerBuild?: boolean, ) { console.log( `⚙️ Generating entry points for ${defaultPlugins.length} bundled plugins...`, @@ -173,10 +164,7 @@ async function generateDefaultPluginEntryPoints( serverAddOnEntry: undefined, } as BundledPluginDetails), ); - await fs.writeJSON( - path.join(defaultPluginsDir, 'bundled.json'), - bundledPlugins, - ); + const pluginRequires = bundledPlugins .map( (x) => @@ -195,22 +183,6 @@ async function generateDefaultPluginEntryPoints( generatedIndex, ); - const serverAddOns = flipperServerBuild - ? [] - : defaultPlugins.filter(({serverAddOnSource}) => !!serverAddOnSource); - const serverAddOnRequires = serverAddOns - .map( - (x) => - ` '${x.name}': tryRequire('${x.name}', () => require('${x.name}/${x.serverAddOnSource}'))`, - ) - .join(',\n'); - const generatedIndexServerAddOns = getGeneratedIndex(serverAddOnRequires); - await fs.ensureDir(path.join(serverCoreDir, 'src', 'defaultPlugins')); - await fs.writeFile( - path.join(serverCoreDir, 'src', 'defaultPlugins', 'index.tsx'), - generatedIndexServerAddOns, - ); - console.log('✅ Generated bundled plugin entry points.'); } diff --git a/desktop/scripts/start-dev-server.tsx b/desktop/scripts/start-dev-server.tsx index ae47003a0..762a013f0 100644 --- a/desktop/scripts/start-dev-server.tsx +++ b/desktop/scripts/start-dev-server.tsx @@ -24,6 +24,7 @@ import { compileMain, prepareDefaultPlugins, buildHeadlessPlugins, + buildServerAddOns, } from './build-utils'; import Watchman from './watchman'; // @ts-ignore no typings for metro @@ -445,6 +446,7 @@ function checkDevServer() { await prepareDefaultPlugins( process.env.FLIPPER_RELEASE_CHANNEL === 'insiders', ); + await buildServerAddOns(true); await buildHeadlessPlugins(true); await ensurePluginFoldersWatchable(); const port = await detect(DEFAULT_PORT); diff --git a/desktop/scripts/start-flipper-server-dev.tsx b/desktop/scripts/start-flipper-server-dev.tsx index 0784d723c..6c96d7a73 100644 --- a/desktop/scripts/start-flipper-server-dev.tsx +++ b/desktop/scripts/start-flipper-server-dev.tsx @@ -186,10 +186,9 @@ async function startWatchChanges() { } await prepareDefaultPlugins( process.env.FLIPPER_RELEASE_CHANNEL === 'insiders', - true, ); - await buildHeadlessPlugins(true); await buildServerAddOns(true); + await buildHeadlessPlugins(true); await ensurePluginFoldersWatchable(); // builds and starts