Summary: Pull Request resolved: https://github.com/facebook/flipper/pull/998 After this diff all the default plugins (which are distributed with Flipper) will be included into the main app bundle instead of bundling each of them separately and then loading from file system. This is done by auto-generating plugins index in build-time and importing it from Flipper app bundle, so Metro can follow these imports and bundle all the plugins to the app bundle. This provides several benefits: 1) reduced Flipper bundle size (~10% reduction of zipped Flipper archive), because Metro bundles each of re-used dependencies only once instead of bundling them for each plugin where such dependency used. 2) Faster Flipper startup because of reduced bundle and the fact that we don't need to load each plugin bundle from disk - just need to load the single bundle where everything is already included. 3) Metro dev server for plugins works in the same way as for Flipper app itself, e.g. simple refresh automatically recompiles bundled plugins too if there are changes. This also potentially should allow us to enable "fast refresh" for quicker iterations while developing plugins. 4) Faster build ("yarn build --mac" is 2 times faster on my machine after this change) Potential downsides: 1) Currently all the plugins are identically loaded from disk. After this change some of plugins will be bundled, and some of them (third-party) will be loaded from disk. 2) In future when it will be possible to publish new versions of default plugins separately, installing new version of such plugin (e.g. with some urgent fix) will mean the "default" pre-built version will still be bundled (we cannot "unbundle" it :)), but we'll skip it and instead load new version from disk. Changelog: Internals: include default plugins into the main bundle instead producing separate bundles for them. Reviewed By: passy Differential Revision: D20864002 fbshipit-source-id: 2968f3b786cdd1767d6223996090143d03894b92
210 lines
5.6 KiB
TypeScript
210 lines
5.6 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 Metro from 'metro';
|
|
import tmp from 'tmp';
|
|
import path from 'path';
|
|
import fs from 'fs-extra';
|
|
import {spawn} from 'promisify-child-process';
|
|
import getWatchFolders from '../static/getWatchFolders';
|
|
import getAppWatchFolders from './get-app-watch-folders';
|
|
import getPlugins from '../static/getPlugins';
|
|
import {
|
|
appDir,
|
|
staticDir,
|
|
defaultPluginsIndexDir,
|
|
headlessDir,
|
|
babelTransformationsDir,
|
|
} from './paths';
|
|
import getPluginFolders from '../static/getPluginFolders';
|
|
|
|
const dev = process.env.NODE_ENV !== 'production';
|
|
|
|
export function die(err: Error) {
|
|
console.error(err.stack);
|
|
process.exit(1);
|
|
}
|
|
|
|
export async function generatePluginEntryPoints() {
|
|
console.log('⚙️ Generating plugin entry points...');
|
|
const pluginEntryPoints = await getPlugins();
|
|
if (await fs.pathExists(defaultPluginsIndexDir)) {
|
|
await fs.remove(defaultPluginsIndexDir);
|
|
}
|
|
await fs.mkdirp(defaultPluginsIndexDir);
|
|
await fs.writeJSON(
|
|
path.join(defaultPluginsIndexDir, 'index.json'),
|
|
pluginEntryPoints.map((plugin) => plugin.manifest),
|
|
);
|
|
const pluginRequres = pluginEntryPoints
|
|
.map((x) => ` '${x.name}': require('${x.name}')`)
|
|
.join(',\n');
|
|
const generatedIndex = `
|
|
// THIS FILE IS AUTO-GENERATED by function "generatePluginEntryPoints" in "build-utils.ts".
|
|
export default {\n${pluginRequres}\n} as any
|
|
`;
|
|
await fs.ensureDir(path.join(appDir, 'src', 'defaultPlugins'));
|
|
await fs.writeFile(
|
|
path.join(appDir, 'src', 'defaultPlugins', 'index.tsx'),
|
|
generatedIndex,
|
|
);
|
|
console.log('✅ Generated plugin entry points.');
|
|
}
|
|
|
|
async function compile(
|
|
buildFolder: string,
|
|
projectRoot: string,
|
|
watchFolders: string[],
|
|
entry: string,
|
|
) {
|
|
await Metro.runBuild(
|
|
{
|
|
reporter: {update: () => {}},
|
|
projectRoot,
|
|
watchFolders,
|
|
serializer: {},
|
|
transformer: {
|
|
babelTransformerPath: path.join(
|
|
babelTransformationsDir,
|
|
'transform-app',
|
|
),
|
|
},
|
|
resolver: {
|
|
resolverMainFields: ['flipper:source', 'module', 'main'],
|
|
blacklistRE: /\.native\.js$/,
|
|
sourceExts: ['js', 'jsx', 'ts', 'tsx', 'json', 'mjs'],
|
|
},
|
|
},
|
|
{
|
|
dev,
|
|
minify: false,
|
|
resetCache: !dev,
|
|
sourceMap: true,
|
|
entry,
|
|
out: path.join(buildFolder, 'bundle.js'),
|
|
},
|
|
);
|
|
}
|
|
|
|
export async function compileHeadless(buildFolder: string) {
|
|
console.log(`⚙️ Compiling headless bundle...`);
|
|
const watchFolders = [
|
|
headlessDir,
|
|
...(await getWatchFolders(staticDir)),
|
|
...(await getAppWatchFolders()),
|
|
...(await getPluginFolders()),
|
|
]
|
|
.filter((value, index, self) => self.indexOf(value) === index)
|
|
.filter(fs.pathExistsSync);
|
|
try {
|
|
await compile(
|
|
buildFolder,
|
|
headlessDir,
|
|
watchFolders,
|
|
path.join(headlessDir, 'index.tsx'),
|
|
);
|
|
console.log('✅ Compiled headless bundle.');
|
|
} catch (err) {
|
|
die(err);
|
|
}
|
|
}
|
|
|
|
export async function compileRenderer(buildFolder: string) {
|
|
console.log(`⚙️ Compiling renderer bundle...`);
|
|
const watchFolders = [
|
|
...(await getAppWatchFolders()),
|
|
...(await getPluginFolders()),
|
|
];
|
|
try {
|
|
await compile(
|
|
buildFolder,
|
|
appDir,
|
|
watchFolders,
|
|
path.join(appDir, 'src', 'init.tsx'),
|
|
);
|
|
console.log('✅ Compiled renderer bundle.');
|
|
} catch (err) {
|
|
die(err);
|
|
}
|
|
}
|
|
|
|
export async function compileMain() {
|
|
const out = path.join(staticDir, 'main.bundle.js');
|
|
process.env.FLIPPER_ELECTRON_VERSION = require('electron/package.json').version;
|
|
console.log('⚙️ Compiling main bundle...');
|
|
try {
|
|
const config = Object.assign({}, await Metro.loadConfig(), {
|
|
reporter: {update: () => {}},
|
|
projectRoot: staticDir,
|
|
watchFolders: await getWatchFolders(staticDir),
|
|
transformer: {
|
|
babelTransformerPath: path.join(
|
|
babelTransformationsDir,
|
|
'transform-main',
|
|
),
|
|
},
|
|
resolver: {
|
|
sourceExts: ['tsx', 'ts', 'js'],
|
|
resolverMainFields: ['flipper:source', 'module', 'main'],
|
|
blacklistRE: /\.native\.js$/,
|
|
},
|
|
});
|
|
await Metro.runBuild(config, {
|
|
platform: 'web',
|
|
entry: path.join(staticDir, 'main.ts'),
|
|
out,
|
|
dev,
|
|
minify: false,
|
|
sourceMap: true,
|
|
resetCache: !dev,
|
|
});
|
|
console.log('✅ Compiled main bundle.');
|
|
} catch (err) {
|
|
die(err);
|
|
}
|
|
}
|
|
export function buildFolder(): Promise<string> {
|
|
// eslint-disable-next-line no-console
|
|
console.log('Creating build directory');
|
|
return new Promise<string>((resolve, reject) => {
|
|
tmp.dir({prefix: 'flipper-build-'}, (err, buildFolder) => {
|
|
if (err) {
|
|
reject(err);
|
|
} else {
|
|
resolve(buildFolder);
|
|
}
|
|
});
|
|
}).catch((e) => {
|
|
die(e);
|
|
return '';
|
|
});
|
|
}
|
|
export function getVersionNumber() {
|
|
let {version} = require('../package.json');
|
|
const buildNumber = process.argv.join(' ').match(/--version=(\d+)/);
|
|
if (buildNumber && buildNumber.length > 0) {
|
|
version = [...version.split('.').slice(0, 2), buildNumber[1]].join('.');
|
|
}
|
|
return version;
|
|
}
|
|
|
|
// Asynchronously determine current mercurial revision as string or `null` in case of any error.
|
|
export function genMercurialRevision(): Promise<string | null> {
|
|
return spawn('hg', ['log', '-r', '.', '-T', '{node}'], {encoding: 'utf8'})
|
|
.then(
|
|
(res) =>
|
|
(res &&
|
|
(typeof res.stdout === 'string'
|
|
? res.stdout
|
|
: res.stdout?.toString())) ||
|
|
null,
|
|
)
|
|
.catch(() => null);
|
|
}
|