-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
STCLI-224 Add transpile command (#295)
* Add transpile command * Cleanup * Add ability to analyze output * Cleanup
- Loading branch information
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |