diff --git a/packages/babel-core/src/transform-file-sync.js b/packages/babel-core/src/transform-file-sync.js index 496c26f888..28b89e05c9 100644 --- a/packages/babel-core/src/transform-file-sync.js +++ b/packages/babel-core/src/transform-file-sync.js @@ -8,13 +8,14 @@ export default function transformFileSync( filename: string, opts: ?InputOptions, ): FileResult | null { + let options; if (opts == null) { - opts = { filename }; + options = { filename }; } 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; return runSync(config, fs.readFileSync(filename, "utf8")); diff --git a/packages/babel-core/src/transform-file.js b/packages/babel-core/src/transform-file.js index 0c884f2918..e73b855c0c 100644 --- a/packages/babel-core/src/transform-file.js +++ b/packages/babel-core/src/transform-file.js @@ -10,21 +10,22 @@ type TransformFile = { }; export default ((function transformFile(filename, opts, callback) { + let options; if (typeof opts === "function") { callback = opts; opts = undefined; } if (opts == null) { - opts = { filename }; + options = { filename }; } else if (opts && typeof opts === "object") { - opts = Object.assign(opts, { filename }); + options = Object.assign({}, opts, { filename }); } process.nextTick(() => { let cfg; try { - cfg = loadConfig(opts); + cfg = loadConfig(options); if (cfg === null) return callback(null, null); } catch (err) { return callback(err); diff --git a/packages/babel-core/test/api.js b/packages/babel-core/test/api.js index 215a8960f1..dfbe5a8504 100644 --- a/packages/babel-core/test/api.js +++ b/packages/babel-core/test/api.js @@ -118,26 +118,33 @@ describe("api", function() { }); it("transformFile", function(done) { - babel.transformFile( - __dirname + "/fixtures/api/file.js", - { - babelrc: false, - }, - function(err, res) { - if (err) return done(err); - assert.equal(res.code, "foo();"); - done(); - }, - ); + const options = { + babelrc: false, + }; + Object.freeze(options); + babel.transformFile(__dirname + "/fixtures/api/file.js", options, function( + err, + res, + ) { + if (err) return done(err); + assert.equal(res.code, "foo();"); + // keep user options untouched + assert.deepEqual(options, { babelrc: false }); + done(); + }); }); it("transformFileSync", function() { + const options = { + babelrc: false, + }; + Object.freeze(options); assert.equal( - babel.transformFileSync(__dirname + "/fixtures/api/file.js", { - babelrc: false, - }).code, + babel.transformFileSync(__dirname + "/fixtures/api/file.js", options) + .code, "foo();", ); + assert.deepEqual(options, { babelrc: false }); }); it("options throw on falsy true", function() {