| 1 |
import gensync, { type Handler } 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 |
const 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 |
| 31 |
export function transformFile( |
| 32 |
filename: string, |
| 33 |
callback: FileResultCallback, |
| 34 |
): void; |
| 35 |
export function transformFile( |
| 36 |
filename: string, |
| 37 |
opts: InputOptions | undefined | null, |
| 38 |
callback: FileResultCallback, |
| 39 |
): void; |
| 40 |
export function transformFile( |
| 41 |
...args: Parameters<typeof transformFileRunner.errback> |
| 42 |
) { |
| 43 |
return transformFileRunner.errback(...args); |
| 44 |
} |
| 45 |
|
| 46 |
export function transformFileSync( |
| 47 |
...args: Parameters<typeof transformFileRunner.sync> |
| 48 |
) { |
| 49 |
return transformFileRunner.sync(...args); |
| 50 |
} |
| 51 |
export function transformFileAsync( |
| 52 |
...args: Parameters<typeof transformFileRunner.async> |
| 53 |
) { |
| 54 |
return transformFileRunner.async(...args); |
| 55 |
} |