Skip to content

Commit 21ba1c7

Browse files
committed
allow for override settings for @babel/preset-env and regenerator fixes (#5216)
1 parent f9c1a76 commit 21ba1c7

File tree

2 files changed

+94
-46
lines changed

2 files changed

+94
-46
lines changed

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

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ const validateBoolOption = (name, value, defaultValue) => {
2020
return value;
2121
};
2222

23+
// @babel/preset-env provides good validations and error messages for bogus
24+
// options. Here we are simply falling back to default if the overrides are
25+
// falsey.
26+
const validatePresetEnvOptions = (options, defaultOptions) =>
27+
options || defaultOptions;
28+
2329
module.exports = function(api, opts, env) {
2430
if (!opts) {
2531
opts = {};
@@ -31,6 +37,11 @@ module.exports = function(api, opts, env) {
3137

3238
var isFlowEnabled = validateBoolOption('flow', opts.flow, true);
3339
var areHelpersEnabled = validateBoolOption('helpers', opts.helpers, true);
40+
var isRegeneratorEnabled = validateBoolOption(
41+
'regenerator',
42+
opts.regenerator,
43+
true
44+
);
3445
var useAbsoluteRuntime = validateBoolOption(
3546
'absoluteRuntime',
3647
opts.absoluteRuntime,
@@ -44,6 +55,13 @@ module.exports = function(api, opts, env) {
4455
);
4556
}
4657

58+
// We allow for separate preset overrides for production/development and test.
59+
var presetEnvOverrides = validatePresetEnvOptions(opts.presetEnv, undefined);
60+
var presetEnvTestOverrides = validatePresetEnvOptions(
61+
opts.presetEnvTest,
62+
undefined
63+
);
64+
4765
if (!isEnvDevelopment && !isEnvProduction && !isEnvTest) {
4866
throw new Error(
4967
'Using `babel-preset-react-app` requires that you specify `NODE_ENV` or ' +
@@ -59,32 +77,38 @@ module.exports = function(api, opts, env) {
5977
isEnvTest && [
6078
// ES features necessary for user's Node version
6179
require('@babel/preset-env').default,
62-
{
63-
targets: {
64-
node: 'current',
80+
Object.assign(
81+
{
82+
targets: {
83+
node: 'current',
84+
},
6585
},
66-
},
86+
presetEnvTestOverrides
87+
),
6788
],
6889
(isEnvProduction || isEnvDevelopment) && [
6990
// Latest stable ECMAScript features
7091
require('@babel/preset-env').default,
71-
{
72-
// We want Create React App to be IE 9 compatible until React itself
73-
// no longer works with IE 9
74-
targets: {
75-
ie: 9,
92+
Object.assign(
93+
{
94+
// We want Create React App to be IE 9 compatible until React itself
95+
// no longer works with IE 9
96+
targets: {
97+
ie: 9,
98+
},
99+
// Users cannot override this behavior because this Babel
100+
// configuration is highly tuned for ES5 support
101+
ignoreBrowserslistConfig: true,
102+
// If users import all core-js they're probably not concerned with
103+
// bundle size. We shouldn't rely on magic to try and shrink it.
104+
useBuiltIns: false,
105+
// Do not transform modules to CJS
106+
modules: false,
107+
// Exclude transforms that make all code slower
108+
exclude: ['transform-typeof-symbol'],
76109
},
77-
// Users cannot override this behavior because this Babel
78-
// configuration is highly tuned for ES5 support
79-
ignoreBrowserslistConfig: true,
80-
// If users import all core-js they're probably not concerned with
81-
// bundle size. We shouldn't rely on magic to try and shrink it.
82-
useBuiltIns: false,
83-
// Do not transform modules to CJS
84-
modules: false,
85-
// Exclude transforms that make all code slower
86-
exclude: ['transform-typeof-symbol'],
87-
},
110+
presetEnvOverrides
111+
),
88112
],
89113
[
90114
require('@babel/preset-react').default,
@@ -136,7 +160,7 @@ module.exports = function(api, opts, env) {
136160
{
137161
corejs: false,
138162
helpers: areHelpersEnabled,
139-
regenerator: true,
163+
regenerator: isRegeneratorEnabled,
140164
// https://babeljs.io/docs/en/babel-plugin-transform-runtime#useesmodules
141165
// We should turn this on once the lowest version of Node LTS
142166
// supports ES Modules.

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

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ const validateBoolOption = (name, value, defaultValue) => {
2020
return value;
2121
};
2222

23+
// @babel/preset-env provides good validations and error messages for bogus
24+
// options. Here we are simply falling back to default if the overrides are
25+
// falsey.
26+
const validatePresetEnvOptions = (options, defaultOptions) =>
27+
options || defaultOptions;
28+
2329
module.exports = function(api, opts) {
2430
if (!opts) {
2531
opts = {};
@@ -37,6 +43,11 @@ module.exports = function(api, opts) {
3743
var isEnvTest = env === 'test';
3844

3945
var areHelpersEnabled = validateBoolOption('helpers', opts.helpers, false);
46+
var isRegeneratorEnabled = validateBoolOption(
47+
'regenerator',
48+
opts.regenerator,
49+
true
50+
);
4051
var useAbsoluteRuntime = validateBoolOption(
4152
'absoluteRuntime',
4253
opts.absoluteRuntime,
@@ -50,6 +61,13 @@ module.exports = function(api, opts) {
5061
);
5162
}
5263

64+
// We allow for separate preset overrides for production/development and test.
65+
var presetEnvOverrides = validatePresetEnvOptions(opts.presetEnv, undefined);
66+
var presetEnvTestOverrides = validatePresetEnvOptions(
67+
opts.presetEnvTest,
68+
undefined
69+
);
70+
5371
if (!isEnvDevelopment && !isEnvProduction && !isEnvTest) {
5472
throw new Error(
5573
'Using `babel-preset-react-app` requires that you specify `NODE_ENV` or ' +
@@ -70,36 +88,42 @@ module.exports = function(api, opts) {
7088
isEnvTest && [
7189
// ES features necessary for user's Node version
7290
require('@babel/preset-env').default,
73-
{
74-
targets: {
75-
node: 'current',
91+
Object.assign(
92+
{
93+
targets: {
94+
node: 'current',
95+
},
96+
// Do not transform modules to CJS
97+
modules: false,
98+
// Exclude transforms that make all code slower
99+
exclude: ['transform-typeof-symbol'],
76100
},
77-
// Do not transform modules to CJS
78-
modules: false,
79-
// Exclude transforms that make all code slower
80-
exclude: ['transform-typeof-symbol'],
81-
},
101+
presetEnvTestOverrides
102+
),
82103
],
83104
(isEnvProduction || isEnvDevelopment) && [
84105
// Latest stable ECMAScript features
85106
require('@babel/preset-env').default,
86-
{
87-
// We want Create React App to be IE 9 compatible until React itself
88-
// no longer works with IE 9
89-
targets: {
90-
ie: 9,
107+
Object.assign(
108+
{
109+
// We want Create React App to be IE 9 compatible until React itself
110+
// no longer works with IE 9
111+
targets: {
112+
ie: 9,
113+
},
114+
// Users cannot override this behavior because this Babel
115+
// configuration is highly tuned for ES5 support
116+
ignoreBrowserslistConfig: true,
117+
// If users import all core-js they're probably not concerned with
118+
// bundle size. We shouldn't rely on magic to try and shrink it.
119+
useBuiltIns: false,
120+
// Do not transform modules to CJS
121+
modules: false,
122+
// Exclude transforms that make all code slower
123+
exclude: ['transform-typeof-symbol'],
91124
},
92-
// Users cannot override this behavior because this Babel
93-
// configuration is highly tuned for ES5 support
94-
ignoreBrowserslistConfig: true,
95-
// If users import all core-js they're probably not concerned with
96-
// bundle size. We shouldn't rely on magic to try and shrink it.
97-
useBuiltIns: false,
98-
// Do not transform modules to CJS
99-
modules: false,
100-
// Exclude transforms that make all code slower
101-
exclude: ['transform-typeof-symbol'],
102-
},
125+
presetEnvOverrides
126+
),
103127
],
104128
].filter(Boolean),
105129
plugins: [
@@ -110,7 +134,7 @@ module.exports = function(api, opts) {
110134
{
111135
corejs: false,
112136
helpers: areHelpersEnabled,
113-
regenerator: true,
137+
regenerator: isRegeneratorEnabled,
114138
// https://babeljs.io/docs/en/babel-plugin-transform-runtime#useesmodules
115139
// We should turn this on once the lowest version of Node LTS
116140
// supports ES Modules.

0 commit comments

Comments
 (0)