Skip to content

Commit f3241f8

Browse files
nikopavlicadanez
authored andcommitted
Fixed reset of BABEL_ENV when options.forceEnv is used (#420)
1 parent 36db87b commit f3241f8

File tree

2 files changed

+128
-2
lines changed

2 files changed

+128
-2
lines changed

src/index.js

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

76-
if (forceEnv) process.env.BABEL_ENV = tmpEnv;
76+
if (forceEnv) restoreBabelEnv(tmpEnv);
7777

7878
return {
7979
code: code,
@@ -82,6 +82,14 @@ const transpile = function(source, options) {
8282
};
8383
};
8484

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

test/loader.test.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,121 @@ test.cb("should use correct env", (t) => {
117117
t.end();
118118
});
119119
});
120+
121+
test.serial.cb("should not polute BABEL_ENV after using forceEnv", (t) => {
122+
const initialBabelEnv = process.env.BABEL_ENV;
123+
124+
const config = {
125+
entry: path.join(__dirname, "fixtures/basic.js"),
126+
output: {
127+
path: t.context.directory,
128+
},
129+
module: {
130+
loaders: [
131+
{
132+
test: /\.jsx?/,
133+
loader: babelLoader,
134+
query: {
135+
forceEnv: "testenv",
136+
env: {
137+
testenv: {
138+
presets: ["es2015"],
139+
},
140+
}
141+
},
142+
exclude: /node_modules/,
143+
},
144+
],
145+
},
146+
};
147+
148+
webpack(config, () => {
149+
t.truthy(process.env.BABEL_ENV === initialBabelEnv);
150+
t.end();
151+
});
152+
});
153+
154+
test.serial.cb("should not polute BABEL_ENV after using forceEnv (on exception)", (t) => {
155+
const initialBabelEnv = process.env.BABEL_ENV;
156+
157+
const config = {
158+
entry: path.join(__dirname, "fixtures/basic.js"),
159+
output: {
160+
path: t.context.directory,
161+
},
162+
module: {
163+
loaders: [
164+
{
165+
test: /\.jsx?/,
166+
loader: babelLoader,
167+
query: {
168+
forceEnv: "testenv",
169+
env: {
170+
testenv: {
171+
presets: ["es2015asd"],
172+
},
173+
}
174+
},
175+
exclude: /node_modules/,
176+
},
177+
],
178+
},
179+
};
180+
181+
webpack(config, () => {
182+
t.truthy(process.env.BABEL_ENV === initialBabelEnv);
183+
t.end();
184+
});
185+
});
186+
187+
test.serial.cb("should not change BABEL_ENV when using forceEnv", (t) => {
188+
const initialBabelEnv = process.env.BABEL_ENV;
189+
190+
process.env.BABEL_ENV = "nontestenv";
191+
192+
const config = {
193+
entry: path.join(__dirname, "fixtures/basic.js"),
194+
output: {
195+
path: t.context.directory,
196+
},
197+
module: {
198+
loaders: [
199+
{
200+
test: /\.jsx?/,
201+
loader: babelLoader,
202+
query: {
203+
forceEnv: "testenv",
204+
env: {
205+
testenv: {
206+
presets: ["es2015abc"],
207+
},
208+
nontestenv: {
209+
presets: ["es2015xzy"]
210+
}
211+
}
212+
},
213+
exclude: /node_modules/,
214+
},
215+
],
216+
},
217+
};
218+
219+
webpack(config, (err, stats) => {
220+
t.is(err, null);
221+
222+
t.true(stats.compilation.errors.length === 1);
223+
224+
t.truthy(stats.compilation.errors[0].message.match(/es2015abc/));
225+
t.falsy(stats.compilation.errors[0].message.match(/es2015xyz/));
226+
227+
t.truthy("nontestenv" === process.env.BABEL_ENV);
228+
229+
if (initialBabelEnv !== undefined) {
230+
process.env.BABEL_ENV = initialBabelEnv;
231+
} else {
232+
delete process.env.BABEL_ENV;
233+
}
234+
235+
t.end();
236+
});
237+
});

0 commit comments

Comments
 (0)