Skip to content

Commit

Permalink
Fixed reset of BABEL_ENV when options.forceEnv is used (#420)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikopavlica authored and danez committed Mar 15, 2017
1 parent 36db87b commit f3241f8
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/index.js
Expand Up @@ -46,7 +46,7 @@ const transpile = function(source, options) {
try {
result = babel.transform(source, options);
} catch (error) {
if (forceEnv) process.env.BABEL_ENV = tmpEnv;
if (forceEnv) restoreBabelEnv(tmpEnv);
if (error.message && error.codeFrame) {
let message = error.message;
let name;
Expand All @@ -73,7 +73,7 @@ const transpile = function(source, options) {
map.sourcesContent = [source];
}

if (forceEnv) process.env.BABEL_ENV = tmpEnv;
if (forceEnv) restoreBabelEnv(tmpEnv);

return {
code: code,
Expand All @@ -82,6 +82,14 @@ const transpile = function(source, options) {
};
};

function restoreBabelEnv(prevValue) {
if (prevValue === undefined) {
delete process.env.BABEL_ENV;
} else {
process.env.BABEL_ENV = prevValue;
}
}

function passMetadata(s, context, metadata) {
if (context[s]) {
context[s](metadata);
Expand Down
118 changes: 118 additions & 0 deletions test/loader.test.js
Expand Up @@ -117,3 +117,121 @@ test.cb("should use correct env", (t) => {
t.end();
});
});

test.serial.cb("should not polute BABEL_ENV after using forceEnv", (t) => {
const initialBabelEnv = process.env.BABEL_ENV;

const config = {
entry: path.join(__dirname, "fixtures/basic.js"),
output: {
path: t.context.directory,
},
module: {
loaders: [
{
test: /\.jsx?/,
loader: babelLoader,
query: {
forceEnv: "testenv",
env: {
testenv: {
presets: ["es2015"],
},
}
},
exclude: /node_modules/,
},
],
},
};

webpack(config, () => {
t.truthy(process.env.BABEL_ENV === initialBabelEnv);
t.end();
});
});

test.serial.cb("should not polute BABEL_ENV after using forceEnv (on exception)", (t) => {
const initialBabelEnv = process.env.BABEL_ENV;

const config = {
entry: path.join(__dirname, "fixtures/basic.js"),
output: {
path: t.context.directory,
},
module: {
loaders: [
{
test: /\.jsx?/,
loader: babelLoader,
query: {
forceEnv: "testenv",
env: {
testenv: {
presets: ["es2015asd"],
},
}
},
exclude: /node_modules/,
},
],
},
};

webpack(config, () => {
t.truthy(process.env.BABEL_ENV === initialBabelEnv);
t.end();
});
});

test.serial.cb("should not change BABEL_ENV when using forceEnv", (t) => {
const initialBabelEnv = process.env.BABEL_ENV;

process.env.BABEL_ENV = "nontestenv";

const config = {
entry: path.join(__dirname, "fixtures/basic.js"),
output: {
path: t.context.directory,
},
module: {
loaders: [
{
test: /\.jsx?/,
loader: babelLoader,
query: {
forceEnv: "testenv",
env: {
testenv: {
presets: ["es2015abc"],
},
nontestenv: {
presets: ["es2015xzy"]
}
}
},
exclude: /node_modules/,
},
],
},
};

webpack(config, (err, stats) => {
t.is(err, null);

t.true(stats.compilation.errors.length === 1);

t.truthy(stats.compilation.errors[0].message.match(/es2015abc/));
t.falsy(stats.compilation.errors[0].message.match(/es2015xyz/));

t.truthy("nontestenv" === process.env.BABEL_ENV);

if (initialBabelEnv !== undefined) {
process.env.BABEL_ENV = initialBabelEnv;
} else {
delete process.env.BABEL_ENV;
}

t.end();
});
});

0 comments on commit f3241f8

Please sign in to comment.