Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STCLI-224 Add transpile command #295

Merged
merged 10 commits into from
Mar 21, 2023
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,
};