Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export function versions of createConfigItem #12852

Merged
merged 6 commits into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/babel-core/src/config/index.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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" }),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shows that it should be fixed for gatsby.

).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