Summary:
- Removed compilation on startup which is not required anymore after switching to plugin spec v2.
- Removed from Node process ("static" package) all the dependencies which are not required anymore: e.g. metro, babel etc.
- Plugin loading code from node process moved to browser process and made asyncronous.
Some expected benefits after these changes:
1) Reduced size of Flipper bundle (~4.5MB reduction for lzma package in my tests) as well as startup time. It's hard to say the exact startup time difference as it is very machine-dependent, and on my machine it was already fast ~1500ms (vs 5500ms for p95) and decreased by just 100ms. But I think we should definitely see some improvements on "launch time" analytics graph for p95/p99.
2) Plugin loading is async now and happens when UI is already shown, so perceptive startup time should be also better now.
3) All plugin loading code is now consolidated in "app/dispatcher/plugins.tsx" instead of being splitted between Node and Browser processes as before. So it will be easier to debug plugin loading.
4) Now it is possible to apply updates of plugins by simple refresh of browser window instead of full Electron process restart as before.
5) 60% less code in Node process. This is good because it is harder to debug changes in Node process than in Browser process, especially taking in account differences between dev/release builds. Because of this Node process often ended up broken after changes. Hopefully now it will be more stable.
Changelog: changed the way of plugin loading, and removed obsolete dependencies, which should reduce bundle size and startup time.
Reviewed By: passy
Differential Revision: D23682756
fbshipit-source-id: 8445c877234b41c73853cebe585e2fdb1638b2c9
89 lines
2.7 KiB
TypeScript
89 lines
2.7 KiB
TypeScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @format
|
|
*/
|
|
|
|
import path from 'path';
|
|
import fs from 'fs-extra';
|
|
import pMap from 'p-map';
|
|
import {
|
|
PluginDetails,
|
|
getSourcePlugins,
|
|
getInstalledPlugins,
|
|
finishPendingPluginInstallations,
|
|
} from 'flipper-plugin-lib';
|
|
import os from 'os';
|
|
import {getStaticPath} from '../utils/pathUtils';
|
|
|
|
const pluginCache = path.join(os.homedir(), '.flipper', 'plugins');
|
|
|
|
// Load "dynamic" plugins, e.g. those which are either installed or loaded from sources for development purposes.
|
|
// This opposed to "static" plugins which are already included into Flipper bundle.
|
|
export default async function loadDynamicPlugins(): Promise<PluginDetails[]> {
|
|
if (process.env.FLIPPER_FAST_REFRESH) {
|
|
console.log(
|
|
'❌ Skipping loading of dynamic plugins because Fast Refresh is enabled. Fast Refresh only works with bundled plugins.',
|
|
);
|
|
return [];
|
|
}
|
|
try {
|
|
await finishPendingPluginInstallations();
|
|
} catch (err) {
|
|
console.error('❌ Failed to finish pending installations', err);
|
|
}
|
|
const staticPath = getStaticPath();
|
|
const defaultPlugins = new Set<string>(
|
|
(
|
|
await fs.readJson(path.join(staticPath, 'defaultPlugins', 'index.json'))
|
|
).map((p: any) => p.name) as string[],
|
|
);
|
|
const dynamicPlugins = [
|
|
...(await getInstalledPlugins()),
|
|
...(await getSourcePlugins()).filter((p) => !defaultPlugins.has(p.name)),
|
|
];
|
|
await fs.ensureDir(pluginCache);
|
|
const compilations = pMap(
|
|
dynamicPlugins,
|
|
(plugin) => {
|
|
return loadPlugin(plugin);
|
|
},
|
|
{concurrency: 4},
|
|
);
|
|
const compiledDynamicPlugins = (await compilations).filter(
|
|
(c) => c !== null,
|
|
) as PluginDetails[];
|
|
console.log('✅ Loaded all plugins.');
|
|
return compiledDynamicPlugins;
|
|
}
|
|
async function loadPlugin(
|
|
pluginDetails: PluginDetails,
|
|
): Promise<PluginDetails | null> {
|
|
const {specVersion, version, entry, name} = pluginDetails;
|
|
if (specVersion > 1) {
|
|
if (await fs.pathExists(entry)) {
|
|
return pluginDetails;
|
|
} else {
|
|
console.error(
|
|
`❌ Plugin ${name} is ignored, because its entry point not found: ${entry}.`,
|
|
);
|
|
return null;
|
|
}
|
|
} else {
|
|
// Try to load cached version of legacy plugin
|
|
const entry = path.join(pluginCache, `${name}@${version || '0.0.0'}.js`);
|
|
if (await fs.pathExists(entry)) {
|
|
console.log(`🥫 Using cached version of legacy plugin ${name}...`);
|
|
return pluginDetails;
|
|
} else {
|
|
console.error(
|
|
`❌ Plugin ${name} is ignored, because it is defined by the unsupported spec v1 and could not be compiled.`,
|
|
);
|
|
return null;
|
|
}
|
|
}
|
|
}
|