Skip to content

Commit 98da747

Browse files
committed
Add env option to babel-preset-react-app
1 parent 5e300ce commit 98da747

File tree

1 file changed

+96
-90
lines changed

1 file changed

+96
-90
lines changed

packages/babel-preset-react-app/index.js

Lines changed: 96 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -38,101 +38,107 @@ const plugins = [
3838
],
3939
];
4040

41-
// This is similar to how `env` works in Babel:
42-
// https://babeljs.io/docs/usage/babelrc/#env-option
43-
// We are not using `env` because it’s ignored in versions > [email protected]:
44-
// https://github.com/babel/babel/issues/4539
45-
// https://github.com/facebookincubator/create-react-app/issues/720
46-
// It’s also nice that we can enforce `NODE_ENV` being specified.
47-
var env = process.env.BABEL_ENV || process.env.NODE_ENV;
48-
if (env !== 'development' && env !== 'test' && env !== 'production') {
49-
throw new Error(
50-
'Using `babel-preset-react-app` requires that you specify `NODE_ENV` or ' +
51-
'`BABEL_ENV` environment variables. Valid values are "development", ' +
52-
'"test", and "production". Instead, received: ' +
53-
JSON.stringify(env) +
54-
'.'
55-
);
56-
}
41+
module.exports = (context, opts = {}) => {
42+
// This is similar to how `env` works in Babel:
43+
// https://babeljs.io/docs/usage/babelrc/#env-option
44+
// We are not using `env` because it’s ignored in versions > [email protected]:
45+
// https://github.com/babel/babel/issues/4539
46+
// https://github.com/facebookincubator/create-react-app/issues/720
47+
// It’s also nice that we can enforce `NODE_ENV` being specified.
48+
var env =
49+
opts.environment ||
50+
opts.env ||
51+
process.env.BABEL_ENV ||
52+
process.env.NODE_ENV;
53+
if (env !== 'development' && env !== 'test' && env !== 'production') {
54+
throw new Error(
55+
'Using `babel-preset-react-app` requires that you specify `NODE_ENV` or ' +
56+
'`BABEL_ENV` environment variables. Valid values are "development", ' +
57+
'"test", and "production". Instead, received: ' +
58+
JSON.stringify(env) +
59+
'.'
60+
);
61+
}
5762

58-
if (env === 'development' || env === 'test') {
59-
// The following two plugins are currently necessary to make React warnings
60-
// include more valuable information. They are included here because they are
61-
// currently not enabled in babel-preset-react. See the below threads for more info:
62-
// https://github.com/babel/babel/issues/4702
63-
// https://github.com/babel/babel/pull/3540#issuecomment-228673661
64-
// https://github.com/facebookincubator/create-react-app/issues/989
65-
plugins.push.apply(plugins, [
66-
// Adds component stack to warning messages
67-
require.resolve('babel-plugin-transform-react-jsx-source'),
68-
// Adds __self attribute to JSX which React will use for some warnings
69-
require.resolve('babel-plugin-transform-react-jsx-self'),
70-
]);
71-
}
63+
if (env === 'development' || env === 'test') {
64+
// The following two plugins are currently necessary to make React warnings
65+
// include more valuable information. They are included here because they are
66+
// currently not enabled in babel-preset-react. See the below threads for more info:
67+
// https://github.com/babel/babel/issues/4702
68+
// https://github.com/babel/babel/pull/3540#issuecomment-228673661
69+
// https://github.com/facebookincubator/create-react-app/issues/989
70+
plugins.push.apply(plugins, [
71+
// Adds component stack to warning messages
72+
require.resolve('babel-plugin-transform-react-jsx-source'),
73+
// Adds __self attribute to JSX which React will use for some warnings
74+
require.resolve('babel-plugin-transform-react-jsx-self'),
75+
]);
76+
}
7277

73-
if (env === 'test') {
74-
module.exports = {
75-
presets: [
76-
// ES features necessary for user's Node version
77-
[
78-
require('babel-preset-env').default,
79-
{
80-
targets: {
81-
node: 'current',
78+
if (env === 'test') {
79+
return {
80+
presets: [
81+
// ES features necessary for user's Node version
82+
[
83+
require('babel-preset-env').default,
84+
{
85+
targets: {
86+
node: 'current',
87+
},
8288
},
83-
},
89+
],
90+
// JSX, Flow
91+
require.resolve('babel-preset-react'),
8492
],
85-
// JSX, Flow
86-
require.resolve('babel-preset-react'),
87-
],
88-
plugins: plugins.concat([
89-
// Compiles import() to a deferred require()
90-
require.resolve('babel-plugin-dynamic-import-node'),
91-
]),
92-
};
93-
} else {
94-
module.exports = {
95-
presets: [
96-
// Latest stable ECMAScript features
97-
[
98-
require.resolve('babel-preset-env'),
99-
{
100-
targets: {
101-
// React parses on ie 9, so we should too
102-
ie: 9,
103-
// We currently minify with uglify
104-
// Remove after https://github.com/mishoo/UglifyJS2/issues/448
105-
uglify: true,
93+
plugins: plugins.concat([
94+
// Compiles import() to a deferred require()
95+
require.resolve('babel-plugin-dynamic-import-node'),
96+
]),
97+
};
98+
} else {
99+
return {
100+
presets: [
101+
// Latest stable ECMAScript features
102+
[
103+
require.resolve('babel-preset-env'),
104+
{
105+
targets: {
106+
// React parses on ie 9, so we should too
107+
ie: 9,
108+
// We currently minify with uglify
109+
// Remove after https://github.com/mishoo/UglifyJS2/issues/448
110+
uglify: true,
111+
},
112+
// Disable polyfill transforms
113+
useBuiltIns: false,
114+
// Do not transform modules to CJS
115+
modules: false,
106116
},
107-
// Disable polyfill transforms
108-
useBuiltIns: false,
109-
// Do not transform modules to CJS
110-
modules: false,
111-
},
117+
],
118+
// JSX, Flow
119+
require.resolve('babel-preset-react'),
112120
],
113-
// JSX, Flow
114-
require.resolve('babel-preset-react'),
115-
],
116-
plugins: plugins.concat([
117-
// function* () { yield 42; yield 43; }
118-
[
119-
require.resolve('babel-plugin-transform-regenerator'),
120-
{
121-
// Async functions are converted to generators by babel-preset-env
122-
async: false,
123-
},
124-
],
125-
// Adds syntax support for import()
126-
require.resolve('babel-plugin-syntax-dynamic-import'),
127-
]),
128-
};
121+
plugins: plugins.concat([
122+
// function* () { yield 42; yield 43; }
123+
[
124+
require.resolve('babel-plugin-transform-regenerator'),
125+
{
126+
// Async functions are converted to generators by babel-preset-env
127+
async: false,
128+
},
129+
],
130+
// Adds syntax support for import()
131+
require.resolve('babel-plugin-syntax-dynamic-import'),
132+
]),
133+
};
129134

130-
if (env === 'production') {
131-
// Optimization: hoist JSX that never changes out of render()
132-
// Disabled because of issues: https://github.com/facebookincubator/create-react-app/issues/553
133-
// TODO: Enable again when these issues are resolved.
134-
// plugins.push.apply(plugins, [
135-
// require.resolve('babel-plugin-transform-react-constant-elements')
136-
// ]);
135+
if (env === 'production') {
136+
// Optimization: hoist JSX that never changes out of render()
137+
// Disabled because of issues: https://github.com/facebookincubator/create-react-app/issues/553
138+
// TODO: Enable again when these issues are resolved.
139+
// plugins.push.apply(plugins, [
140+
// require.resolve('babel-plugin-transform-react-constant-elements')
141+
// ]);
142+
}
137143
}
138-
}
144+
};

0 commit comments

Comments
 (0)