From 148e6dfc261e1bf1a2b50f5ae3e470c9bab1644e Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Wed, 15 Nov 2017 11:38:53 -0800 Subject: [PATCH 1/2] Centralize the plugin/configuration API object. --- packages/babel-core/src/config/caching.js | 2 +- .../src/config/files/configuration.js | 8 ++------ .../src/config/helpers/config-api.js | 19 +++++++++++++++++++ packages/babel-core/src/config/index.js | 7 ++----- 4 files changed, 24 insertions(+), 12 deletions(-) create mode 100644 packages/babel-core/src/config/helpers/config-api.js diff --git a/packages/babel-core/src/config/caching.js b/packages/babel-core/src/config/caching.js index b9703560b5..7c3a28bda2 100644 --- a/packages/babel-core/src/config/caching.js +++ b/packages/babel-core/src/config/caching.js @@ -1,6 +1,6 @@ // @flow -type SimpleCacheConfigurator = SimpleCacheConfiguratorFn & +export type SimpleCacheConfigurator = SimpleCacheConfiguratorFn & SimpleCacheConfiguratorObj; type SimpleCacheConfiguratorFn = { diff --git a/packages/babel-core/src/config/files/configuration.js b/packages/babel-core/src/config/files/configuration.js index 73786b89fb..c1a55cc598 100644 --- a/packages/babel-core/src/config/files/configuration.js +++ b/packages/babel-core/src/config/files/configuration.js @@ -6,6 +6,7 @@ import fs from "fs"; import json5 from "json5"; import resolve from "resolve"; import { makeStrongCache, type CacheConfigurator } from "../caching"; +import makeAPI from "../helpers/config-api"; const debug = buildDebug("babel:config:loading:files:configuration"); @@ -150,12 +151,7 @@ const readConfigJS = makeStrongCache( } if (typeof options === "function") { - options = options({ - cache: cache.simple(), - // Expose ".env()" so people can easily get the same env that we expose using the "env" key. - env: () => cache.using(data => data.envName), - async: () => false, - }); + options = options(makeAPI(cache)); if (!cache.configured()) throwConfigError(); } diff --git a/packages/babel-core/src/config/helpers/config-api.js b/packages/babel-core/src/config/helpers/config-api.js new file mode 100644 index 0000000000..a579cf28fa --- /dev/null +++ b/packages/babel-core/src/config/helpers/config-api.js @@ -0,0 +1,19 @@ +// @flow +import type { CacheConfigurator, SimpleCacheConfigurator } from "../caching"; + +export type PluginAPI = { + cache: SimpleCacheConfigurator, + env: () => string, + async: () => boolean, +}; + +export default function makeAPI( + cache: CacheConfigurator<{ envName: string }>, +): PluginAPI { + return { + cache: cache.simple(), + // Expose ".env()" so people can easily get the same env that we expose using the "env" key. + env: () => cache.using(data => data.envName), + async: () => false, + }; +} diff --git a/packages/babel-core/src/config/index.js b/packages/babel-core/src/config/index.js index 05ac8e7ce9..5ee1c04818 100644 --- a/packages/babel-core/src/config/index.js +++ b/packages/babel-core/src/config/index.js @@ -16,6 +16,7 @@ import { makeWeakCache, type CacheConfigurator } from "./caching"; import { getEnv } from "./helpers/environment"; import { validate, type ValidatedOptions } from "./validation/options"; import { validatePluginObject } from "./validation/plugins"; +import makeAPI from "./helpers/config-api"; type LoadedDescriptor = { value: {}, @@ -202,11 +203,7 @@ const loadDescriptor = makeWeakCache( let item = value; if (typeof value === "function") { - const api = Object.assign(Object.create(context), { - cache: cache.simple(), - env: () => cache.using(data => data.envName), - async: () => false, - }); + const api = Object.assign(Object.create(context), makeAPI(cache)); try { item = value(api, options, dirname); From 2c3eb3096f596acfc07c236860fac0eec90f9c94 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Wed, 15 Nov 2017 12:39:09 -0800 Subject: [PATCH 2/2] Expand the '.env()' API call with more flexibility. --- .../src/config/helpers/config-api.js | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/babel-core/src/config/helpers/config-api.js b/packages/babel-core/src/config/helpers/config-api.js index a579cf28fa..933c605016 100644 --- a/packages/babel-core/src/config/helpers/config-api.js +++ b/packages/babel-core/src/config/helpers/config-api.js @@ -1,19 +1,40 @@ // @flow import type { CacheConfigurator, SimpleCacheConfigurator } from "../caching"; +type EnvFunction = { + (): string, + ((string) => T): T, + (string): boolean, + (Array): boolean, +}; + export type PluginAPI = { cache: SimpleCacheConfigurator, - env: () => string, + env: EnvFunction, async: () => boolean, }; export default function makeAPI( cache: CacheConfigurator<{ envName: string }>, ): PluginAPI { + const env: any = value => + cache.using(data => { + if (typeof value === "undefined") return data.envName; + if (typeof value === "function") return value(data.envName); + if (!Array.isArray(value)) value = [value]; + + return value.some(entry => { + if (typeof entry !== "string") { + throw new Error("Unexpected non-string value"); + } + return entry === data.envName; + }); + }); + return { cache: cache.simple(), // Expose ".env()" so people can easily get the same env that we expose using the "env" key. - env: () => cache.using(data => data.envName), + env, async: () => false, }; }