From aa2d41550717b4f8a5e7d87832ee50318b281a76 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Tue, 14 Nov 2017 23:04:08 -0800 Subject: [PATCH] Expose the envName as a programmatic and CLI option. --- packages/babel-cli/src/babel/index.js | 5 +++++ packages/babel-core/README.md | 3 ++- packages/babel-core/src/config/option-manager.js | 3 ++- packages/babel-core/src/config/options.js | 5 +++++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/babel-cli/src/babel/index.js b/packages/babel-cli/src/babel/index.js index 00fcdfbd77..606f5b33cd 100755 --- a/packages/babel-cli/src/babel/index.js +++ b/packages/babel-cli/src/babel/index.js @@ -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]", ""); diff --git a/packages/babel-core/README.md b/packages/babel-core/README.md index c014d4d77b..24262bec47 100644 --- a/packages/babel-core/README.md +++ b/packages/babel-core/README.md @@ -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` | diff --git a/packages/babel-core/src/config/option-manager.js b/packages/babel-core/src/config/option-manager.js index cd7a96a96e..d7c7c958c5 100644 --- a/packages/babel-core/src/config/option-manager.js +++ b/packages/babel-core/src/config/option-manager.js @@ -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, diff --git a/packages/babel-core/src/config/options.js b/packages/babel-core/src/config/options.js index aa915cc94c..5ca278b14b 100644 --- a/packages/babel-core/src/config/options.js +++ b/packages/babel-core/src/config/options.js @@ -28,6 +28,10 @@ const ROOT_VALIDATORS: ValidatorSet = { >), code: (assertBoolean: Validator<$PropertyType>), ast: (assertBoolean: Validator<$PropertyType>), + + envName: (assertString: Validator< + $PropertyType, + >), }; const NONPRESET_VALIDATORS: ValidatorSet = { @@ -130,6 +134,7 @@ export type ValidatedOptions = { code?: boolean, ast?: boolean, inputSourceMap?: RootInputSourceMapOption, + envName?: string, extends?: string, env?: EnvSet,