forked from babel/babel
/
index.ts
73 lines (61 loc) 路 2.48 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import gensync from "gensync";
export type {
ResolvedConfig,
InputOptions,
PluginPasses,
Plugin,
} from "./full";
import type { PluginTarget } from "./validation/options";
import type {
PluginAPI as basePluginAPI,
PresetAPI as basePresetAPI,
} from "./helpers/config-api";
export type { PluginObject } from "./validation/plugins";
type PluginAPI = basePluginAPI & typeof import("..");
type PresetAPI = basePresetAPI & typeof import("..");
export type { PluginAPI, PresetAPI };
// todo: may need to refine PresetObject to be a subset of ValidatedOptions
export type { ValidatedOptions as PresetObject } 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";
import type { ConfigItem } from "./item";
const loadOptionsRunner = gensync<(opts: unknown) => any>(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<(...args: Parameters<typeof createConfigItemImpl>) => ConfigItem>(
createConfigItemImpl,
);
const maybeErrback = runner => (opts: unknown, callback?: Function) => {
if (callback === undefined && typeof opts === "function") {
callback = opts;
opts = undefined;
}
return callback ? runner.errback(opts, callback) : runner.sync(opts);
};
export const loadPartialConfig = maybeErrback(loadPartialConfigRunner);
export const loadPartialConfigSync = loadPartialConfigRunner.sync;
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?: (err: Error, val: ConfigItem | null) => void,
) {
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);
}
}