From 5b68c59b5cc08cc1d28e2213af467230941d2d51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20B=C3=BCchele?= Date: Mon, 4 Feb 2019 07:21:55 -0800 Subject: [PATCH] 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 --- headless/index.js | 5 +++++ package.json | 2 +- scripts/build-headless.js | 7 ++++++- scripts/build-release.js | 2 +- scripts/build-utils.js | 6 ++---- src/dispatcher/plugins.js | 14 +++++++++++--- 6 files changed, 26 insertions(+), 10 deletions(-) diff --git a/headless/index.js b/headless/index.js index 36ea75d61..dab3e590e 100644 --- a/headless/index.js +++ b/headless/index.js @@ -9,6 +9,7 @@ import {createStore} from 'redux'; import reducers from '../src/reducers/index.js'; import dispatcher from '../src/dispatcher/index.js'; import Logger, {init} from '../src/fb-stubs/Logger.js'; +import path from 'path'; // $FlowFixMe this file exist, trust me, flow! 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.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 const devToolsEnhancer = require('remote-redux-devtools').default; diff --git a/package.json b/package.json index 7fd4a1b9f..31c4f0ccb 100644 --- a/package.json +++ b/package.json @@ -111,7 +111,7 @@ "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", "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", "test": "jest --testPathPattern=node\\.js$ --no-cache", "test-electron": "jest --testPathPattern=electron\\.js$ --testEnvironment=@jest-runner/electron/environment --runner=@jest-runner/electron --no-cache", diff --git a/scripts/build-headless.js b/scripts/build-headless.js index f31b609bf..5a86a477b 100644 --- a/scripts/build-headless.js +++ b/scripts/build-headless.js @@ -8,7 +8,11 @@ const path = require('path'); const lineReplace = require('line-replace'); const {exec: createBinary} = require('pkg'); -const {buildFolder, compile} = require('./build-utils.js'); +const { + buildFolder, + compile, + compileDefaultPlugins, +} = require('./build-utils.js'); function preludeBundle(dir) { return new Promise((resolve, reject) => @@ -53,6 +57,7 @@ function preludeBundle(dir) { console.log('Created build directory', buildDir); await compile(buildDir, path.join(__dirname, '..', 'headless', 'index.js')); await preludeBundle(buildDir); + await compileDefaultPlugins(path.join(distDir, 'plugins')); await createBinary([ path.join(buildDir, 'bundle.js'), '--output', diff --git a/scripts/build-release.js b/scripts/build-release.js index 4dfe608b7..e21ce017c 100755 --- a/scripts/build-release.js +++ b/scripts/build-release.js @@ -135,7 +135,7 @@ function copyStaticFolder(buildFolder) { // eslint-disable-next-line no-console console.log('Created build directory', dir); await copyStaticFolder(dir); - await compileDefaultPlugins(dir); + await compileDefaultPlugins(path.join(dir, 'defaultPlugins')); await compile(dir, path.join(__dirname, '..', 'src', 'init.js')); const versionNumber = await modifyPackageManifest(dir); generateManifest(versionNumber); diff --git a/scripts/build-utils.js b/scripts/build-utils.js index e72ec3ed7..fa94e0ac2 100644 --- a/scripts/build-utils.js +++ b/scripts/build-utils.js @@ -16,9 +16,7 @@ function die(err) { process.exit(1); } -function compileDefaultPlugins(buildFolder) { - const defaultPluginFolder = 'defaultPlugins'; - const defaultPluginDir = path.join(buildFolder, defaultPluginFolder); +function compileDefaultPlugins(defaultPluginDir) { return compilePlugins( null, [ @@ -34,7 +32,7 @@ function compileDefaultPlugins(buildFolder) { JSON.stringify( defaultPlugins.map(plugin => ({ ...plugin, - out: path.join(defaultPluginFolder, path.parse(plugin.out).base), + out: path.parse(plugin.out).base, })), ), ), diff --git a/src/dispatcher/plugins.js b/src/dispatcher/plugins.js index 110ba384f..dc77d1dac 100644 --- a/src/dispatcher/plugins.js +++ b/src/dispatcher/plugins.js @@ -23,6 +23,7 @@ import {remote} from 'electron'; import GK from '../fb-stubs/GK'; import {FlipperBasePlugin} from '../plugin.js'; import {setupMenuBar} from '../MenuBar.js'; +import path from 'path'; export type PluginDefinition = { name: string, @@ -74,14 +75,21 @@ export default (store: Store, logger: Logger) => { function getBundledPlugins(): Array { // DefaultPlugins that are included in the bundle. // List of defaultPlugins is written at build time + const pluginPath = + process.env.BUNDLED_PLUGIN_PATH || path.join(__dirname, 'defaultPlugins'); + let bundledPlugins: Array = []; try { - bundledPlugins = global.electronRequire('./defaultPlugins/index.json'); - } catch (e) {} + bundledPlugins = global.electronRequire( + path.join(pluginPath, 'index.json'), + ); + } catch (e) { + console.error(e); + } return bundledPlugins.map(plugin => ({ ...plugin, - out: './' + plugin.out, + out: path.join(pluginPath, plugin.out), })); }