Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed reset of BABEL_ENV when options.forceEnv is used #420

Merged
merged 1 commit into from
Mar 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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();
});
});