Skip to content

Commit 1ed7ff5

Browse files
committed
Merge branch 'master' into 7.0
# Conflicts: # package.json # test/loader.test.js
2 parents 87e4e85 + e20f6b6 commit 1ed7ff5

File tree

3 files changed

+134
-4
lines changed

3 files changed

+134
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## v6.4.1
4+
5+
### 🐛 Bug Fix
6+
7+
- Fixed reset of BABEL_ENV when options.forceEnv is used (#420) @nikopavlica
8+
39
## v6.4.0
410

511
### 🚀 New Feature

src/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const transpile = function(source, options) {
4545
try {
4646
result = babel.transform(source, options);
4747
} catch (error) {
48-
if (forceEnv) process.env.BABEL_ENV = tmpEnv;
48+
if (forceEnv) restoreBabelEnv(tmpEnv);
4949
if (error.message && error.codeFrame) {
5050
let message = error.message;
5151
let name;
@@ -72,7 +72,7 @@ const transpile = function(source, options) {
7272
map.sourcesContent = [source];
7373
}
7474

75-
if (forceEnv) process.env.BABEL_ENV = tmpEnv;
75+
if (forceEnv) restoreBabelEnv(tmpEnv);
7676

7777
return {
7878
code: code,
@@ -81,6 +81,14 @@ const transpile = function(source, options) {
8181
};
8282
};
8383

84+
function restoreBabelEnv(prevValue) {
85+
if (prevValue === undefined) {
86+
delete process.env.BABEL_ENV;
87+
} else {
88+
process.env.BABEL_ENV = prevValue;
89+
}
90+
}
91+
8492
function passMetadata(s, context, metadata) {
8593
if (context[s]) {
8694
context[s](metadata);

test/loader.test.js

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,42 @@ test.cb("should use correct env", (t) => {
117117
});
118118
});
119119

120-
test.cb("should not throw without config", (t) => {
120+
test.serial.cb("should not polute BABEL_ENV after using forceEnv", (t) => {
121+
const initialBabelEnv = process.env.BABEL_ENV;
122+
123+
const config = {
124+
entry: path.join(__dirname, "fixtures/basic.js"),
125+
output: {
126+
path: t.context.directory,
127+
},
128+
module: {
129+
loaders: [
130+
{
131+
test: /\.jsx?/,
132+
loader: babelLoader,
133+
query: {
134+
forceEnv: "testenv",
135+
env: {
136+
testenv: {
137+
presets: ["es2015"],
138+
},
139+
}
140+
},
141+
exclude: /node_modules/,
142+
},
143+
],
144+
},
145+
};
146+
147+
webpack(config, () => {
148+
t.truthy(process.env.BABEL_ENV === initialBabelEnv);
149+
t.end();
150+
});
151+
});
152+
153+
test.serial.cb("should not polute BABEL_ENV after using forceEnv (on exception)", (t) => {
154+
const initialBabelEnv = process.env.BABEL_ENV;
155+
121156
const config = {
122157
entry: path.join(__dirname, "fixtures/basic.js"),
123158
output: {
@@ -128,6 +163,52 @@ test.cb("should not throw without config", (t) => {
128163
{
129164
test: /\.jsx?/,
130165
loader: babelLoader,
166+
query: {
167+
forceEnv: "testenv",
168+
env: {
169+
testenv: {
170+
presets: ["es2015asd"],
171+
},
172+
}
173+
},
174+
exclude: /node_modules/,
175+
},
176+
],
177+
},
178+
};
179+
180+
webpack(config, () => {
181+
t.truthy(process.env.BABEL_ENV === initialBabelEnv);
182+
t.end();
183+
});
184+
});
185+
186+
test.serial.cb("should not change BABEL_ENV when using forceEnv", (t) => {
187+
const initialBabelEnv = process.env.BABEL_ENV;
188+
189+
process.env.BABEL_ENV = "nontestenv";
190+
191+
const config = {
192+
entry: path.join(__dirname, "fixtures/basic.js"),
193+
output: {
194+
path: t.context.directory,
195+
},
196+
module: {
197+
loaders: [
198+
{
199+
test: /\.jsx?/,
200+
loader: babelLoader,
201+
query: {
202+
forceEnv: "testenv",
203+
env: {
204+
testenv: {
205+
presets: ["es2015abc"],
206+
},
207+
nontestenv: {
208+
presets: ["es2015xzy"]
209+
}
210+
}
211+
},
131212
exclude: /node_modules/,
132213
},
133214
],
@@ -137,8 +218,43 @@ test.cb("should not throw without config", (t) => {
137218
webpack(config, (err, stats) => {
138219
t.is(err, null);
139220

140-
t.true(stats.compilation.errors.length === 0);
221+
t.true(stats.compilation.errors.length === 1);
222+
223+
t.truthy(stats.compilation.errors[0].message.match(/es2015abc/));
224+
t.falsy(stats.compilation.errors[0].message.match(/es2015xyz/));
225+
226+
t.truthy("nontestenv" === process.env.BABEL_ENV);
227+
228+
if (initialBabelEnv !== undefined) {
229+
process.env.BABEL_ENV = initialBabelEnv;
230+
} else {
231+
delete process.env.BABEL_ENV;
232+
}
141233

142234
t.end();
143235
});
144236
});
237+
238+
test.cb("should not throw without config", (t) => {
239+
const config = {
240+
entry: path.join(__dirname, "fixtures/basic.js"),
241+
output: {
242+
path: t.context.directory,
243+
},
244+
module: {
245+
loaders: [
246+
{
247+
test: /\.jsx?/,
248+
loader: babelLoader,
249+
exclude: /node_modules/,
250+
},
251+
],
252+
},
253+
};
254+
255+
webpack(config, (err, stats) => {
256+
t.is(err, null);
257+
t.true(stats.compilation.errors.length === 0);
258+
t.end();
259+
});
260+
});

0 commit comments

Comments
 (0)