Centralize the plugin/configuration API object.
This commit is contained in:
parent
ddd40bf5c7
commit
148e6dfc26
@ -1,6 +1,6 @@
|
||||
// @flow
|
||||
|
||||
type SimpleCacheConfigurator = SimpleCacheConfiguratorFn &
|
||||
export type SimpleCacheConfigurator = SimpleCacheConfiguratorFn &
|
||||
SimpleCacheConfiguratorObj;
|
||||
|
||||
type SimpleCacheConfiguratorFn = {
|
||||
|
||||
@ -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();
|
||||
}
|
||||
|
||||
19
packages/babel-core/src/config/helpers/config-api.js
Normal file
19
packages/babel-core/src/config/helpers/config-api.js
Normal file
@ -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,
|
||||
};
|
||||
}
|
||||
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user