Fix generation of bundled.json, make source maps work in prod builds

Summary:
This diff fixes several issues around loading plugin, such as:

* make suresource maps work in the flipper-server production build
* make sure default plugins are no symlinked, which wouldn't work anywhere else but on the the system where it was build
* support release channel param for flipper-server

Bundled flipper-server is now 42MB (with icons (see later diffs) and plugins
```
ll flipper-server-v0.0.0.tgz
-rw-r--r--  1 mweststrate  staff    42M 23 Dec 15:29 flipper-server-v0.0.0.tgz
```

Reviewed By: nikoant

Differential Revision: D33294677

fbshipit-source-id: 63538dc8127f883fee6a3608673ad11ce239b350
This commit is contained in:
Michel Weststrate
2021-12-24 02:15:25 -08:00
committed by Facebook GitHub Bot
parent b1d960e4c4
commit 72fa481d27
10 changed files with 86 additions and 30 deletions

View File

@@ -15,10 +15,18 @@ import {
launchServer,
prepareDefaultPlugins,
} from './build-utils';
import {serverStaticDir, staticDir} from './paths';
import {
defaultPluginsDir,
serverDefaultPluginsDir,
serverStaticDir,
staticDir,
} from './paths';
import isFB from './isFB';
import yargs from 'yargs';
import {copy, mkdir, remove} from 'fs-extra';
import fs from 'fs-extra';
import copyPackageWithDependencies, {
copyPackageWithDependenciesRecursive,
} from './copy-package-with-dependencies';
const argv = yargs
.usage('yarn build-flipper-server [args]')
@@ -51,6 +59,11 @@ const argv = yargs
'Load only specified plugins and skip loading rest. This is useful when you are developing only one or few plugins. Plugins to load can be specified as a comma-separated list with either plugin id or name used as identifier, e.g. "--enabled-plugins network,inspector". The flag is not provided by default which means that all plugins loaded.',
type: 'array',
},
channel: {
description: 'Release channel for the build',
choices: ['stable', 'insiders'],
default: 'stable',
},
})
.version('DEV')
.help()
@@ -60,7 +73,8 @@ if (isFB) {
process.env.FLIPPER_FB = 'true';
}
// Don't bundle any plugins into the UI
process.env.FLIPPER_RELEASE_CHANNEL = argv.channel;
process.env.FLIPPER_NO_BUNDLED_PLUGINS = 'true';
// Don't rebuild default plugins, mostly to speed up testing
@@ -93,10 +107,10 @@ if (argv['enabled-plugins'] !== undefined) {
}
// clear and re-create static dir
await remove(serverStaticDir);
await mkdir(serverStaticDir);
await fs.remove(serverStaticDir);
await fs.mkdir(serverStaticDir);
await prepareDefaultPlugins(false);
await prepareDefaultPlugins(argv.channel === 'insiders');
await compileServerMain(false);
await buildBrowserBundle(false);
@@ -111,11 +125,32 @@ if (argv['enabled-plugins'] !== undefined) {
});
async function copyStaticResources() {
console.log(`⚙️ Copying default plugins...`);
await fs.mkdirp(serverDefaultPluginsDir);
const plugins = await fs.readdir(defaultPluginsDir);
for (const plugin of plugins) {
let source = path.join(defaultPluginsDir, plugin);
// static/defaultPlugins will symlink, resolve those first
while ((await fs.lstat(source)).isSymbolicLink()) {
source = await fs.readlink(source);
}
const target = path.join(serverDefaultPluginsDir, plugin);
if ((await fs.stat(source)).isDirectory()) {
// for plugins, only copy package.json & dist, to keep impact minimal
await fs.copy(
path.join(source, 'package.json'),
path.join(target, 'package.json'),
);
await fs.copy(path.join(source, 'dist'), path.join(target, 'dist'));
} else {
await fs.copy(source, target);
}
}
console.log(`⚙️ Copying static resources...`);
// static folder, without the things that are only for Electron
const thingsToCopy = [
'defaultPlugins',
'facebook',
'icons',
'native-modules',
@@ -134,7 +169,7 @@ async function copyStaticResources() {
await Promise.all(
thingsToCopy.map((e) =>
copy(path.join(staticDir, e), path.join(serverStaticDir, e)),
fs.copy(path.join(staticDir, e), path.join(serverStaticDir, e)),
),
);
console.log('✅ Copied static resources.');

View File

@@ -31,7 +31,7 @@ import {
import fetch from '@adobe/node-fetch-retry';
import isFB from './isFB';
import copyPackageWithDependencies from './copy-package-with-dependencies';
import {staticDir, distDir} from './paths';
import {staticDir, distDir, defaultPluginsDir} from './paths';
import yargs from 'yargs';
import {WinPackager} from 'app-builder-lib/out/winPackager';
// eslint-disable-next-line node/no-extraneous-import

View File

@@ -420,9 +420,6 @@ export async function compileServerMain(dev: boolean) {
resetCache: !dev,
});
console.log('✅ Compiled server bundle.');
if (!dev) {
stripSourceMapComment(out);
}
}
// TODO: needed?
@@ -503,9 +500,6 @@ export async function buildBrowserBundle(dev: boolean) {
inlineSourceMap: false,
});
console.log('✅ Compiled browser bundle.');
if (!dev) {
stripSourceMapComment(out);
}
}
async function dedupeFolders(paths: string[]): Promise<string[]> {

View File

@@ -16,6 +16,10 @@ export const staticDir = path.join(rootDir, 'static');
export const serverDir = path.join(rootDir, 'flipper-server');
export const serverStaticDir = path.join(serverDir, 'static'); // for pre-bundled server, static resources are copied here
export const defaultPluginsDir = path.join(staticDir, 'defaultPlugins');
export const serverDefaultPluginsDir = path.join(
serverStaticDir,
'defaultPlugins',
);
export const pluginsDir = path.join(rootDir, 'plugins');
export const fbPluginsDir = path.join(pluginsDir, 'fb');
export const publicPluginsDir = path.join(pluginsDir, 'public');

View File

@@ -65,6 +65,11 @@ const argv = yargs
type: 'boolean',
default: true,
},
channel: {
description: 'Release channel for the build',
choices: ['stable', 'insiders'],
default: 'stable',
},
})
.version('DEV')
.help()
@@ -74,6 +79,8 @@ if (isFB) {
process.env.FLIPPER_FB = 'true';
}
process.env.FLIPPER_RELEASE_CHANNEL = argv.channel;
if (argv['default-plugins'] === true) {
delete process.env.FLIPPER_NO_DEFAULT_PLUGINS;
} else if (argv['default-plugins'] === false) {