UNPKG

1.41 kBJavaScriptView Raw
1// @flow
2
3import gensync from "gensync";
4
5import loadConfig, { type InputOptions, type ResolvedConfig } from "./config";
6import {
7 run,
8 type FileResult,
9 type FileResultCallback,
10} from "./transformation";
11import * as fs from "./gensync-utils/fs";
12
13import typeof * as transformFileBrowserType from "./transform-file-browser";
14import typeof * as transformFileType from "./transform-file";
15
16// Kind of gross, but essentially asserting that the exports of this module are the same as the
17// exports of transform-file-browser, since this file may be replaced at bundle time with
18// transform-file-browser.
19((({}: any): $Exact<transformFileBrowserType>): $Exact<transformFileType>);
20
21type TransformFile = {
22 (filename: string, callback: FileResultCallback): void,
23 (filename: string, opts: ?InputOptions, callback: FileResultCallback): void,
24};
25
26const transformFileRunner = gensync<[string, ?InputOptions], FileResult | null>(
27 function* (filename, opts) {
28 const options = { ...opts, filename };
29
30 const config: ResolvedConfig | null = yield* loadConfig(options);
31 if (config === null) return null;
32
33 const code = yield* fs.readFile(filename, "utf8");
34 return yield* run(config, code);
35 },
36);
37
38export const transformFile: TransformFile = transformFileRunner.errback;
39export const transformFileSync = transformFileRunner.sync;
40export const transformFileAsync = transformFileRunner.async;