Skip to content

Commit 38db30c

Browse files
author
Robert Jackson
authored
Merge pull request #345 from fivetanley/stitchfix-for-babel-compilation-helpers
[bugfix] defensively copy `targets` so @babel/helper-compilation-targets can't mutate `targets`
2 parents d835fbb + 3dfc15b commit 38db30c

File tree

3 files changed

+230
-103
lines changed

3 files changed

+230
-103
lines changed

index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,14 @@ module.exports = {
542542

543543
let parser = require('@babel/helper-compilation-targets').default;
544544
if (typeof targets === 'object' && targets !== null) {
545-
return parser(targets);
545+
// babel version 7.10.0 introduced a change that mutates the input:
546+
// https://github.com/babel/babel/pull/11500
547+
// copy the object to guard against it, otherwise subsequent calls to
548+
// _getTargets() will only have a mutated copy and lose all config from `config/targets.js`
549+
// in the host application.
550+
// PR to fix this upstream in babel: https://github.com/babel/babel/pull/11648
551+
const copy = clone(targets);
552+
return parser(copy);
546553
} else {
547554
return targets;
548555
}

node-tests/addon-test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const terminateWorkerPool = require('./utils/terminate-workers');
1818
const path = require('path');
1919
const fs = require('fs');
2020
const rimraf = require('rimraf');
21+
const clone = require('clone');
2122

2223
function prepareAddon(addon) {
2324
addon.pkg.keywords.push('ember-addon');
@@ -1353,6 +1354,16 @@ describe('ember-cli-babel', function() {
13531354
let pluginRequired = this.addon.isPluginRequired('transform-regenerator');
13541355
expect(pluginRequired).to.be.false;
13551356
});
1357+
1358+
it('defensively copies `targets` to prevent @babel/helper-compilation-functions mutating it', function() {
1359+
let targets = {
1360+
browsers: ['last 2 Chrome versions']
1361+
};
1362+
this.addon.project.targets = clone(targets);
1363+
1364+
this.addon.isPluginRequired('transform-regenerator');
1365+
expect(this.addon.project.targets).to.deep.equal(targets);
1366+
});
13561367
});
13571368
});
13581369

0 commit comments

Comments
 (0)