Skip to content

Commit c4a936e

Browse files
authored
Add JSX source transform for better warnings (facebook#716)
Fixes facebook#700. Read about it here: https://twitter.com/dan_abramov/status/779308833399332864.
1 parent 7c978e5 commit c4a936e

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

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

+36-14
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,40 @@ module.exports = {
3535
// Resolve the Babel runtime relative to the config.
3636
moduleName: path.dirname(require.resolve('babel-runtime/package'))
3737
}]
38-
],
39-
env: {
40-
production: {
41-
plugins: [
42-
// Optimization: hoist JSX that never changes out of render()
43-
// Disabled because of issues:
44-
// * https://github.com/facebookincubator/create-react-app/issues/525
45-
// * https://phabricator.babeljs.io/search/query/pCNlnC2xzwzx/
46-
// * https://github.com/babel/babel/issues/4516
47-
// TODO: Enable again when these issues are resolved.
48-
// require.resolve('babel-plugin-transform-react-constant-elements')
49-
]
50-
}
51-
}
38+
]
5239
};
40+
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 > babel-core@6.10.4:
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: ' + JSON.stringify(env) + '.'
53+
);
54+
}
55+
var plugins = module.exports.plugins;
56+
if (env === 'development' || env === 'test') {
57+
plugins.push.apply(plugins, [
58+
// Adds component stack to warning messages
59+
require.resolve('babel-plugin-transform-react-jsx-source'),
60+
// Adds __self attribute to JSX which React will use for some warnings
61+
require.resolve('babel-plugin-transform-react-jsx-self')
62+
]);
63+
}
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+
// ]);
74+
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
"babel-plugin-transform-class-properties": "6.11.5",
1515
"babel-plugin-transform-object-rest-spread": "6.8.0",
1616
"babel-plugin-transform-react-constant-elements": "6.9.1",
17+
"babel-plugin-transform-react-jsx-self": "6.11.0",
18+
"babel-plugin-transform-react-jsx-source": "6.9.0",
1719
"babel-plugin-transform-regenerator": "6.14.0",
1820
"babel-plugin-transform-runtime": "6.15.0",
1921
"babel-preset-latest": "6.14.0",

0 commit comments

Comments
 (0)