Expose the envName as a programmatic and CLI option.

This commit is contained in:
Logan Smyth
2017-11-14 23:04:08 -08:00
parent 74439889d8
commit aa2d415507
4 changed files with 14 additions and 2 deletions

View File

@@ -49,6 +49,11 @@ commander.option(
collect,
);
commander.option("--config-file [path]", "Path a to .babelrc file to use");
commander.option(
"--env-name [name]",
"The name of the 'env' to use when loading configs and plugins. " +
"Defaults to the value of BABEL_ENV, or else NODE_ENV, or else 'development'.",
);
// Basic file input configuration.
commander.option("--source-type [script|module]", "");

View File

@@ -144,10 +144,11 @@ Following is a table of the options you can use:
| `auxiliaryCommentAfter` | `null` | Attach a comment after all non-user injected code |
| `auxiliaryCommentBefore` | `null` | Attach a comment before all non-user injected code |
| `babelrc` | `true` | Specify whether or not to use .babelrc and .babelignore files. Not available when using the CLI, [use `--no-babelrc` instead](https://babeljs.io/docs/usage/cli/#babel-ignoring-babelrc) |
| `envName` | env vars | Defaults to environment variable `BABEL_ENV` if set, or else `NODE_ENV` if set, or else it defaults to `"development"` |
| `code` | `true` | Enable code generation |
| `comments` | `true` | Output comments in generated output |
| `compact` | `"auto"` | Do not include superfluous whitespace characters and line terminators. When set to `"auto"` compact is set to `true` on input sizes of >500KB |
| `env` | `{}` | This is an object of keys that represent different environments. For example, you may have: `{ env: { production: { /* specific options */ } } }` which will use those options when the environment variable `BABEL_ENV` is set to `"production"`. If `BABEL_ENV` isn't set then `NODE_ENV` will be used, if it's not set then it defaults to `"development"` |
| `env` | `{}` | This is an object of keys that represent different environments. For example, you may have: `{ env: { production: { /* specific options */ } } }` which will use those options when the `envName` is `production` |
| `extends` | `null` | A path to a `.babelrc` file to extend |
| `filename` | `"unknown"` | Filename for use in errors etc |
| `filenameRelative` | `(filename)` | Filename relative to `sourceRoot` |

View File

@@ -103,7 +103,7 @@ class OptionManager {
init(inputOpts: {}) {
const args = validate("arguments", inputOpts);
const envName = getEnv();
const { envName = getEnv() } = args;
const configChain = buildConfigChain(args, envName);
if (!configChain) return null;
@@ -133,6 +133,7 @@ class OptionManager {
.filter(plugins => plugins.length > 0)
.map(plugins => ({ plugins }));
opts.passPerPreset = opts.presets.length > 0;
opts.envName = envName;
return {
options: opts,

View File

@@ -28,6 +28,10 @@ const ROOT_VALIDATORS: ValidatorSet = {
>),
code: (assertBoolean: Validator<$PropertyType<ValidatedOptions, "code">>),
ast: (assertBoolean: Validator<$PropertyType<ValidatedOptions, "ast">>),
envName: (assertString: Validator<
$PropertyType<ValidatedOptions, "envName">,
>),
};
const NONPRESET_VALIDATORS: ValidatorSet = {
@@ -130,6 +134,7 @@ export type ValidatedOptions = {
code?: boolean,
ast?: boolean,
inputSourceMap?: RootInputSourceMapOption,
envName?: string,
extends?: string,
env?: EnvSet<ValidatedOptions>,