Skip to content

Commit adfb20c

Browse files
authored
Turn on Babel helpers (facebook#5093)
* Turn on helpers and test importing something with async/await works * Compiling babel runtime breaks itself * Add helpers option to babel plugin with defaults * Make helpers off by default and on in our configuration * Hit eject and e2e * meh * copy'n'paste * change again * Turn off helpers by default in /prod, /dev, /test * oops * Spread undefined * Use object assign not object spread * Put preset in template since it's needed * Fix e2e tests
1 parent 5599eff commit adfb20c

File tree

9 files changed

+35
-10
lines changed

9 files changed

+35
-10
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module.exports = function(api, opts, env) {
2727
var isEnvProduction = env === 'production';
2828
var isEnvTest = env === 'test';
2929
var isFlowEnabled = validateBoolOption('flow', opts.flow, true);
30+
var areHelpersEnabled = validateBoolOption('helpers', opts.helpers, true);
3031

3132
if (!isEnvDevelopment && !isEnvProduction && !isEnvTest) {
3233
throw new Error(
@@ -113,7 +114,7 @@ module.exports = function(api, opts, env) {
113114
require('@babel/plugin-transform-runtime').default,
114115
{
115116
corejs: false,
116-
helpers: false,
117+
helpers: areHelpersEnabled,
117118
regenerator: true,
118119
// https://babeljs.io/docs/en/babel-plugin-transform-runtime#useesmodules
119120
// We should turn this on once the lowest version of Node LTS

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@
66
*/
77
'use strict';
88

9+
const validateBoolOption = (name, value, defaultValue) => {
10+
if (typeof value === 'undefined') {
11+
value = defaultValue;
12+
}
13+
14+
if (typeof value !== 'boolean') {
15+
throw new Error(`Preset react-app: '${name}' option must be a boolean.`);
16+
}
17+
18+
return value;
19+
};
20+
921
module.exports = function(api, opts) {
1022
if (!opts) {
1123
opts = {};
@@ -21,6 +33,7 @@ module.exports = function(api, opts) {
2133
var isEnvDevelopment = env === 'development';
2234
var isEnvProduction = env === 'production';
2335
var isEnvTest = env === 'test';
36+
var areHelpersEnabled = validateBoolOption('helpers', opts.helpers, false);
2437
if (!isEnvDevelopment && !isEnvProduction && !isEnvTest) {
2538
throw new Error(
2639
'Using `babel-preset-react-app` requires that you specify `NODE_ENV` or ' +
@@ -76,7 +89,7 @@ module.exports = function(api, opts) {
7689
require('@babel/plugin-transform-runtime').default,
7790
{
7891
corejs: false,
79-
helpers: false,
92+
helpers: areHelpersEnabled,
8093
regenerator: true,
8194
// https://babeljs.io/docs/en/babel-plugin-transform-runtime#useesmodules
8295
// We should turn this on once the lowest version of Node LTS

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
const create = require('./create');
1010

1111
module.exports = function(api, opts) {
12-
return create(api, opts, 'development');
12+
return create(api, Object.assign({ helpers: false }, opts), 'development');
1313
};

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
const create = require('./create');
1010

1111
module.exports = function(api, opts) {
12-
return create(api, opts, 'production');
12+
return create(api, Object.assign({ helpers: false }, opts), 'production');
1313
};

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
const create = require('./create');
1010

1111
module.exports = function(api, opts) {
12-
return create(api, opts, 'test');
12+
return create(api, Object.assign({ helpers: false }, opts), 'test');
1313
};

packages/react-error-overlay/webpack.config.iframe.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,15 @@ module.exports = {
3232
// Dependencies
3333
{
3434
test: /\.js$/,
35+
exclude: /@babel\/runtime/,
3536
use: {
3637
loader: 'babel-loader',
3738
options: {
3839
babelrc: false,
3940
compact: false,
40-
presets: ['babel-preset-react-app/dependencies'],
41+
presets: [
42+
['babel-preset-react-app/dependencies', { helpers: true }],
43+
],
4144
},
4245
},
4346
},

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ module.exports = {
275275
// Unlike the application JS, we only compile the standard ES features.
276276
{
277277
test: /\.js$/,
278+
exclude: /@babel\/runtime/,
278279
use: [
279280
// This loader parallelizes code compilation, it is optional but
280281
// improves compile time on larger projects
@@ -290,7 +291,10 @@ module.exports = {
290291
babelrc: false,
291292
compact: false,
292293
presets: [
293-
require.resolve('babel-preset-react-app/dependencies'),
294+
[
295+
require.resolve('babel-preset-react-app/dependencies'),
296+
{ helpers: true },
297+
],
294298
],
295299
cacheDirectory: true,
296300
// Don't waste time on Gzipping the cache

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ module.exports = {
314314
// Unlike the application JS, we only compile the standard ES features.
315315
{
316316
test: /\.js$/,
317+
exclude: /@babel\/runtime/,
317318
use: [
318319
// This loader parallelizes code compilation, it is optional but
319320
// improves compile time on larger projects
@@ -324,7 +325,10 @@ module.exports = {
324325
babelrc: false,
325326
compact: false,
326327
presets: [
327-
require.resolve('babel-preset-react-app/dependencies'),
328+
[
329+
require.resolve('babel-preset-react-app/dependencies'),
330+
{ helpers: true },
331+
],
328332
],
329333
cacheDirectory: true,
330334
// Save disk space when time isn't as important

tasks/e2e-kitchensink.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ PORT=3001 \
146146
nohup yarn start &>$tmp_server_log &
147147
grep -q 'You can now view' <(tail -f $tmp_server_log)
148148

149-
# Before running Mocha, specify that it should use our preset
150-
# TODO: this is very hacky and we should find some other solution
149+
# We haven't ejected yet which means there's no `babel` key
150+
# in package.json yet
151151
echo '{"presets":["react-app"]}' > .babelrc
152152

153153
# Test "development" environment

0 commit comments

Comments
 (0)