Skip to content

Commit 6a83763

Browse files
shubhekshajarlef
authored andcommitted
Disable ES2015 transforms based on node version using babel-preset-env (facebook#878)
* Disable ES2015 transforms based on node version using babel-preset-env * pass major version number for node to babel-preset-env instead of version string * use parseFloat() instead of parseInt() to parse node version * fixed style nits
1 parent 669b5ac commit 6a83763

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

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

+41-21
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,7 @@
1010

1111
var path = require('path');
1212

13-
module.exports = {
14-
presets: [
15-
// Latest stable ECMAScript features
16-
require.resolve('babel-preset-latest'),
17-
// JSX, Flow
18-
require.resolve('babel-preset-react')
19-
],
20-
plugins: [
13+
const plugins = [
2114
// class { handleClick = () => { } }
2215
require.resolve('babel-plugin-transform-class-properties'),
2316
// { ...todo, completed: true }
@@ -35,8 +28,7 @@ module.exports = {
3528
// Resolve the Babel runtime relative to the config.
3629
moduleName: path.dirname(require.resolve('babel-runtime/package'))
3730
}]
38-
]
39-
};
31+
];
4032

4133
// This is similar to how `env` works in Babel:
4234
// https://babeljs.io/docs/usage/babelrc/#env-option
@@ -52,7 +44,7 @@ if (env !== 'development' && env !== 'test' && env !== 'production') {
5244
'"test", and "production". Instead, received: ' + JSON.stringify(env) + '.'
5345
);
5446
}
55-
var plugins = module.exports.plugins;
47+
5648
if (env === 'development' || env === 'test') {
5749
plugins.push.apply(plugins, [
5850
// Adds component stack to warning messages
@@ -61,14 +53,42 @@ if (env === 'development' || env === 'test') {
6153
require.resolve('babel-plugin-transform-react-jsx-self')
6254
]);
6355
}
64-
if (env === 'production') {
65-
// Optimization: hoist JSX that never changes out of render()
66-
// Disabled because of issues:
67-
// * https://github.com/facebookincubator/create-react-app/issues/525
68-
// * https://phabricator.babeljs.io/search/query/pCNlnC2xzwzx/
69-
// * https://github.com/babel/babel/issues/4516
70-
// TODO: Enable again when these issues are resolved.
71-
// plugins.push.apply(plugins, [
72-
// require.resolve('babel-plugin-transform-react-constant-elements')
73-
// ]);
56+
57+
if (env === 'test') {
58+
module.exports = {
59+
presets: [
60+
// ES features necessary for user's Node version
61+
[require('babel-preset-env').default, {
62+
targets: {
63+
node: parseFloat(process.versions.node),
64+
},
65+
}],
66+
// JSX, Flow
67+
require.resolve('babel-preset-react')
68+
],
69+
plugins: plugins
70+
};
71+
} else {
72+
module.exports = {
73+
presets: [
74+
// Latest stable ECMAScript features
75+
require.resolve('babel-preset-latest'),
76+
// JSX, Flow
77+
require.resolve('babel-preset-react')
78+
],
79+
plugins: plugins
80+
};
81+
82+
if (env === 'production') {
83+
// Optimization: hoist JSX that never changes out of render()
84+
// Disabled because of issues:
85+
// * https://github.com/facebookincubator/create-react-app/issues/525
86+
// * https://phabricator.babeljs.io/search/query/pCNlnC2xzwzx/
87+
// * https://github.com/babel/babel/issues/4516
88+
// TODO: Enable again when these issues are resolved.
89+
// plugins.push.apply(plugins, [
90+
// require.resolve('babel-plugin-transform-react-constant-elements')
91+
// ]);
92+
}
7493
}
94+

packages/babel-preset-react-app/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"babel-plugin-transform-react-jsx-source": "6.9.0",
1919
"babel-plugin-transform-regenerator": "6.16.1",
2020
"babel-plugin-transform-runtime": "6.15.0",
21+
"babel-preset-env": "0.0.4",
2122
"babel-preset-latest": "6.14.0",
2223
"babel-preset-react": "6.11.1",
2324
"babel-runtime": "6.11.6"

0 commit comments

Comments
 (0)