babel/packages/babel-core/src/transform-file-sync.js
Nicolas Marien cf62908bbd Keep user options intact in transformFile (#6890)
* Preserve user options in transformFile

* Improve tests for transformFile user opts handling
2017-11-25 10:13:56 -08:00

23 lines
570 B
JavaScript

// @flow
import fs from "fs";
import loadConfig, { type InputOptions } from "./config";
import { runSync, type FileResult } from "./transformation";
export default function transformFileSync(
filename: string,
opts: ?InputOptions,
): FileResult | null {
let options;
if (opts == null) {
options = { filename };
} else if (opts && typeof opts === "object") {
options = Object.assign({}, opts, { filename });
}
const config = loadConfig(options);
if (config === null) return null;
return runSync(config, fs.readFileSync(filename, "utf8"));
}