Skip to content

Commit

Permalink
Copy rather than mutating .babelrc configuration objects.
Browse files Browse the repository at this point in the history
Helps with #9469, since any extraneous "env" property will be removed from
the copied object.
  • Loading branch information
Ben Newman committed Dec 20, 2017
1 parent f1c3c0a commit 29140f8
Showing 1 changed file with 50 additions and 34 deletions.
84 changes: 50 additions & 34 deletions packages/babel-compiler/babel-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,25 +222,40 @@ BCp._inferHelper = function (

function walkBabelRC(obj, path) {
if (obj && typeof obj === "object") {
const copy = Object.create(null);

path = path || [];
var index = path.push("presets") - 1;
walkHelper(obj.presets, path);
path[index] = "plugins";
walkHelper(obj.plugins, path);
const index = path.length;

if (obj.presets) {
path[index] = "presets";
copy.presets = walkHelper(obj.presets, path);
}

if (obj.plugins) {
path[index] = "plugins";
copy.plugins = walkHelper(obj.plugins, path);
}

path.pop();

return copy;
}

return obj;
}

function walkHelper(list, path) {
if (list) {
// Empty the list and then refill it with resolved values.
list.splice(0).forEach(function (pluginOrPreset) {
var res = resolveHelper(pluginOrPreset, path);
if (res) {
list.push(res);
}
});
}
const copy = [];

list.forEach(function (pluginOrPreset) {
const res = resolveHelper(pluginOrPreset, path);
if (res) {
copy.push(res);
}
});

return copy;
}

function resolveHelper(value, path) {
Expand All @@ -252,24 +267,24 @@ BCp._inferHelper = function (

if (Array.isArray(value)) {
// The value is a [plugin, options] pair.
var res = value[0] = resolveHelper(value[0], path);
const res = resolveHelper(value[0], path);
if (res) {
return value;
const copy = value.slice(0);
copy[0] = res;
return copy;
}

} else if (typeof value === "string") {
// The value is a string that we need to require.
var result = requireWithPath(value, path);
const result = requireWithPath(value, path);
if (result && result.module) {
cacheDeps[result.name] = result.version;
walkBabelRC(result.module, path);
return result.module;
return walkBabelRC(result.module, path);
}

} else if (typeof value === "object") {
// The value is a { presets?, plugins? } preset object.
walkBabelRC(value, path);
return value;
return walkBabelRC(value, path);
}
}

Expand Down Expand Up @@ -310,25 +325,26 @@ BCp._inferHelper = function (
}
}

babelrc = JSON.parse(JSON.stringify(babelrc));
const clean = walkBabelRC(babelrc);
merge(babelOptions, clean, "presets");
merge(babelOptions, clean, "plugins");

walkBabelRC(babelrc);
if (babelrc && babelrc.env) {
const envKey =
process.env.BABEL_ENV ||
process.env.NODE_ENV ||
"development";

merge(babelOptions, babelrc, "presets");
merge(babelOptions, babelrc, "plugins");
const clean = walkBabelRC(babelrc.env[envKey]);

const babelEnv = (process.env.BABEL_ENV ||
process.env.NODE_ENV ||
"development");
if (babelrc && babelrc.env && babelrc.env[babelEnv]) {
const env = babelrc.env[babelEnv];
walkBabelRC(env);
merge(babelOptions, env, "presets");
merge(babelOptions, env, "plugins");
if (clean) {
merge(babelOptions, clean, "presets");
merge(babelOptions, clean, "plugins");
}
}

return !! (babelrc.presets ||
babelrc.plugins);
return !! (babelOptions.presets ||
babelOptions.plugins);
};

function merge(babelOptions, babelrc, name) {
Expand Down

0 comments on commit 29140f8

Please sign in to comment.