Export function versions of createConfigItem (#12852)
This commit is contained in:
parent
e3f090395a
commit
5861704361
@ -9,18 +9,27 @@ export type {
|
||||
Plugin,
|
||||
} from "./full";
|
||||
|
||||
import type { PluginTarget } from "./validation/options";
|
||||
|
||||
import loadFullConfig from "./full";
|
||||
import { loadPartialConfig as loadPartialConfigRunner } from "./partial";
|
||||
|
||||
export { loadFullConfig as default };
|
||||
export type { PartialConfig } from "./partial";
|
||||
|
||||
import { createConfigItem as createConfigItemImpl } from "./item";
|
||||
|
||||
const loadOptionsRunner = gensync<[mixed], Object | null>(function* (opts) {
|
||||
const config = yield* loadFullConfig(opts);
|
||||
// NOTE: We want to return "null" explicitly, while ?. alone returns undefined
|
||||
return config?.options ?? null;
|
||||
});
|
||||
|
||||
const createConfigItemRunner = gensync<[PluginTarget, any], Object | null>(
|
||||
// $FlowIgnore
|
||||
createConfigItemImpl,
|
||||
);
|
||||
|
||||
const maybeErrback = runner => (opts: mixed, callback: Function) => {
|
||||
if (callback === undefined && typeof opts === "function") {
|
||||
callback = opts;
|
||||
@ -36,3 +45,19 @@ export const loadPartialConfigAsync = loadPartialConfigRunner.async;
|
||||
export const loadOptions = maybeErrback(loadOptionsRunner);
|
||||
export const loadOptionsSync = loadOptionsRunner.sync;
|
||||
export const loadOptionsAsync = loadOptionsRunner.async;
|
||||
|
||||
export const createConfigItemSync = createConfigItemRunner.sync;
|
||||
export const createConfigItemAsync = createConfigItemRunner.async;
|
||||
export function createConfigItem(
|
||||
target: PluginTarget,
|
||||
options: any,
|
||||
callback?: Function,
|
||||
) {
|
||||
if (callback !== undefined) {
|
||||
return createConfigItemRunner.errback(target, options, callback);
|
||||
} else if (typeof options === "function") {
|
||||
return createConfigItemRunner.errback(target, undefined, callback);
|
||||
} else {
|
||||
return createConfigItemRunner.sync(target, options);
|
||||
}
|
||||
}
|
||||
|
||||
@ -14,7 +14,11 @@ export { tokTypes } from "@babel/parser";
|
||||
export { default as traverse } from "@babel/traverse";
|
||||
export { default as template } from "@babel/template";
|
||||
|
||||
export { createConfigItem } from "./config/item";
|
||||
export {
|
||||
createConfigItem,
|
||||
createConfigItemSync,
|
||||
createConfigItemAsync,
|
||||
} from "./config";
|
||||
|
||||
export {
|
||||
loadPartialConfig,
|
||||
|
||||
@ -1,4 +1,7 @@
|
||||
import loadConfigRunner, { loadPartialConfig } from "../lib/config";
|
||||
import loadConfigRunner, {
|
||||
loadPartialConfig,
|
||||
createConfigItem,
|
||||
} from "../lib/config";
|
||||
import path from "path";
|
||||
|
||||
const loadConfig = loadConfigRunner.sync;
|
||||
@ -38,6 +41,41 @@ describe("@babel/core config loading", () => {
|
||||
};
|
||||
}
|
||||
|
||||
describe("createConfigItem", () => {
|
||||
// Windows uses different file paths
|
||||
const noWin = process.platform === "win32" ? it.skip : it;
|
||||
|
||||
noWin("can be called synchronously with one param", () => {
|
||||
function myPlugin() {
|
||||
return {};
|
||||
}
|
||||
|
||||
expect(createConfigItem(myPlugin)).toEqual({
|
||||
dirname: process.cwd(),
|
||||
file: undefined,
|
||||
name: undefined,
|
||||
options: undefined,
|
||||
value: myPlugin,
|
||||
});
|
||||
});
|
||||
|
||||
noWin("can be called synchronously with two params", () => {
|
||||
function myPlugin() {
|
||||
return {};
|
||||
}
|
||||
|
||||
expect(
|
||||
createConfigItem(myPlugin, { dirname: "/foo", type: "plugin" }),
|
||||
).toEqual({
|
||||
dirname: "/foo",
|
||||
file: undefined,
|
||||
name: undefined,
|
||||
options: undefined,
|
||||
value: myPlugin,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("loadPartialConfig", () => {
|
||||
it("should preserve disabled plugins in the partial config", () => {
|
||||
const plugin = function () {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user