Skip to content

Commit

Permalink
STCLI-224 Add transpile command (#295)
Browse files Browse the repository at this point in the history
* Add transpile command

* Cleanup

* Add ability to analyze output

* Cleanup
  • Loading branch information
mkuklis committed Mar 21, 2023
1 parent c7a31f3 commit f6a2944
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions lib/commands/transpile.js
Original file line number Diff line number Diff line change
@@ -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,
};

0 comments on commit f6a2944

Please sign in to comment.