Skip to content

Commit

Permalink
Export function versions of createConfigItem (#12852)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Feb 23, 2021
1 parent e3f0903 commit 5861704
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
25 changes: 25 additions & 0 deletions packages/babel-core/src/config/index.js
Expand Up @@ -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;
Expand All @@ -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);
}
}
6 changes: 5 additions & 1 deletion packages/babel-core/src/index.js
Expand Up @@ -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,
Expand Down
40 changes: 39 additions & 1 deletion packages/babel-core/test/config-loading.js
@@ -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;
Expand Down Expand Up @@ -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 () {
Expand Down

0 comments on commit 5861704

Please sign in to comment.