Skip to content

Commit

Permalink
Merge branch 'master' into 7.0
Browse files Browse the repository at this point in the history
# Conflicts:
#	package.json
#	test/loader.test.js
  • Loading branch information
danez committed Mar 16, 2017
2 parents 87e4e85 + e20f6b6 commit 1ed7ff5
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v6.4.1

### 🐛 Bug Fix

- Fixed reset of BABEL_ENV when options.forceEnv is used (#420) @nikopavlica

## v6.4.0

### 🚀 New Feature
Expand Down
12 changes: 10 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,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 @@ -72,7 +72,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 @@ -81,6 +81,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
120 changes: 118 additions & 2 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,42 @@ test.cb("should use correct env", (t) => {
});
});

test.cb("should not throw without config", (t) => {
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: {
Expand All @@ -128,6 +163,52 @@ test.cb("should not throw without config", (t) => {
{
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/,
},
],
Expand All @@ -137,8 +218,43 @@ test.cb("should not throw without config", (t) => {
webpack(config, (err, stats) => {
t.is(err, null);

t.true(stats.compilation.errors.length === 0);
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();
});
});

test.cb("should not throw without config", (t) => {
const config = {
entry: path.join(__dirname, "fixtures/basic.js"),
output: {
path: t.context.directory,
},
module: {
loaders: [
{
test: /\.jsx?/,
loader: babelLoader,
exclude: /node_modules/,
},
],
},
};

webpack(config, (err, stats) => {
t.is(err, null);
t.true(stats.compilation.errors.length === 0);
t.end();
});
});

0 comments on commit 1ed7ff5

Please sign in to comment.