Skip to content

Commit

Permalink
Apply forceEnv option to Babel transformation (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvasz authored and danez committed Feb 15, 2017
1 parent 08ef070 commit fdf0184
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/index.js
Expand Up @@ -78,6 +78,7 @@ module.exports = function(source, inputSourceMap) {
const globalOptions = this.options.babel || {};
const loaderOptions = loaderUtils.parseQuery(this.query);
const userOptions = assign({}, globalOptions, loaderOptions);
const env = userOptions.forceEnv || process.env.BABEL_ENV || process.env.NODE_ENV;
const defaultOptions = {
inputSourceMap: inputSourceMap,
sourceRoot: process.cwd(),
Expand All @@ -88,7 +89,7 @@ module.exports = function(source, inputSourceMap) {
babelrc: exists(userOptions.babelrc) ?
read(userOptions.babelrc) :
resolveRc(path.dirname(filename)),
env: userOptions.forceEnv || process.env.BABEL_ENV || process.env.NODE_ENV,
env: env,
}),
};

Expand Down Expand Up @@ -129,6 +130,9 @@ module.exports = function(source, inputSourceMap) {
});
}

const tmpEnv = process.env.BABEL_ENV;
process.env.BABEL_ENV = env;
result = transpile(source, options);
process.env.BABEL_ENV = tmpEnv;
this.callback(null, result.code, result.map);
};
40 changes: 40 additions & 0 deletions test/loader.test.js
Expand Up @@ -77,3 +77,43 @@ test.cb("should not throw error on syntax error", (t) => {
t.end();
});
});

test.cb("should use correct env", (t) => {
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"],
},
otherenv: {
presets: ["es2015xyz"],
}
}
},
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.end();
});
});

0 comments on commit fdf0184

Please sign in to comment.