forked from babel/babel
-
Notifications
You must be signed in to change notification settings - Fork 3
/
transform.ts
44 lines (34 loc) 路 1.33 KB
/
transform.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
import gensync from "gensync";
import loadConfig from "./config";
import type { InputOptions, ResolvedConfig } from "./config";
import { run } from "./transformation";
import type { FileResult, FileResultCallback } from "./transformation";
export type { FileResult } from "./transformation";
type Transform = {
(code: string, callback: FileResultCallback): void;
(
code: string,
opts: InputOptions | undefined | null,
callback: FileResultCallback,
): void;
(code: string, opts?: InputOptions | null): FileResult | null;
};
const transformRunner = gensync<
(code: string, opts?: InputOptions) => FileResult | null
>(function* transform(code, opts) {
const config: ResolvedConfig | null = yield* loadConfig(opts);
if (config === null) return null;
return yield* run(config, code);
});
export const transform: Transform = function transform(code, opts?, callback?) {
if (typeof opts === "function") {
callback = opts;
opts = undefined;
}
// For backward-compat with Babel 6, we allow sync transformation when
// no callback is given. Will be dropped in some future Babel major version.
if (callback === undefined) return transformRunner.sync(code, opts);
transformRunner.errback(code, opts, callback);
};
export const transformSync = transformRunner.sync;
export const transformAsync = transformRunner.async;