Remove default plugin entrypoints for hot-reloading

Summary: As we stopped bundling plugins in D39276249, we no longer need the entry points for the bundled plugins (these entry points are always going to be empty)

Reviewed By: lblasa

Differential Revision: D39307565

fbshipit-source-id: 43751fe31c8bd962677c226b27cfe52093d3f2d4
This commit is contained in:
Andrey Goncharov
2022-09-15 10:02:19 -07:00
committed by Facebook GitHub Bot
parent 94df830dfb
commit 642a3ebf81
16 changed files with 55 additions and 164 deletions

View File

@@ -13,8 +13,5 @@ react-native/ReactNativeFlipperExample
scripts/generate-changelog.js
static/index.js
static/defaultPlugins/*
app/src/defaultPlugins/index.tsx
flipper-server-core/src/defaultPlugins/index.tsx
flipper-server-companion/src/defaultPlugins/index.tsx
generated
flipper-server/static

4
desktop/.gitignore vendored
View File

@@ -5,10 +5,6 @@ node_modules/
/static/idb-applications
/static/themes/
/static/defaultPlugins/
/app/src/defaultPlugins/index.tsx
/flipper-ui-browser/src/defaultPlugins/index.tsx
/flipper-server-core/src/defaultPlugins/index.tsx
/flipper-server-companion/src/defaultPlugins/index.tsx
/coverage
.env
tsc-error.log

View File

@@ -1,10 +0,0 @@
/**
* 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
*/
export default {} as any;

View File

@@ -172,7 +172,6 @@ export async function initializeElectron(
restartFlipper(update: boolean = false) {
restart(update);
},
loadDefaultPlugins: getDefaultPluginsIndex,
serverConfig: flipperServerConfig,
GK(gatekeeper) {
return flipperServerConfig.gatekeepers[gatekeeper] ?? false;
@@ -207,9 +206,3 @@ export async function initializeElectron(
},
} as RenderHost;
}
function getDefaultPluginsIndex() {
// eslint-disable-next-line import/no-unresolved
const index = require('../defaultPlugins');
return index.default || index;
}

View File

