From 2774cb7d4243027c7c7730a84cd1efc5f3f83c8c Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Thu, 13 Apr 2017 13:39:41 -0700 Subject: [PATCH] Allow function-based .babelrc.js files. --- .../src/config/loading/files/configuration.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/babel-core/src/config/loading/files/configuration.js b/packages/babel-core/src/config/loading/files/configuration.js index aa47e374f5..4827f7300d 100644 --- a/packages/babel-core/src/config/loading/files/configuration.js +++ b/packages/babel-core/src/config/loading/files/configuration.js @@ -4,6 +4,7 @@ import path from "path"; import fs from "fs"; import json5 from "json5"; import resolve from "resolve"; +import { getEnv } from "../../helpers/environment"; import { makeStrongCache } from "../../caching"; type ConfigFile = { @@ -101,6 +102,16 @@ const readConfigJS = makeStrongCache((filepath, cache) => { throw err; } + if (typeof options === "function") { + options = options({ + cache, + // Expose ".env()" so people can easily get the same env that we expose using the "env" key. + env: () => cache.using(() => getEnv()), + }); + } else { + cache.forever(); + } + if (!options || typeof options !== "object" || Array.isArray(options)) { throw new Error(`${filepath}: Configuration should be an exported JavaScript object.`); } @@ -110,7 +121,7 @@ const readConfigJS = makeStrongCache((filepath, cache) => { dirname: path.dirname(filepath), options, }; -}); +}, false /* autoPermacache */); const readConfigFile = makeStaticFileCache((filepath, content) => { let options;