From 752757c16f085f4dad8a43d61afe32d17b901151 Mon Sep 17 00:00:00 2001 From: Michal Kuklis Date: Tue, 25 Apr 2023 09:57:51 -0400 Subject: [PATCH 1/2] STRIPES-861: Setup federation --- lib/commands/federate.js | 57 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 lib/commands/federate.js diff --git a/lib/commands/federate.js b/lib/commands/federate.js new file mode 100644 index 0000000..7affbda --- /dev/null +++ b/lib/commands/federate.js @@ -0,0 +1,57 @@ +const importLazy = require('import-lazy')(require); + +const { contextMiddleware } = importLazy('../cli/context-middleware'); +const StripesCore = importLazy('../cli/stripes-core'); +const StripesPlatform = importLazy('../platform/stripes-platform'); +const { stripesConfigFile } = importLazy('./common-options'); + +let _stripesPlatform; +let _stripesCore; + +// stripesPlatform and stripesCore overrides primarily used as injection for unit tests +function stripesOverrides(stripesPlatform, stripesCore) { + _stripesPlatform = stripesPlatform; + _stripesCore = stripesCore; +} + +function federateCommand(argv) { + const context = argv.context; + // Default federate command to production env + if (!process.env.NODE_ENV) { + process.env.NODE_ENV = 'production'; + } + + const platform = _stripesPlatform || new StripesPlatform(argv.stripesConfig, context, argv); + const webpackOverrides = []; + + if (argv.analyze) { + const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin; // eslint-disable-line + webpackOverrides.push((config) => { + config.plugins.push(new BundleAnalyzerPlugin()); + return config; + }); + } + + console.info('Federate module...'); + const stripes = _stripesCore || new StripesCore(context, platform.aliases); + stripes.api.federate(Object.assign({}, argv, { webpackOverrides })); +} + +module.exports = { + command: 'federate', + describe: 'federate single module', + builder: (yargs) => { + yargs + .middleware([ + contextMiddleware(), + ]) + .positional('configFile', stripesConfigFile.configFile) + .option('analyze', { + describe: 'Run the Webpack Bundle Analyzer after build (launches in browser)', + type: 'boolean', + }) + .example('$0 federate', 'federate a module'); + }, + handler: federateCommand, + stripesOverrides, +}; From 7259755492f67e29a164471db49b66f260f31af7 Mon Sep 17 00:00:00 2001 From: Michal Kuklis Date: Mon, 1 May 2023 13:43:59 -0400 Subject: [PATCH 2/2] Introduce port argument --- lib/commands/federate.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/commands/federate.js b/lib/commands/federate.js index 7affbda..c696adf 100644 --- a/lib/commands/federate.js +++ b/lib/commands/federate.js @@ -50,6 +50,10 @@ module.exports = { describe: 'Run the Webpack Bundle Analyzer after build (launches in browser)', type: 'boolean', }) + .option('port', { + describe: 'A port which will be used for the remote federated module', + type: 'number', + }) .example('$0 federate', 'federate a module'); }, handler: federateCommand,