UNPKG

1.8 kBPlain TextView Raw
1import gensync, { type Handler } from "gensync";
2
3import loadConfig from "./config";
4import type { InputOptions, ResolvedConfig } from "./config";
5import { run } from "./transformation";
6import type { FileResult, FileResultCallback } from "./transformation";
7import * as fs from "./gensync-utils/fs";
8
9type transformFileBrowserType = typeof import("./transform-file-browser");
10type transformFileType = typeof import("./transform-file");
11
12// Kind of gross, but essentially asserting that the exports of this module are the same as the
13// exports of transform-file-browser, since this file may be replaced at bundle time with
14// transform-file-browser.
15({} as any as transformFileBrowserType as transformFileType);
16
17const transformFileRunner = gensync(function* (
18 filename: string,
19 opts?: InputOptions,
20): Handler<FileResult | null> {
21 const options = { ...opts, filename };
22
23 const config: ResolvedConfig | null = yield* loadConfig(options);
24 if (config === null) return null;
25
26 const code = yield* fs.readFile(filename, "utf8");
27 return yield* run(config, code);
28});
29
30// @ts-expect-error TS doesn't detect that this signature is compatible
31export function transformFile(
32 filename: string,
33 callback: FileResultCallback,
34): void;
35export function transformFile(
36 filename: string,
37 opts: InputOptions | undefined | null,
38 callback: FileResultCallback,
39): void;
40export function transformFile(
41 ...args: Parameters<typeof transformFileRunner.errback>
42) {
43 return transformFileRunner.errback(...args);
44}
45
46export function transformFileSync(
47 ...args: Parameters<typeof transformFileRunner.sync>
48) {
49 return transformFileRunner.sync(...args);
50}
51export function transformFileAsync(
52 ...args: Parameters<typeof transformFileRunner.async>
53) {
54 return transformFileRunner.async(...args);
55}