package bundle

Summary:
For the electron build, plugins are bundled with the app and loaded from there at launch. The headless version can't require from its binary, so plugins need to be required from another path.

This diff makes the path where bundled plugins are loaded from adjustable via an environment variable: `BUNDLED_PLUGIN_PATH`. If it's set, the plugins are loaded from this path, otherwise we default to the old behaviour of including them from `./defaultPlugins`.

For the headless version we expect the plugins to be in a folder called `plugins` next to the executable. This should later be configurable via an argument passed to the CLI.

Reviewed By: passy

Differential Revision: D13843676

fbshipit-source-id: 04237ae6631b4f2ba56887fe992a56f860724edc
This commit is contained in:
Daniel Büchele
2019-02-04 07:21:55 -08:00
committed by Facebook Github Bot
parent d512c97cab
commit 5b68c59b5c
6 changed files with 26 additions and 10 deletions

View File

@@ -9,6 +9,7 @@ import {createStore} from 'redux';
import reducers from '../src/reducers/index.js'; import reducers from '../src/reducers/index.js';
import dispatcher from '../src/dispatcher/index.js'; import dispatcher from '../src/dispatcher/index.js';
import Logger, {init} from '../src/fb-stubs/Logger.js'; import Logger, {init} from '../src/fb-stubs/Logger.js';
import path from 'path';
// $FlowFixMe this file exist, trust me, flow! // $FlowFixMe this file exist, trust me, flow!
import setup from '../static/setup.js'; import setup from '../static/setup.js';
@@ -16,6 +17,10 @@ import setup from '../static/setup.js';
global.WebSocket = require('ws'); // used for redux devtools global.WebSocket = require('ws'); // used for redux devtools
global.fetch = require('node-fetch/lib/index'); global.fetch = require('node-fetch/lib/index');
process.env.BUNDLED_PLUGIN_PATH =
process.env.BUNDLED_PLUGIN_PATH ||
path.join(path.dirname(process.execPath), 'plugins');
// needs to be required after WebSocket polyfill is loaded // needs to be required after WebSocket polyfill is loaded
const devToolsEnhancer = require('remote-redux-devtools').default; const devToolsEnhancer = require('remote-redux-devtools').default;

View File

@@ -111,7 +111,7 @@
"reset": "yarn cache clean && yarn rm-dist && yarn rm-modules && yarn rm-temp", "reset": "yarn cache clean && yarn rm-dist && yarn rm-modules && yarn rm-temp",
"start": "cross-env NODE_ENV=development node scripts/start-dev-server.js", "start": "cross-env NODE_ENV=development node scripts/start-dev-server.js",
"build": "yarn rm-dist && cross-env NODE_ENV=production node scripts/build-release.js $@", "build": "yarn rm-dist && cross-env NODE_ENV=production node scripts/build-release.js $@",
"build-headless": "yarn rm-dist && cross-env NODE_ENV=production node scripts/build-headless.js $@", "build-headless": "yarn rm-dist && mkdir dist && cross-env NODE_ENV=production node scripts/build-headless.js $@",
"fix": "eslint . --fix", "fix": "eslint . --fix",
"test": "jest --testPathPattern=node\\.js$ --no-cache", "test": "jest --testPathPattern=node\\.js$ --no-cache",
"test-electron": "jest --testPathPattern=electron\\.js$ --testEnvironment=@jest-runner/electron/environment --runner=@jest-runner/electron --no-cache", "test-electron": "jest --testPathPattern=electron\\.js$ --testEnvironment=@jest-runner/electron/environment --runner=@jest-runner/electron --no-cache",

View File

