Debian Codegraph @

packages/babel-core/src/config/index.ts

raw
1 import gensync, { type Handler, type Callback } from "gensync";
2
3 export type {
4 ResolvedConfig,
5 InputOptions,
6 PluginPasses,
7 Plugin,
8 } from "./full";
9
10 import type { PluginTarget } from "./validation/options";
11
12 import type {
13 PluginAPI as basePluginAPI,
14 PresetAPI as basePresetAPI,
15 } from "./helpers/config-api";
16 export type { PluginObject } from "./validation/plugins";
17 type PluginAPI = basePluginAPI & typeof import("..");
18 type PresetAPI = basePresetAPI & typeof import("..");
19 export type { PluginAPI, PresetAPI };
20 // todo: may need to refine PresetObject to be a subset of ValidatedOptions
21 export type {
22 CallerMetadata,
23 ValidatedOptions as PresetObject,
24 } from "./validation/options";
25
26 import loadFullConfig, { type ResolvedConfig } from "./full";
27 import { loadPartialConfig as loadPartialConfigRunner } from "./partial";
28
29 export { loadFullConfig as default };
30 export type { PartialConfig } from "./partial";
31
32 import { createConfigItem as createConfigItemImpl } from "./item";
33 import type { ConfigItem } from "./item";
34
35 const loadOptionsRunner = gensync(function* (
36 opts: unknown,
37 ): Handler<ResolvedConfig | null> {
38 const config = yield* loadFullConfig(opts);
39 // NOTE: We want to return "null" explicitly, while ?. alone returns undefined
40 return config?.options ?? null;
41 });
42
43 const createConfigItemRunner = gensync(createConfigItemImpl);
44
45 const maybeErrback =
46 <Arg, Return>(runner: gensync.Gensync<[Arg], Return>) =>
47 (argOrCallback: Arg | Callback<Return>, maybeCallback?: Callback<Return>) => {
48 let arg: Arg | undefined;
49 let callback: Callback<Return>;
50 if (maybeCallback === undefined && typeof argOrCallback === "function") {
51 callback = argOrCallback as Callback<Return>;
52 arg = undefined;
53 } else {
54 callback = maybeCallback;
55 arg = argOrCallback as Arg;
56 }
57 return callback ? runner.errback(arg, callback) : runner.sync(arg);
58 };
59
60 export const loadPartialConfig = maybeErrback(loadPartialConfigRunner);
61 export const loadPartialConfigSync = loadPartialConfigRunner.sync;
62 export const loadPartialConfigAsync = loadPartialConfigRunner.async;
63
64 export const loadOptions = maybeErrback(loadOptionsRunner);
65 export const loadOptionsSync = loadOptionsRunner.sync;
66 export const loadOptionsAsync = loadOptionsRunner.async;
67
68 export const createConfigItemSync = createConfigItemRunner.sync;
69 export const createConfigItemAsync = createConfigItemRunner.async;
70 export function createConfigItem(
71 target: PluginTarget,
72 options: Parameters<typeof createConfigItemImpl>[1],
73 callback?: (err: Error, val: ConfigItem | null) => void,
74 ) {
75 if (callback !== undefined) {
76 return createConfigItemRunner.errback(target, options, callback);
77 } else if (typeof options === "function") {
78 return createConfigItemRunner.errback(target, undefined, callback);
79 } else {
80 return createConfigItemRunner.sync(target, options);
81 }
82 }