Keep user options intact in transformFile (#6890)
* Preserve user options in transformFile * Improve tests for transformFile user opts handling
This commit is contained in:
parent
66ee192a7f
commit
cf62908bbd
@ -8,13 +8,14 @@ export default function transformFileSync(
|
|||||||
filename: string,
|
filename: string,
|
||||||
opts: ?InputOptions,
|
opts: ?InputOptions,
|
||||||
): FileResult | null {
|
): FileResult | null {
|
||||||
|
let options;
|
||||||
if (opts == null) {
|
if (opts == null) {
|
||||||
opts = { filename };
|
options = { filename };
|
||||||
} else if (opts && typeof opts === "object") {
|
} else if (opts && typeof opts === "object") {
|
||||||
opts = Object.assign(opts, { filename });
|
options = Object.assign({}, opts, { filename });
|
||||||
}
|
}
|
||||||
|
|
||||||
const config = loadConfig(opts);
|
const config = loadConfig(options);
|
||||||
if (config === null) return null;
|
if (config === null) return null;
|
||||||
|
|
||||||
return runSync(config, fs.readFileSync(filename, "utf8"));
|
return runSync(config, fs.readFileSync(filename, "utf8"));
|
||||||
|
|||||||
@ -10,21 +10,22 @@ type TransformFile = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export default ((function transformFile(filename, opts, callback) {
|
export default ((function transformFile(filename, opts, callback) {
|
||||||
|
let options;
|
||||||
if (typeof opts === "function") {
|
if (typeof opts === "function") {
|
||||||
callback = opts;
|
callback = opts;
|
||||||
opts = undefined;
|
opts = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opts == null) {
|
if (opts == null) {
|
||||||
opts = { filename };
|
options = { filename };
|
||||||
} else if (opts && typeof opts === "object") {
|
} else if (opts && typeof opts === "object") {
|
||||||
opts = Object.assign(opts, { filename });
|
options = Object.assign({}, opts, { filename });
|
||||||
}
|
}
|
||||||
|
|
||||||
process.nextTick(() => {
|
process.nextTick(() => {
|
||||||
let cfg;
|
let cfg;
|
||||||
try {
|
try {
|
||||||
cfg = loadConfig(opts);
|
cfg = loadConfig(options);
|
||||||
if (cfg === null) return callback(null, null);
|
if (cfg === null) return callback(null, null);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|||||||
@ -118,26 +118,33 @@ describe("api", function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("transformFile", function(done) {
|
it("transformFile", function(done) {
|
||||||
babel.transformFile(
|
const options = {
|
||||||
__dirname + "/fixtures/api/file.js",
|
babelrc: false,
|
||||||
{
|
};
|
||||||
babelrc: false,
|
Object.freeze(options);
|
||||||
},
|
babel.transformFile(__dirname + "/fixtures/api/file.js", options, function(
|
||||||
function(err, res) {
|
err,
|
||||||
if (err) return done(err);
|
res,
|
||||||
assert.equal(res.code, "foo();");
|
) {
|
||||||
done();
|
if (err) return done(err);
|
||||||
},
|
assert.equal(res.code, "foo();");
|
||||||
);
|
// keep user options untouched
|
||||||
|
assert.deepEqual(options, { babelrc: false });
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("transformFileSync", function() {
|
it("transformFileSync", function() {
|
||||||
|
const options = {
|
||||||
|
babelrc: false,
|
||||||
|
};
|
||||||
|
Object.freeze(options);
|
||||||
assert.equal(
|
assert.equal(
|
||||||
babel.transformFileSync(__dirname + "/fixtures/api/file.js", {
|
babel.transformFileSync(__dirname + "/fixtures/api/file.js", options)
|
||||||
babelrc: false,
|
.code,
|
||||||
}).code,
|
|
||||||
"foo();",
|
"foo();",
|
||||||
);
|
);
|
||||||
|
assert.deepEqual(options, { babelrc: false });
|
||||||
});
|
});
|
||||||
|
|
||||||
it("options throw on falsy true", function() {
|
it("options throw on falsy true", function() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user