@@ -8,7 +8,11 @@
const path = require('path'); const path = require('path');
const lineReplace = require('line-replace'); const lineReplace = require('line-replace');
const {exec: createBinary} = require('pkg'); const {exec: createBinary} = require('pkg');
const {buildFolder, compile} = require('./build-utils.js'); const {
buildFolder,
compile,
compileDefaultPlugins,
} = require('./build-utils.js');
function preludeBundle(dir) { function preludeBundle(dir) {
return new Promise((resolve, reject) => return new Promise((resolve, reject) =>
@@ -53,6 +57,7 @@ function preludeBundle(dir) {
console.log('Created build directory', buildDir); console.log('Created build directory', buildDir);
await compile(buildDir, path.join(__dirname, '..', 'headless', 'index.js')); await compile(buildDir, path.join(__dirname, '..', 'headless', 'index.js'));
await preludeBundle(buildDir); await preludeBundle(buildDir);
await compileDefaultPlugins(path.join(distDir, 'plugins'));
await createBinary([ await createBinary([
path.join(buildDir, 'bundle.js'), path.join(buildDir, 'bundle.js'),
'--output', '--output',

View File

@@ -135,7 +135,7 @@ function copyStaticFolder(buildFolder) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.log('Created build directory', dir); console.log('Created build directory', dir);
await copyStaticFolder(dir); await copyStaticFolder(dir);
await compileDefaultPlugins(dir); await compileDefaultPlugins(path.join(dir, 'defaultPlugins'));
await compile(dir, path.join(__dirname, '..', 'src', 'init.js')); await compile(dir, path.join(__dirname, '..', 'src', 'init.js'));
const versionNumber = await modifyPackageManifest(dir); const versionNumber = await modifyPackageManifest(dir);
generateManifest(versionNumber); generateManifest(versionNumber);

View File

@@ -16,9 +16,7 @@ function die(err) {
process.exit(1); process.exit(1);
} }
function compileDefaultPlugins(buildFolder) { function compileDefaultPlugins(defaultPluginDir) {
const defaultPluginFolder = 'defaultPlugins';
const defaultPluginDir = path.join(buildFolder, defaultPluginFolder);
return compilePlugins( return compilePlugins(
null, null,
[ [
@@ -34,7 +32,7 @@ function compileDefaultPlugins(buildFolder) {
JSON.stringify( JSON.stringify(
defaultPlugins.map(plugin => ({ defaultPlugins.map(plugin => ({
...plugin, ...plugin,
out: path.join(defaultPluginFolder, path.parse(plugin.out).base), out: path.parse(plugin.out).base,
})), })),
), ),
), ),

View File

@@ -23,6 +23,7 @@ import {remote} from 'electron';
import GK from '../fb-stubs/GK'; import GK from '../fb-stubs/GK';
import {FlipperBasePlugin} from '../plugin.js'; import {FlipperBasePlugin} from '../plugin.js';
import {setupMenuBar} from '../MenuBar.js'; import {setupMenuBar} from '../MenuBar.js';
import path from 'path';
export type PluginDefinition = { export type PluginDefinition = {
name: string, name: string,
@@ -74,14 +75,21 @@ export default (store: Store, logger: Logger) => {
function getBundledPlugins(): Array<PluginDefinition> { function getBundledPlugins(): Array<PluginDefinition> {
// DefaultPlugins that are included in the bundle. // DefaultPlugins that are included in the bundle.
// List of defaultPlugins is written at build time // List of defaultPlugins is written at build time
const pluginPath =
process.env.BUNDLED_PLUGIN_PATH || path.join(__dirname, 'defaultPlugins');
let bundledPlugins: Array<PluginDefinition> = []; let bundledPlugins: Array<PluginDefinition> = [];
try { try {
bundledPlugins = global.electronRequire('./defaultPlugins/index.json'); bundledPlugins = global.electronRequire(
} catch (e) {} path.join(pluginPath, 'index.json'),
);
} catch (e) {
console.error(e);
}
return bundledPlugins.map(plugin => ({ return bundledPlugins.map(plugin => ({
...plugin, ...plugin,
out: './' + plugin.out, out: path.join(pluginPath, plugin.out),
})); }));
} }