Merge pull request #7449 from loganfsmyth/env-enhancement

Centralize the plugin/preset and config APIs into one place
This commit is contained in:
Logan Smyth 2018-02-28 01:03:35 -08:00 committed by GitHub
commit a07f96ce3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 12 deletions

View File

@ -1,6 +1,6 @@
// @flow // @flow
type SimpleCacheConfigurator = SimpleCacheConfiguratorFn & export type SimpleCacheConfigurator = SimpleCacheConfiguratorFn &
SimpleCacheConfiguratorObj; SimpleCacheConfiguratorObj;
type SimpleCacheConfiguratorFn = { type SimpleCacheConfiguratorFn = {

View File

@ -6,6 +6,7 @@ import fs from "fs";
import json5 from "json5"; import json5 from "json5";
import resolve from "resolve"; import resolve from "resolve";
import { makeStrongCache, type CacheConfigurator } from "../caching"; import { makeStrongCache, type CacheConfigurator } from "../caching";
import makeAPI from "../helpers/config-api";
const debug = buildDebug("babel:config:loading:files:configuration"); const debug = buildDebug("babel:config:loading:files:configuration");
@ -150,12 +151,7 @@ const readConfigJS = makeStrongCache(
} }
if (typeof options === "function") { if (typeof options === "function") {
options = options({ options = options(makeAPI(cache));
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,
});
if (!cache.configured()) throwConfigError(); if (!cache.configured()) throwConfigError();
} }

View File

@ -0,0 +1,40 @@
// @flow
import type { CacheConfigurator, SimpleCacheConfigurator } from "../caching";
type EnvFunction = {
(): string,
<T>((string) => T): T,
(string): boolean,
(Array<string>): boolean,
};
export type PluginAPI = {
cache: SimpleCacheConfigurator,
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,
async: () => false,
};
}

View File

@ -16,6 +16,7 @@ import { makeWeakCache, type CacheConfigurator } from "./caching";
import { getEnv } from "./helpers/environment"; import { getEnv } from "./helpers/environment";
import { validate, type ValidatedOptions } from "./validation/options"; import { validate, type ValidatedOptions } from "./validation/options";
import { validatePluginObject } from "./validation/plugins"; import { validatePluginObject } from "./validation/plugins";
import makeAPI from "./helpers/config-api";
type LoadedDescriptor = { type LoadedDescriptor = {
value: {}, value: {},
@ -202,11 +203,7 @@ const loadDescriptor = makeWeakCache(
let item = value; let item = value;
if (typeof value === "function") { if (typeof value === "function") {
const api = Object.assign(Object.create(context), { const api = Object.assign(Object.create(context), makeAPI(cache));
cache: cache.simple(),
env: () => cache.using(data => data.envName),
async: () => false,
});
try { try {
item = value(api, options, dirname); item = value(api, options, dirname);