Skip to content

override settings for @babel/preset-env and regenerator. #5264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 45 additions & 21 deletions packages/babel-preset-react-app/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ const validateBoolOption = (name, value, defaultValue) => {
return value;
};

// @babel/preset-env provides good validations and error messages for bogus
// options. Here we are simply falling back to default if the overrides are
// falsey.
const validatePresetEnvOptions = (options, defaultOptions) =>
options || defaultOptions;

module.exports = function(api, opts, env) {
if (!opts) {
opts = {};
Expand All @@ -31,6 +37,11 @@ module.exports = function(api, opts, env) {

var isFlowEnabled = validateBoolOption('flow', opts.flow, true);
var areHelpersEnabled = validateBoolOption('helpers', opts.helpers, true);
var isRegeneratorEnabled = validateBoolOption(
'regenerator',
opts.regenerator,
true
);
var useAbsoluteRuntime = validateBoolOption(
'absoluteRuntime',
opts.absoluteRuntime,
Expand All @@ -44,6 +55,13 @@ module.exports = function(api, opts, env) {
);
}

// We allow for separate preset overrides for production/development and test.
var presetEnvOverrides = validatePresetEnvOptions(opts.presetEnv, undefined);
var presetEnvTestOverrides = validatePresetEnvOptions(
opts.presetEnvTest,
undefined
);

if (!isEnvDevelopment && !isEnvProduction && !isEnvTest) {
throw new Error(
'Using `babel-preset-react-app` requires that you specify `NODE_ENV` or ' +
Expand All @@ -59,32 +77,38 @@ module.exports = function(api, opts, env) {
isEnvTest && [
// ES features necessary for user's Node version
require('@babel/preset-env').default,
{
targets: {
node: 'current',
Object.assign(
{
targets: {
node: 'current',
},
},
},
presetEnvTestOverrides
),
],
(isEnvProduction || isEnvDevelopment) && [
// Latest stable ECMAScript features
require('@babel/preset-env').default,
{
// We want Create React App to be IE 9 compatible until React itself
// no longer works with IE 9
targets: {
ie: 9,
Object.assign(
{
// We want Create React App to be IE 9 compatible until React itself
// no longer works with IE 9
targets: {
ie: 9,
},
// Users cannot override this behavior because this Babel
// configuration is highly tuned for ES5 support
ignoreBrowserslistConfig: true,
// If users import all core-js they're probably not concerned with
// bundle size. We shouldn't rely on magic to try and shrink it.
useBuiltIns: false,
// Do not transform modules to CJS
modules: false,
// Exclude transforms that make all code slower
exclude: ['transform-typeof-symbol'],
},
// Users cannot override this behavior because this Babel
// configuration is highly tuned for ES5 support
ignoreBrowserslistConfig: true,
// If users import all core-js they're probably not concerned with
// bundle size. We shouldn't rely on magic to try and shrink it.
useBuiltIns: false,
// Do not transform modules to CJS
modules: false,
// Exclude transforms that make all code slower
exclude: ['transform-typeof-symbol'],
},
presetEnvOverrides
),
],
[
require('@babel/preset-react').default,
Expand Down Expand Up @@ -136,7 +160,7 @@ module.exports = function(api, opts, env) {
{
corejs: false,
helpers: areHelpersEnabled,
regenerator: true,
regenerator: isRegeneratorEnabled,
// https://babeljs.io/docs/en/babel-plugin-transform-runtime#useesmodules
// We should turn this on once the lowest version of Node LTS
// supports ES Modules.
Expand Down
74 changes: 49 additions & 25 deletions packages/babel-preset-react-app/dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ const validateBoolOption = (name, value, defaultValue) => {
return value;
};

// @babel/preset-env provides good validations and error messages for bogus
// options. Here we are simply falling back to default if the overrides are
// falsey.
const validatePresetEnvOptions = (options, defaultOptions) =>
options || defaultOptions;

module.exports = function(api, opts) {
if (!opts) {
opts = {};
Expand All @@ -37,6 +43,11 @@ module.exports = function(api, opts) {
var isEnvTest = env === 'test';

var areHelpersEnabled = validateBoolOption('helpers', opts.helpers, false);
var isRegeneratorEnabled = validateBoolOption(
'regenerator',
opts.regenerator,
true
);
var useAbsoluteRuntime = validateBoolOption(
'absoluteRuntime',
opts.absoluteRuntime,
Expand All @@ -50,6 +61,13 @@ module.exports = function(api, opts) {
);
}

// We allow for separate preset overrides for production/development and test.
var presetEnvOverrides = validatePresetEnvOptions(opts.presetEnv, undefined);
var presetEnvTestOverrides = validatePresetEnvOptions(
opts.presetEnvTest,
undefined
);

if (!isEnvDevelopment && !isEnvProduction && !isEnvTest) {
throw new Error(
'Using `babel-preset-react-app` requires that you specify `NODE_ENV` or ' +
Expand All @@ -70,36 +88,42 @@ module.exports = function(api, opts) {
isEnvTest && [
// ES features necessary for user's Node version
require('@babel/preset-env').default,
{
targets: {
node: 'current',
Object.assign(
{
targets: {
node: 'current',
},
// Do not transform modules to CJS
modules: false,
// Exclude transforms that make all code slower
exclude: ['transform-typeof-symbol'],
},
// Do not transform modules to CJS
modules: false,
// Exclude transforms that make all code slower
exclude: ['transform-typeof-symbol'],
},
presetEnvTestOverrides
),
],
(isEnvProduction || isEnvDevelopment) && [
// Latest stable ECMAScript features
require('@babel/preset-env').default,
{
// We want Create React App to be IE 9 compatible until React itself
// no longer works with IE 9
targets: {
ie: 9,
Object.assign(
{
// We want Create React App to be IE 9 compatible until React itself
// no longer works with IE 9
targets: {
ie: 9,
},
// Users cannot override this behavior because this Babel
// configuration is highly tuned for ES5 support
ignoreBrowserslistConfig: true,
// If users import all core-js they're probably not concerned with
// bundle size. We shouldn't rely on magic to try and shrink it.
useBuiltIns: false,
// Do not transform modules to CJS
modules: false,
// Exclude transforms that make all code slower
exclude: ['transform-typeof-symbol'],
},
// Users cannot override this behavior because this Babel
// configuration is highly tuned for ES5 support
ignoreBrowserslistConfig: true,
// If users import all core-js they're probably not concerned with
// bundle size. We shouldn't rely on magic to try and shrink it.
useBuiltIns: false,
// Do not transform modules to CJS
modules: false,
// Exclude transforms that make all code slower
exclude: ['transform-typeof-symbol'],
},
presetEnvOverrides
),
],
].filter(Boolean),
plugins: [
Expand All @@ -110,7 +134,7 @@ module.exports = function(api, opts) {
{
corejs: false,
helpers: areHelpersEnabled,
regenerator: true,
regenerator: isRegeneratorEnabled,
// https://babeljs.io/docs/en/babel-plugin-transform-runtime#useesmodules
// We should turn this on once the lowest version of Node LTS
// supports ES Modules.
Expand Down