Skip to content

Commit

Permalink
allow for override settings for @babel/preset-env and regenerator fix…
Browse files Browse the repository at this point in the history
  • Loading branch information
heygrady committed Oct 6, 2018
1 parent f9c1a76 commit 21ba1c7
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 46 deletions.
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

0 comments on commit 21ba1c7

Please sign in to comment.