@@ -138,7 +138,6 @@ export interface RenderHost {
shouldUseDarkColors(): boolean;
restartFlipper(update?: boolean): void;
openLink(url: string): void;
loadDefaultPlugins(): Record<string, any>;
GK(gatekeeper: string): boolean;
flipperServer: FlipperServer;
serverConfig: FlipperServerConfig;

View File

@@ -25,7 +25,6 @@ import {getRenderHostInstance} from './RenderHost';
import pMap from 'p-map';
export abstract class AbstractPluginInitializer {
protected defaultPluginsIndex: any = null;
protected gatekeepedPlugins: Array<ActivatablePluginDetails> = [];
protected disabledPlugins: Array<ActivatablePluginDetails> = [];
protected failedPlugins: Array<[ActivatablePluginDetails, string]> = [];
@@ -48,7 +47,6 @@ export abstract class AbstractPluginInitializer {
}
protected async _init(): Promise<_SandyPluginDefinition[]> {
this.loadDefaultPluginIndex();
this.loadMarketplacePlugins();
const uninstalledPluginNames = this.loadUninstalledPluginNames();
const allLocalVersions = await this.loadAllLocalVersions(
@@ -65,9 +63,6 @@ export abstract class AbstractPluginInitializer {
pluginDetails: ActivatablePluginDetails,
): Promise<_SandyPluginDefinition>;
protected loadDefaultPluginIndex() {
this.defaultPluginsIndex = getRenderHostInstance().loadDefaultPlugins();
}
protected loadMarketplacePlugins() {}
protected loadUninstalledPluginNames(): Set<string> {
return new Set();
@@ -75,15 +70,10 @@ export abstract class AbstractPluginInitializer {
protected async loadAllLocalVersions(
uninstalledPluginNames: Set<string>,
): Promise<(BundledPluginDetails | InstalledPluginDetails)[]> {
this.bundledPlugins = Object.values(this.defaultPluginsIndex).map(
(defaultPluginEntry: any) => defaultPluginEntry.description,
const allLocalVersions = [...(await getDynamicPlugins())].filter(
(p) => !uninstalledPluginNames.has(p.name),
);
const allLocalVersions = [
...(await getDynamicPlugins()),
...this.bundledPlugins,
].filter((p) => !uninstalledPluginNames.has(p.name));
return allLocalVersions;
}
protected async filterAllLocalVersions(

View File

@@ -46,7 +46,6 @@ export function initializeRenderHost(
restartFlipper() {
// TODO:
},
loadDefaultPlugins: () => ({}),
serverConfig: flipperServerConfig,
GK(gatekeeper) {
return flipperServerConfig.gatekeepers[gatekeeper] ?? false;

View File

@@ -1,10 +0,0 @@
/**
* 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
*/
export default {} as any;

View File

@@ -52,7 +52,6 @@ export function initializeRenderHost(
'Flipper settings have changed, please restart flipper server for the changes to take effect',
);
},
loadDefaultPlugins: getDefaultPluginsIndex,
serverConfig: flipperServerConfig,
GK(gatekeeper) {
return flipperServerConfig.gatekeepers[gatekeeper] ?? false;
@@ -87,10 +86,3 @@ export function initializeRenderHost(
},
} as RenderHost;
}
function getDefaultPluginsIndex() {
// @ts-ignore
// eslint-disable-next-line import/no-unresolved
const index = require('./defaultPlugins');
return index.default || index;
}

View File

@@ -42,9 +42,6 @@ const sampleInstalledPluginDetails: InstalledPluginDetails = {
isActivatable: true,
};
// bind to empty default plugin index so we try fetching from flipper-server every time
const requirePlugin = requirePluginInternal.bind({}, {});
beforeEach(() => {
loadDynamicPluginsMock = getRenderHostInstance().flipperServer.exec =
jest.fn();
@@ -58,7 +55,7 @@ test('dispatcher dispatches REGISTER_PLUGINS', async () => {
});
test('requirePluginInternal returns null for invalid requires', async () => {
const requireFn = createRequirePluginFunction(requirePlugin)([]);
const requireFn = createRequirePluginFunction(requirePluginInternal)([]);
const plugin = await requireFn({
...sampleInstalledPluginDetails,
name: 'pluginID',
@@ -72,7 +69,7 @@ test('requirePluginInternal returns null for invalid requires', async () => {
test('requirePluginInternal loads plugin', async () => {
const name = 'pluginID';
const requireFn = createRequirePluginFunction(requirePlugin)([]);
const requireFn = createRequirePluginFunction(requirePluginInternal)([]);
const plugin = await requireFn({
...sampleInstalledPluginDetails,
name,
@@ -94,7 +91,7 @@ test('requirePluginInternal loads plugin', async () => {
test('requirePluginInternal loads valid Sandy plugin', async () => {
const name = 'pluginID';
const requireFn = createRequirePluginFunction(requirePlugin)([]);
const requireFn = createRequirePluginFunction(requirePluginInternal)([]);
const plugin = (await requireFn({
...sampleInstalledPluginDetails,
name,
@@ -132,7 +129,9 @@ test('requirePluginInternal loads valid Sandy plugin', async () => {
test('requirePluginInternal errors on invalid Sandy plugin', async () => {
const name = 'pluginID';
const failedPlugins: any[] = [];
const requireFn = createRequirePluginFunction(requirePlugin)(failedPlugins);
const requireFn = createRequirePluginFunction(requirePluginInternal)(
failedPlugins,
);
await requireFn({
...sampleInstalledPluginDetails,
name,
@@ -149,7 +148,7 @@ test('requirePluginInternal errors on invalid Sandy plugin', async () => {
test('requirePluginInternal loads valid Sandy Device plugin', async () => {
const name = 'pluginID';
const requireFn = createRequirePluginFunction(requirePlugin)([]);
const requireFn = createRequirePluginFunction(requirePluginInternal)([]);
const plugin = (await requireFn({
...sampleInstalledPluginDetails,
pluginType: 'device',

View File

@@ -8,7 +8,11 @@
*/
import type {Store} from '../reducers/index';
import {Logger, MarketplacePluginDetails} from 'flipper-common';
import {
InstalledPluginDetails,
Logger,
MarketplacePluginDetails,
} from 'flipper-common';
import {PluginDefinition} from '../plugin';
import React from 'react';
import ReactDOM from 'react-dom';
@@ -83,7 +87,7 @@ class UIPluginInitializer extends AbstractPluginInitializer {
}
public requirePluginImpl(pluginDetails: ActivatablePluginDetails) {
return requirePluginInternal(this.defaultPluginsIndex, pluginDetails);
return requirePluginInternal(pluginDetails);
}
protected loadMarketplacePlugins() {
@@ -123,12 +127,11 @@ export const requirePlugin = (pluginDetails: ActivatablePluginDetails) =>
)(pluginDetails);
export const requirePluginInternal = async (
defaultPluginsIndex: any,
pluginDetails: ActivatablePluginDetails,
): Promise<PluginDefinition> => {
let plugin = pluginDetails.isBundled
? defaultPluginsIndex[pluginDetails.name].source
: await getRenderHostInstance().requirePlugin(pluginDetails.entry);
let plugin = await getRenderHostInstance().requirePlugin(
(pluginDetails as InstalledPluginDetails).entry,
);
if (!plugin) {
throw new Error(
`Failed to obtain plugin source for: ${pluginDetails.name}`,

View File

@@ -139,7 +139,7 @@
"lint:tsc": "tsc && tsc -p tsc-root/tsconfig.json --noemit",
"list-plugins": "./ts-node scripts/list-plugins.tsx",
"open-dist": "open ../dist/mac/Flipper.app --args --launcher=false --inspect=9229",
"postinstall": "patch-package && yarn --cwd plugins install --mutex network:30331 && yarn tsc -b pkg-lib/tsconfig.json && ./ts-node scripts/generate-plugin-entry-points.tsx && yarn build:tsc && yarn build:themes",
"postinstall": "patch-package && yarn --cwd plugins install --mutex network:30331 && yarn tsc -b pkg-lib/tsconfig.json && ./ts-node scripts/remove-plugin-entry-points.tsx && yarn build:tsc && yarn build:themes",
"prebuild": "yarn build:tsc && yarn rm-dist && yarn build:themes",
"predev-server": "yarn build:tsc",
"preflipper-server": "yarn build:tsc",

View File

@@ -24,10 +24,7 @@ import {
} from 'flipper-pkg-lib';
import getAppWatchFolders from './get-app-watch-folders';
import {getSourcePlugins, getPluginSourceFolders} from 'flipper-plugin-lib';
import type {
BundledPluginDetails,
InstalledPluginDetails,
} from 'flipper-common';
import type {InstalledPluginDetails} from 'flipper-common';
import {
appDir,
staticDir,
@@ -41,8 +38,6 @@ import pFilter from 'p-filter';
import child from 'child_process';
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,
@@ -90,7 +85,6 @@ export async function prepareDefaultPlugins(isInsidersBuild: boolean = false) {
dereference: true,
});
console.log('✅ Copied the provided default plugins dir.');
await generateDefaultPluginEntryPoints([]); // calling it here just to generate empty indexes
} else {
const sourcePlugins = process.env.FLIPPER_NO_DEFAULT_PLUGINS
? []
@@ -100,9 +94,6 @@ export async function prepareDefaultPlugins(isInsidersBuild: boolean = false) {
.filter((p) => !isInsidersBuild || hardcodedPlugins.has(p.id));
if (process.env.FLIPPER_NO_BUNDLED_PLUGINS) {
await buildDefaultPlugins(defaultPlugins);
await generateDefaultPluginEntryPoints([]); // calling it here just to generate empty indexes
} else {
await generateDefaultPluginEntryPoints(defaultPlugins);
}
}
console.log('✅ Prepared default plugins.');
@@ -123,69 +114,6 @@ export async function buildServerAddOns(dev: boolean) {
await Promise.all(serverAddOns.map((p) => runBuild(p.dir, dev)));
console.log('✅ Built plugins with server add-ons plugins.');
}
function getGeneratedIndex(pluginRequires: string) {
return `
/* eslint-disable */
// THIS FILE IS AUTO-GENERATED by function "generateDefaultPluginEntryPoints" in "build-utils.ts".
declare const require: any;
// 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 {
try {
return fn();
} catch (e) {
console.error(\`Could not require \${module}: \`, e)
return {};
}
}
export default {\n${pluginRequires}\n} as any
`;
}
async function generateDefaultPluginEntryPoints(
defaultPlugins: InstalledPluginDetails[],
) {
console.log(
`⚙️ Generating entry points for ${defaultPlugins.length} bundled plugins...`,
);
const bundledPlugins = defaultPlugins.map(
(p) =>
({
...p,
isBundled: true,
version: p.version === '0.0.0' ? version : p.version,
flipperSDKVersion:
p.flipperSDKVersion === '0.0.0' ? version : p.flipperSDKVersion,
dir: undefined,
entry: undefined,
serverAddOnEntry: undefined,
} as BundledPluginDetails),
);
const pluginRequires = bundledPlugins
.map(
(x) =>
` '${x.name}': tryRequire('${x.name}', () => require('${x.name}'))`,
)
.join(',\n');
const generatedIndex = getGeneratedIndex(pluginRequires);
await fs.ensureDir(path.join(appDir, 'src', 'defaultPlugins'));
await fs.writeFile(
path.join(appDir, 'src', 'defaultPlugins', 'index.tsx'),
generatedIndex,
);
await fs.ensureDir(path.join(browserUiDir, 'src', 'defaultPlugins'));
await fs.writeFile(
path.join(browserUiDir, 'src', 'defaultPlugins', 'index.tsx'),
generatedIndex,
);
console.log('✅ Generated bundled plugin entry points.');
}
async function buildDefaultPlugins(defaultPlugins: InstalledPluginDetails[]) {
if (process.env.FLIPPER_NO_REBUILD_PLUGINS) {
console.log(

View File

@@ -1,17 +0,0 @@
/**
* 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
*/
/* eslint-disable flipper/no-console-error-without-context */
import {prepareDefaultPlugins} from './build-utils';
prepareDefaultPlugins().catch((err) => {
console.error(err);
process.exit(1);
});

View File

@@ -181,9 +181,6 @@ function createStubRenderHost(): RenderHost {
restartFlipper() {},
openLink() {},
serverConfig: stubConfig,
loadDefaultPlugins() {
return {};
},
GK(gk: string) {
return stubConfig.gatekeepers[gk] ?? false;
},

View File

@@ -0,0 +1,35 @@
/**
* 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
*/
/* eslint-disable flipper/no-console-error-without-context */
import fs from 'fs-extra';
import path from 'path';
import {rootDir} from './paths';
// TODO: Remove me in 2023 when everyone who is building flipper from source have legacy plugin entry points removed by this script
(async () => {
await Promise.all(
[
path.resolve(rootDir, 'app/src/defaultPlugins'),
path.resolve(rootDir, 'flipper-server-companion/src/defaultPlugins'),
path.resolve(rootDir, 'flipper-server-core/src/defaultPlugins'),
path.resolve(rootDir, 'flipper-ui-browser/src/defaultPlugins'),
].map((dir) =>
fs.rm(dir, {
recursive: true,
force: true,
}),
),
);
})().catch((err) => {
console.error(err);
process.exit(1);
});