Skip to content

Commit

Permalink
Merge pull request #345 from fivetanley/stitchfix-for-babel-compilati…
Browse files Browse the repository at this point in the history
…on-helpers

[bugfix] defensively copy `targets` so @babel/helper-compilation-targets can't mutate `targets`
  • Loading branch information
rwjblue authored May 30, 2020
2 parents d835fbb + 3dfc15b commit 38db30c
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 103 deletions.
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,14 @@ module.exports = {

let parser = require('@babel/helper-compilation-targets').default;
if (typeof targets === 'object' && targets !== null) {
return parser(targets);
// babel version 7.10.0 introduced a change that mutates the input:
// https://github.com/babel/babel/pull/11500
// copy the object to guard against it, otherwise subsequent calls to
// _getTargets() will only have a mutated copy and lose all config from `config/targets.js`
// in the host application.
// PR to fix this upstream in babel: https://github.com/babel/babel/pull/11648
const copy = clone(targets);
return parser(copy);
} else {
return targets;
}
Expand Down
11 changes: 11 additions & 0 deletions node-tests/addon-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const terminateWorkerPool = require('./utils/terminate-workers');
const path = require('path');
const fs = require('fs');
const rimraf = require('rimraf');
const clone = require('clone');

function prepareAddon(addon) {
addon.pkg.keywords.push('ember-addon');
Expand Down Expand Up @@ -1353,6 +1354,16 @@ describe('ember-cli-babel', function() {
let pluginRequired = this.addon.isPluginRequired('transform-regenerator');
expect(pluginRequired).to.be.false;
});

it('defensively copies `targets` to prevent @babel/helper-compilation-functions mutating it', function() {
let targets = {
browsers: ['last 2 Chrome versions']
};
this.addon.project.targets = clone(targets);

this.addon.isPluginRequired('transform-regenerator');
expect(this.addon.project.targets).to.deep.equal(targets);
});
});
});

Expand Down
Loading

0 comments on commit 38db30c

Please sign in to comment.