Skip to content

Commit

Permalink
Disable ES2015 transforms based on node version using babel-preset-env (
Browse files Browse the repository at this point in the history
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
  • Loading branch information
shubheksha authored and feiqitian committed Oct 25, 2016
1 parent 8e99095 commit acd5518
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
62 changes: 41 additions & 21 deletions packages/babel-preset-react-app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@

var path = require('path');

module.exports = {
presets: [
// Latest stable ECMAScript features
require.resolve('babel-preset-latest'),
// JSX, Flow
require.resolve('babel-preset-react')
],
plugins: [
const plugins = [
// class { handleClick = () => { } }
require.resolve('babel-plugin-transform-class-properties'),
// { ...todo, completed: true }
Expand All @@ -35,8 +28,7 @@ module.exports = {
// Resolve the Babel runtime relative to the config.
moduleName: path.dirname(require.resolve('babel-runtime/package'))
}]
]
};
];

// This is similar to how `env` works in Babel:
// https://babeljs.io/docs/usage/babelrc/#env-option
Expand All @@ -52,7 +44,7 @@ if (env !== 'development' && env !== 'test' && env !== 'production') {
'"test", and "production". Instead, received: ' + JSON.stringify(env) + '.'
);
}
var plugins = module.exports.plugins;

if (env === 'development' || env === 'test') {
plugins.push.apply(plugins, [
// Adds component stack to warning messages
Expand All @@ -61,14 +53,42 @@ if (env === 'development' || env === 'test') {
require.resolve('babel-plugin-transform-react-jsx-self')
]);
}
if (env === 'production') {
// Optimization: hoist JSX that never changes out of render()
// Disabled because of issues:
// * https://github.com/facebookincubator/create-react-app/issues/525
// * https://phabricator.babeljs.io/search/query/pCNlnC2xzwzx/
// * https://github.com/babel/babel/issues/4516
// TODO: Enable again when these issues are resolved.
// plugins.push.apply(plugins, [
// require.resolve('babel-plugin-transform-react-constant-elements')
// ]);

if (env === 'test') {
module.exports = {
presets: [
// ES features necessary for user's Node version
[require('babel-preset-env').default, {
targets: {
node: parseFloat(process.versions.node),
},
}],
// JSX, Flow
require.resolve('babel-preset-react')
],
plugins: plugins
};
} else {
module.exports = {
presets: [
// Latest stable ECMAScript features
require.resolve('babel-preset-latest'),
// JSX, Flow
require.resolve('babel-preset-react')
],
plugins: plugins
};

if (env === 'production') {
// Optimization: hoist JSX that never changes out of render()
// Disabled because of issues:
// * https://github.com/facebookincubator/create-react-app/issues/525
// * https://phabricator.babeljs.io/search/query/pCNlnC2xzwzx/
// * https://github.com/babel/babel/issues/4516
// TODO: Enable again when these issues are resolved.
// plugins.push.apply(plugins, [
// require.resolve('babel-plugin-transform-react-constant-elements')
// ]);
}
}

1 change: 1 addition & 0 deletions packages/babel-preset-react-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"babel-plugin-transform-react-jsx-source": "6.9.0",
"babel-plugin-transform-regenerator": "6.16.1",
"babel-plugin-transform-runtime": "6.15.0",
"babel-preset-env": "0.0.4",
"babel-preset-latest": "6.14.0",
"babel-preset-react": "6.11.1",
"babel-runtime": "6.11.6"
Expand Down

0 comments on commit acd5518

Please sign in to comment.