UNPKG

1.48 kBPlain TextView Raw
1import gensync 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
17type TransformFile = {
18 (filename: string, callback: FileResultCallback): void;
19 (
20 filename: string,
21 opts: InputOptions | undefined | null,
22 callback: FileResultCallback,
23 ): void;
24};
25
26const transformFileRunner = gensync<
27 (filename: string, opts?: InputOptions) => FileResult | null
28>(function* (filename, opts: InputOptions) {
29 const options = { ...opts, filename };
30
31 const config: ResolvedConfig | null = yield* loadConfig(options);
32 if (config === null) return null;
33
34 const code = yield* fs.readFile(filename, "utf8");
35 return yield* run(config, code);
36});
37
38export const transformFile = transformFileRunner.errback as TransformFile;
39export const transformFileSync = transformFileRunner.sync;
40export const transformFileAsync = transformFileRunner.async;