Skip to content

Commit b08b400

Browse files
authored
Compile dependencies with babel-preset-env (#3776)
1 parent 6c233ea commit b08b400

File tree

4 files changed

+87
-3
lines changed

4 files changed

+87
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
'use strict';
8+
9+
module.exports = function(api, opts) {
10+
if (!opts) {
11+
opts = {};
12+
}
13+
14+
// This is similar to how `env` works in Babel:
15+
// https://babeljs.io/docs/usage/babelrc/#env-option
16+
// We are not using `env` because it’s ignored in versions > babel-core@6.10.4:
17+
// https://github.com/babel/babel/issues/4539
18+
// https://github.com/facebookincubator/create-react-app/issues/720
19+
// It’s also nice that we can enforce `NODE_ENV` being specified.
20+
var env = process.env.BABEL_ENV || process.env.NODE_ENV;
21+
var isEnvDevelopment = env === 'development';
22+
var isEnvProduction = env === 'production';
23+
var isEnvTest = env === 'test';
24+
if (!isEnvDevelopment && !isEnvProduction && !isEnvTest) {
25+
throw new Error(
26+
'Using `babel-preset-react-app` requires that you specify `NODE_ENV` or ' +
27+
'`BABEL_ENV` environment variables. Valid values are "development", ' +
28+
'"test", and "production". Instead, received: ' +
29+
JSON.stringify(env) +
30+
'.'
31+
);
32+
}
33+
34+
return {
35+
presets: [
36+
isEnvTest && [
37+
// ES features necessary for user's Node version
38+
require('@babel/preset-env').default,
39+
{
40+
targets: {
41+
node: 'current',
42+
},
43+
// Do not transform modules to CJS
44+
modules: false,
45+
},
46+
],
47+
(isEnvProduction || isEnvDevelopment) && [
48+
// Latest stable ECMAScript features
49+
require('@babel/preset-env').default,
50+
{
51+
// Do not transform modules to CJS
52+
modules: false,
53+
},
54+
],
55+
].filter(Boolean),
56+
};
57+
};

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"url": "https://github.com/facebookincubator/create-react-app/issues"
99
},
1010
"files": [
11-
"index.js"
11+
"index.js",
12+
"dependencies.js"
1213
],
1314
"dependencies": {
1415
"@babel/core": "7.0.0-beta.36",

packages/react-scripts/config/webpack.config.dev.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ module.exports = {
159159
name: 'static/media/[name].[hash:8].[ext]',
160160
},
161161
},
162-
// Process JS with Babel.
162+
// Process application JS with Babel.
163+
// The preset includes JSX, Flow, and some ESnext features.
163164
{
164165
test: /\.(js|jsx|mjs)$/,
165166
include: paths.appSrc,
@@ -175,6 +176,18 @@ module.exports = {
175176
cacheDirectory: true,
176177
},
177178
},
179+
// Process any JS outside of the app with Babel.
180+
// Unlike the application JS, we only compile the standard ES features.
181+
{
182+
test: /\.js$/,
183+
loader: require.resolve('babel-loader'),
184+
options: {
185+
babelrc: false,
186+
compact: false,
187+
presets: [require.resolve('babel-preset-react-app/dependencies')],
188+
cacheDirectory: true,
189+
},
190+
},
178191
// "postcss" loader applies autoprefixer to our CSS.
179192
// "css" loader resolves paths in CSS and adds assets as dependencies.
180193
// "style" loader turns CSS into JS modules that inject <style> tags.

packages/react-scripts/config/webpack.config.prod.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ module.exports = {
167167
name: 'static/media/[name].[hash:8].[ext]',
168168
},
169169
},
170-
// Process JS with Babel.
170+
// Process application JS with Babel.
171+
// The preset includes JSX, Flow, and some ESnext features.
171172
{
172173
test: /\.(js|jsx|mjs)$/,
173174
include: paths.appSrc,
@@ -180,6 +181,18 @@ module.exports = {
180181
compact: true,
181182
},
182183
},
184+
// Process any JS outside of the app with Babel.
185+
// Unlike the application JS, we only compile the standard ES features.
186+
{
187+
test: /\.js$/,
188+
loader: require.resolve('babel-loader'),
189+
options: {
190+
babelrc: false,
191+
compact: false,
192+
presets: [require.resolve('babel-preset-react-app/dependencies')],
193+
cacheDirectory: true,
194+
},
195+
},
183196
// The notation here is somewhat confusing.
184197
// "postcss" loader applies autoprefixer to our CSS.
185198
// "css" loader resolves paths in CSS and adds assets as dependencies.

0 commit comments

Comments
 (0)