1- import gensync , { type Handler , type Callback } from "gensync" ;
1+ import gensync , { type Handler } from "gensync" ;
22
33export type {
44 ResolvedConfig ,
@@ -24,62 +24,125 @@ export type {
2424} from "./validation/options" ;
2525
2626import loadFullConfig , { type ResolvedConfig } from "./full" ;
27- import { loadPartialConfig as loadPartialConfigRunner } from "./partial" ;
27+ import {
28+ type PartialConfig ,
29+ loadPartialConfig as loadPartialConfigImpl ,
30+ } from "./partial" ;
2831
2932export { loadFullConfig as default } ;
3033export type { PartialConfig } from "./partial" ;
3134
3235import { createConfigItem as createConfigItemImpl } from "./item" ;
3336import type { ConfigItem } from "./item" ;
3437
35- const loadOptionsRunner = gensync ( function * (
36- opts : unknown ,
37- ) : Handler < ResolvedConfig | null > {
38+ import { beginHiddenCallStack } from "../errors/rewrite-stack-trace" ;
39+
40+ const loadPartialConfigRunner = gensync ( loadPartialConfigImpl ) ;
41+ export function loadPartialConfigAsync (
42+ ...args : Parameters < typeof loadPartialConfigRunner . async >
43+ ) {
44+ return beginHiddenCallStack ( loadPartialConfigRunner . async ) ( ...args ) ;
45+ }
46+ export function loadPartialConfigSync (
47+ ...args : Parameters < typeof loadPartialConfigRunner . sync >
48+ ) {
49+ return beginHiddenCallStack ( loadPartialConfigRunner . sync ) ( ...args ) ;
50+ }
51+ export function loadPartialConfig (
52+ opts : Parameters < typeof loadPartialConfigImpl > [ 0 ] ,
53+ callback ?: ( err : Error , val : PartialConfig | null ) => void ,
54+ ) {
55+ if ( callback !== undefined ) {
56+ beginHiddenCallStack ( loadPartialConfigRunner . errback ) ( opts , callback ) ;
57+ } else if ( typeof opts === "function" ) {
58+ beginHiddenCallStack ( loadPartialConfigRunner . errback ) (
59+ undefined ,
60+ opts as ( err : Error , val : PartialConfig | null ) => void ,
61+ ) ;
62+ } else {
63+ if ( process . env . BABEL_8_BREAKING ) {
64+ throw new Error (
65+ "Starting from Babel 8.0.0, the 'loadPartialConfig' function expects a callback. If you need to call it synchronously, please use 'loadPartialConfigSync'." ,
66+ ) ;
67+ } else {
68+ return loadPartialConfigSync ( opts ) ;
69+ }
70+ }
71+ }
72+
73+ function * loadOptionsImpl ( opts : unknown ) : Handler < ResolvedConfig | null > {
3874 const config = yield * loadFullConfig ( opts ) ;
3975 // NOTE: We want to return "null" explicitly, while ?. alone returns undefined
4076 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 ;
77+ }
78+ const loadOptionsRunner = gensync ( loadOptionsImpl ) ;
79+ export function loadOptionsAsync (
80+ ...args : Parameters < typeof loadOptionsRunner . async >
81+ ) {
82+ return beginHiddenCallStack ( loadOptionsRunner . async ) ( ...args ) ;
83+ }
84+ export function loadOptionsSync (
85+ ...args : Parameters < typeof loadOptionsRunner . sync >
86+ ) {
87+ return beginHiddenCallStack ( loadOptionsRunner . sync ) ( ...args ) ;
88+ }
89+ export function loadOptions (
90+ opts : Parameters < typeof loadOptionsImpl > [ 0 ] ,
91+ callback ?: ( err : Error , val : ResolvedConfig | null ) => void ,
92+ ) {
93+ if ( callback !== undefined ) {
94+ beginHiddenCallStack ( loadOptionsRunner . errback ) ( opts , callback ) ;
95+ } else if ( typeof opts === "function" ) {
96+ beginHiddenCallStack ( loadOptionsRunner . errback ) (
97+ undefined ,
98+ opts as ( err : Error , val : ResolvedConfig | null ) => void ,
99+ ) ;
100+ } else {
101+ if ( process . env . BABEL_8_BREAKING ) {
102+ throw new Error (
103+ "Starting from Babel 8.0.0, the 'loadOptions' function expects a callback. If you need to call it synchronously, please use 'loadOptionsSync'." ,
104+ ) ;
53105 } else {
54- callback = maybeCallback ;
55- arg = argOrCallback as Arg ;
56- }
57- if ( ! callback ) {
58- return runner . sync ( arg ) ;
106+ return loadOptionsSync ( opts ) ;
59107 }
60- runner . errback ( arg , callback ) ;
61- } ;
62-
63- export const loadPartialConfig = maybeErrback ( loadPartialConfigRunner ) ;
64- export const loadPartialConfigSync = loadPartialConfigRunner . sync ;
65- export const loadPartialConfigAsync = loadPartialConfigRunner . async ;
66-
67- export const loadOptions = maybeErrback ( loadOptionsRunner ) ;
68- export const loadOptionsSync = loadOptionsRunner . sync ;
69- export const loadOptionsAsync = loadOptionsRunner . async ;
108+ }
109+ }
70110
71- export const createConfigItemSync = createConfigItemRunner . sync ;
72- export const createConfigItemAsync = createConfigItemRunner . async ;
111+ const createConfigItemRunner = gensync ( createConfigItemImpl ) ;
112+ export function createConfigItemAsync (
113+ ...args : Parameters < typeof createConfigItemRunner . async >
114+ ) {
115+ return beginHiddenCallStack ( createConfigItemRunner . async ) ( ...args ) ;
116+ }
117+ export function createConfigItemSync (
118+ ...args : Parameters < typeof createConfigItemRunner . sync >
119+ ) {
120+ return beginHiddenCallStack ( createConfigItemRunner . sync ) ( ...args ) ;
121+ }
73122export function createConfigItem (
74123 target : PluginTarget ,
75124 options : Parameters < typeof createConfigItemImpl > [ 1 ] ,
76125 callback ?: ( err : Error , val : ConfigItem < PluginAPI > | null ) => void ,
77126) {
78127 if ( callback !== undefined ) {
79- createConfigItemRunner . errback ( target , options , callback ) ;
128+ beginHiddenCallStack ( createConfigItemRunner . errback ) (
129+ target ,
130+ options ,
131+ callback ,
132+ ) ;
80133 } else if ( typeof options === "function" ) {
81- createConfigItemRunner . errback ( target , undefined , callback ) ;
134+ beginHiddenCallStack ( createConfigItemRunner . errback ) (
135+ target ,
136+ undefined ,
137+ callback ,
138+ ) ;
82139 } else {
83- return createConfigItemRunner . sync ( target , options ) ;
140+ if ( process . env . BABEL_8_BREAKING ) {
141+ throw new Error (
142+ "Starting from Babel 8.0.0, the 'createConfigItem' function expects a callback. If you need to call it synchronously, please use 'createConfigItemSync'." ,
143+ ) ;
144+ } else {
145+ return createConfigItemSync ( target , options ) ;
146+ }
84147 }
85148}
0 commit comments