From f6a29442c6d47f46de2e7e1f6284b1ad923f973c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kuklis?= Date: Tue, 21 Mar 2023 10:53:26 -0400 Subject: [PATCH] STCLI-224 Add transpile command (#295) * Add transpile command * Cleanup * Add ability to analyze output * Cleanup --- lib/commands/transpile.js | 60 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 lib/commands/transpile.js diff --git a/lib/commands/transpile.js b/lib/commands/transpile.js new file mode 100644 index 0000000..1c2b7db --- /dev/null +++ b/lib/commands/transpile.js @@ -0,0 +1,60 @@ +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'); +const { processError, processStats } = importLazy('../webpack-common'); + +let _stripesPlatform; +let _stripesCore; + +// stripesPlatform and stripesCore overrides primarily used as injection for unit tests +function stripesOverrides(stripesPlatform, stripesCore) { + _stripesPlatform = stripesPlatform; + _stripesCore = stripesCore; +} + +function transpileCommand(argv) { + const context = argv.context; + // Default transpile 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('Transpiling...'); + const stripes = _stripesCore || new StripesCore(context, platform.aliases); + stripes.api.transpile(Object.assign({}, argv, { webpackOverrides })) + .then(processStats) + .catch(processError); +} + +module.exports = { + command: 'transpile', + describe: 'Transpile 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 transpile', 'Transpile a module'); + }, + handler: transpileCommand, + stripesOverrides, +};