From 77ecbabf60d8f8a694a38affc55857fa6e177ffb Mon Sep 17 00:00:00 2001 From: Jordan Brant Baker Date: Mon, 7 Aug 2017 10:23:56 -0700 Subject: [PATCH] Support "env" in .babelrc files. (#8963) --- packages/babel-compiler/babel-compiler.js | 10 ++++++++++ tools/tests/apps/modules/.babelrc | 7 +++++++ tools/tests/apps/modules/babel-env.js | 18 ++++++++++++++++++ tools/tests/apps/modules/package.json | 3 +++ 4 files changed, 38 insertions(+) create mode 100644 tools/tests/apps/modules/.babelrc create mode 100644 tools/tests/apps/modules/babel-env.js diff --git a/packages/babel-compiler/babel-compiler.js b/packages/babel-compiler/babel-compiler.js index c10289c199..ecc2428bb6 100644 --- a/packages/babel-compiler/babel-compiler.js +++ b/packages/babel-compiler/babel-compiler.js @@ -300,6 +300,16 @@ BCp._inferHelper = function ( merge(babelOptions, babelrc, "presets"); merge(babelOptions, babelrc, "plugins"); + const babelEnv = (process.env.BABEL_ENV || + process.env.NODE_ENV || + "development"); + if (babelrc && babelrc.env && babelrc.env[babelEnv]) { + const env = babelrc.env[babelEnv]; + walkBabelRC(env); + merge(babelOptions, env, "presets"); + merge(babelOptions, env, "plugins"); + } + return !! (babelrc.presets || babelrc.plugins); }; diff --git a/tools/tests/apps/modules/.babelrc b/tools/tests/apps/modules/.babelrc new file mode 100644 index 0000000000..f5e2cf5851 --- /dev/null +++ b/tools/tests/apps/modules/.babelrc @@ -0,0 +1,7 @@ +{ + "env": { + "development": { + "plugins": ["transform-do-expressions"] + } + } +} diff --git a/tools/tests/apps/modules/babel-env.js b/tools/tests/apps/modules/babel-env.js new file mode 100644 index 0000000000..3cc0035b6d --- /dev/null +++ b/tools/tests/apps/modules/babel-env.js @@ -0,0 +1,18 @@ +function babeltest() { + // use transform-do-expressions plugin to prove babel `env` subkey was loaded + let x = do { + 1; + }; + console.log(x) +} + +/* + If the plugin is loaded correctly there will be no errors during the compilation of this file. + Without this plugin you will get the error: + + W20170803-17:58:17.054(-7)? (STDERR) var x = do { + W20170803-17:58:17.055(-7)? (STDERR) ^^ + W20170803-17:58:17.055(-7)? (STDERR) + W20170803-17:58:17.055(-7)? (STDERR) SyntaxError: Unexpected token do +*/ + diff --git a/tools/tests/apps/modules/package.json b/tools/tests/apps/modules/package.json index fd63fa15cb..29a8f86116 100644 --- a/tools/tests/apps/modules/package.json +++ b/tools/tests/apps/modules/package.json @@ -19,5 +19,8 @@ "test": "METEOR_PROFILE=100 ../../../../meteor test --full-app --driver-package dispatch:mocha-phantomjs", "browser": "METEOR_PROFILE=100 ../../../../meteor test --full-app --driver-package dispatch:mocha-browser", "test-packages": "../../../../meteor test-packages --driver-package dispatch:mocha-phantomjs packages/modules-test-package" + }, + "devDependencies": { + "babel-plugin-transform-do-expressions": "^6.22.0" } }