Summary: Sorry for long diff! I can try to split it if necessary, but many changes here are 1-1 replacements / renames. **Preambule** Currently we bundle default plugins into the Flipper main bundle. This helps us to reduce bundle size, because of plugin dependencies re-use. E.g. if multiple plugins use "lodash" when they are bundled together, only one copy of "lodash" added. When they are bundled separately, the same dependency might be added to each of them. However as we're not going to include most of plugins into Flipper distributive anymore and going to rely on Marketplace instead, this bundling doesn't provide significant size benefits anymore. In addition to that, bundling makes it impossible to differentiate whether thrown errors are originated from Flipper core or one of its plugins. Why don't we remove plugin bundling at all? Because for "dev mode" it actually quite useful. It makes dev build start much faster and also enables using of Fast Refresh for plugin development (fast refresh won't work for plugins loaded from disk). **Changes** This diff introduces new option "no-bundled-plugins" for "yarn start" and "yarn build" commands. For now, by default, we will continue bundling default plugins into the Flipper main bundle, but if this option provided then we will build each default plugin separately and include their packages into the Flipper distributive as "pre-installed" to be able to load them from disk even without access to Marketplace. For "yarn start", we're adding symlinks to plugin folders in "static/defaultPlugins" and then they are loaded by Flipper. For "yarn build" we are dereferencing these symlinks to include physical files of plugins into folder "defaultPlugins" of the produced distributive. Folder "defaultPlugins" is excluded from asar, because loading of plugins from asar archive might introduce some unexpected issues depending on their implementation. Reviewed By: mweststrate Differential Revision: D28431838 fbshipit-source-id: f7757e9f5ba9183ed918d70252de3ce0e823177d
150 lines
4.0 KiB
TypeScript
150 lines
4.0 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 fs from 'fs-extra';
|
|
import path from 'path';
|
|
import {exec} from 'child_process';
|
|
import {EOL} from 'os';
|
|
import pmap from 'p-map';
|
|
import {rootDir} from './paths';
|
|
import yargs from 'yargs';
|
|
import {isPluginJson} from 'flipper-plugin-lib';
|
|
|
|
const argv = yargs
|
|
.usage('yarn tsc-plugins [args]')
|
|
.version(false)
|
|
.options({
|
|
dir: {
|
|
description: 'Plugins directory name ("plugins" by default)',
|
|
type: 'string',
|
|
default: 'plugins',
|
|
alias: 'd',
|
|
},
|
|
})
|
|
.help()
|
|
.strict()
|
|
.parse(process.argv.slice(1));
|
|
|
|
const pluginsDir = path.join(rootDir, argv.dir);
|
|
const fbPluginsDir = path.join(pluginsDir, 'fb');
|
|
const publicPluginsDir = path.join(pluginsDir, 'public');
|
|
|
|
async function tscPlugins(): Promise<number> {
|
|
const stdout = await new Promise<string | undefined>((resolve) =>
|
|
exec(
|
|
`./node_modules/.bin/tsc -p ./${argv.dir}/tsconfig.json`,
|
|
{
|
|
cwd: rootDir,
|
|
},
|
|
(err, stdout) => {
|
|
if (err) {
|
|
console.error(err);
|
|
resolve(stdout);
|
|
} else {
|
|
resolve(undefined);
|
|
}
|
|
},
|
|
),
|
|
);
|
|
if (stdout) {
|
|
console.error(stdout);
|
|
}
|
|
const errors = (stdout?.split(EOL) ?? []).filter((l) => l !== '');
|
|
if (errors.length > 0) {
|
|
await findAffectedPlugins(errors);
|
|
}
|
|
return stdout ? 1 : 0;
|
|
}
|
|
|
|
async function findAffectedPlugins(errors: string[]) {
|
|
const [publicPackages, fbPackages] = await Promise.all([
|
|
fs.readdir(publicPluginsDir),
|
|
fs.readdir(fbPluginsDir).catch(() => [] as string[]),
|
|
]);
|
|
const allPackages = await pmap(
|
|
[
|
|
...publicPackages.map((p) => path.join(publicPluginsDir, p)),
|
|
...fbPackages.map((p) => path.join(fbPluginsDir, p)),
|
|
],
|
|
async (p) => ({
|
|
dir: p,
|
|
json: await fs
|
|
.readJson(path.join(p, 'package.json'))
|
|
.catch(() => undefined),
|
|
}),
|
|
).then((dirs) => dirs.filter((dir) => !!dir.json));
|
|
const packageByName = new Map(
|
|
allPackages.map((p) => [p.json.name as string, p]),
|
|
);
|
|
const depsByName = new Map<string, Set<string>>();
|
|
function getDependencies(name: string): Set<string> {
|
|
if (!depsByName.has(name)) {
|
|
const set = new Set<string>();
|
|
const pkg = packageByName.get(name)!;
|
|
set.add(name);
|
|
const allDeps = Object.keys({
|
|
...(pkg.json.dependencies ?? {}),
|
|
...(pkg.json.peerDependencies ?? {}),
|
|
});
|
|
for (const dep of allDeps) {
|
|
if (packageByName.get(dep)) {
|
|
const subDeps = getDependencies(dep);
|
|
for (const subDep of subDeps) {
|
|
set.add(subDep);
|
|
}
|
|
}
|
|
}
|
|
depsByName.set(name, set);
|
|
}
|
|
return depsByName.get(name)!;
|
|
}
|
|
for (const name of packageByName.keys()) {
|
|
depsByName.set(name, getDependencies(name));
|
|
}
|
|
for (const pkg of allPackages) {
|
|
if (!isPluginJson(pkg.json)) {
|
|
continue;
|
|
}
|
|
const logFile = path.join(pkg.dir, 'tsc-error.log');
|
|
await fs.remove(logFile);
|
|
let logStream: fs.WriteStream | undefined;
|
|
for (const dep of depsByName.get(pkg.json.name)!) {
|
|
const relativeDir = path.relative(rootDir, packageByName.get(dep)!.dir);
|
|
for (const error of errors) {
|
|
if (error.startsWith(relativeDir)) {
|
|
if (!logStream) {
|
|
logStream = fs.createWriteStream(logFile);
|
|
console.error(
|
|
`Plugin ${path.relative(
|
|
rootDir,
|
|
pkg.dir,
|
|
)} has tsc errors. Check ${path.relative(
|
|
rootDir,
|
|
logFile,
|
|
)} for details.`,
|
|
);
|
|
}
|
|
logStream.write(error);
|
|
logStream.write(EOL);
|
|
}
|
|
}
|
|
}
|
|
logStream?.close();
|
|
}
|
|
}
|
|
|
|
tscPlugins()
|
|
.then((code) => {
|
|
process.exit(code);
|
|
})
|
|
.catch((err: any) => {
|
|
console.error(err);
|
|
process.exit(1);
|
|
});
|