From 58617043612fd6ed184ae44aeed8808c0263388c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Tue, 23 Feb 2021 02:01:39 +0100 Subject: [PATCH] Export function versions of `createConfigItem` (#12852) --- packages/babel-core/src/config/index.js | 25 ++++++++++++++ packages/babel-core/src/index.js | 6 +++- packages/babel-core/test/config-loading.js | 40 +++++++++++++++++++++- 3 files changed, 69 insertions(+), 2 deletions(-) diff --git a/packages/babel-core/src/config/index.js b/packages/babel-core/src/config/index.js index 2f9a229b7cab..ef331f45ca51 100644 --- a/packages/babel-core/src/config/index.js +++ b/packages/babel-core/src/config/index.js @@ -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); + } +} diff --git a/packages/babel-core/src/index.js b/packages/babel-core/src/index.js index 2a7d05a899da..7cabc44ae6b2 100644 --- a/packages/babel-core/src/index.js +++ b/packages/babel-core/src/index.js @@ -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, diff --git a/packages/babel-core/test/config-loading.js b/packages/babel-core/test/config-loading.js index 736534ecd8ae..9968dc2c08e3 100644 --- a/packages/babel-core/test/config-loading.js +++ b/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; @@ -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 () {