1 | import gensync from "gensync";
|
---|
2 |
|
---|
3 | import loadConfig from "./config";
|
---|
4 | import type { InputOptions, ResolvedConfig } from "./config";
|
---|
5 | import { run } from "./transformation";
|
---|
6 | import type { FileResult, FileResultCallback } from "./transformation";
|
---|
7 | import * as fs from "./gensync-utils/fs";
|
---|
8 |
|
---|
9 | type transformFileBrowserType = typeof import("./transform-file-browser");
|
---|
10 | type 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 |
|
---|
17 | type TransformFile = {
|
---|
18 | (filename: string, callback: FileResultCallback): void;
|
---|
19 | (
|
---|
20 | filename: string,
|
---|
21 | opts: InputOptions | undefined | null,
|
---|
22 | callback: FileResultCallback,
|
---|
23 | ): void;
|
---|
24 | };
|
---|
25 |
|
---|
26 | const 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 |
|
---|
38 | export const transformFile = transformFileRunner.errback as TransformFile;
|
---|
39 | export const transformFileSync = transformFileRunner.sync;
|
---|
40 | export const transformFileAsync = transformFileRunner.async;
|
---|