/
transform-file.js
40 lines (31 loc) 路 1.37 KB
/
transform-file.js
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
// @flow
import gensync from "gensync";
import loadConfig, { type InputOptions, type ResolvedConfig } from "./config";
import {
run,
type FileResult,
type FileResultCallback,
} from "./transformation";
import * as fs from "./gensync-utils/fs";
import typeof * as transformFileBrowserType from "./transform-file-browser";
import typeof * as transformFileType from "./transform-file";
// Kind of gross, but essentially asserting that the exports of this module are the same as the
// exports of transform-file-browser, since this file may be replaced at bundle time with
// transform-file-browser.
((({}: any): $Exact<transformFileBrowserType>): $Exact<transformFileType>);
type TransformFile = {
(filename: string, callback: FileResultCallback): void,
(filename: string, opts: ?InputOptions, callback: FileResultCallback): void,
};
const transformFileRunner = gensync<[string, ?InputOptions], FileResult | null>(
function* (filename, opts) {
const options = { ...opts, filename };
const config: ResolvedConfig | null = yield* loadConfig(options);
if (config === null) return null;
const code = yield* fs.readFile(filename, "utf8");
return yield* run(config, code);
},
);
export const transformFile: TransformFile = transformFileRunner.errback;
export const transformFileSync = transformFileRunner.sync;
export const transformFileAsync = transformFileRunner.async;