Skip to content

Commit

Permalink
Delete undefined options so we handle old core versions with new CLI …
Browse files Browse the repository at this point in the history
…versions. (#8728)
  • Loading branch information
loganfsmyth committed Sep 19, 2018
1 parent 9e7c7f5 commit 3b0f9a9
Showing 1 changed file with 40 additions and 29 deletions.
69 changes: 40 additions & 29 deletions packages/babel-cli/src/babel/options.js
Expand Up @@ -221,36 +221,47 @@ export default function parseArgv(args: Array<string>) {

const opts = commander.opts();

const babelOptions = {
presets: opts.presets,
plugins: opts.plugins,
rootMode: opts.rootMode,
configFile: opts.configFile,
envName: opts.envName,
sourceType: opts.sourceType,
ignore: opts.ignore,
only: opts.only,
retainLines: opts.retainLines,
compact: opts.compact,
minified: opts.minified,
auxiliaryCommentBefore: opts.auxiliaryCommentBefore,
auxiliaryCommentAfter: opts.auxiliaryCommentAfter,
sourceMaps: opts.sourceMaps,
sourceFileName: opts.sourceFileName,
sourceRoot: opts.sourceRoot,
moduleRoot: opts.moduleRoot,
moduleIds: opts.moduleIds,
moduleId: opts.moduleId,

// Commander will default the "--no-" arguments to true, but we want to
// leave them undefined so that @babel/core can handle the
// default-assignment logic on its own.
babelrc: opts.babelrc === true ? undefined : opts.babelrc,
highlightCode: opts.highlightCode === true ? undefined : opts.highlightCode,
comments: opts.comments === true ? undefined : opts.comments,
};

// If the @babel/cli version is newer than the @babel/core version, and we have added
// new options for @babel/core, we'll potentially get option validation errors from
// @babel/core. To avoid that, we delete undefined options, so @babel/core will only
// give the error if users actually pass an unsupported CLI option.
for (const key of Object.keys(babelOptions)) {
if (babelOptions[key] === undefined) {
delete babelOptions[key];
}
}

return {
babelOptions: {
presets: opts.presets,
plugins: opts.plugins,
rootMode: opts.rootMode,
configFile: opts.configFile,
envName: opts.envName,
sourceType: opts.sourceType,
ignore: opts.ignore,
only: opts.only,
retainLines: opts.retainLines,
compact: opts.compact,
minified: opts.minified,
auxiliaryCommentBefore: opts.auxiliaryCommentBefore,
auxiliaryCommentAfter: opts.auxiliaryCommentAfter,
sourceMaps: opts.sourceMaps,
sourceFileName: opts.sourceFileName,
sourceRoot: opts.sourceRoot,
moduleRoot: opts.moduleRoot,
moduleIds: opts.moduleIds,
moduleId: opts.moduleId,

// Commander will default the "--no-" arguments to true, but we want to
// leave them undefined so that @babel/core can handle the
// default-assignment logic on its own.
babelrc: opts.babelrc === true ? undefined : opts.babelrc,
highlightCode:
opts.highlightCode === true ? undefined : opts.highlightCode,
comments: opts.comments === true ? undefined : opts.comments,
},
babelOptions,
cliOptions: {
filename: opts.filename,
filenames,
Expand Down

0 comments on commit 3b0f9a9

Please sign in to comment.