Skip to content

Commit 1407287

Browse files
authored
Add mode entry points to our preset (facebook#4669)
1 parent 26febc5 commit 1407287

File tree

6 files changed

+183
-127
lines changed

6 files changed

+183
-127
lines changed
+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
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+
21+
module.exports = function(api, opts, env) {
22+
if (!opts) {
23+
opts = {};
24+
}
25+
26+
var isEnvDevelopment = env === 'development';
27+
var isEnvProduction = env === 'production';
28+
var isEnvTest = env === 'test';
29+
var isFlowEnabled = validateBoolOption('flow', opts.flow, true);
30+
31+
if (!isEnvDevelopment && !isEnvProduction && !isEnvTest) {
32+
throw new Error(
33+
'Using `babel-preset-react-app` requires that you specify `NODE_ENV` or ' +
34+
'`BABEL_ENV` environment variables. Valid values are "development", ' +
35+
'"test", and "production". Instead, received: ' +
36+
JSON.stringify(env) +
37+
'.'
38+
);
39+
}
40+
41+
return {
42+
presets: [
43+
isEnvTest && [
44+
// ES features necessary for user's Node version
45+
require('@babel/preset-env').default,
46+
{
47+
targets: {
48+
node: '6.12',
49+
},
50+
},
51+
],
52+
(isEnvProduction || isEnvDevelopment) && [
53+
// Latest stable ECMAScript features
54+
require('@babel/preset-env').default,
55+
{
56+
// `entry` transforms `@babel/polyfill` into individual requires for
57+
// the targeted browsers. This is safer than `usage` which performs
58+
// static code analysis to determine what's required.
59+
// This is probably a fine default to help trim down bundles when
60+
// end-users inevitably import '@babel/polyfill'.
61+
useBuiltIns: 'entry',
62+
// Do not transform modules to CJS
63+
modules: false,
64+
},
65+
],
66+
[
67+
require('@babel/preset-react').default,
68+
{
69+
// Adds component stack to warning messages
70+
// Adds __self attribute to JSX which React will use for some warnings
71+
development: isEnvDevelopment || isEnvTest,
72+
// Will use the native built-in instead of trying to polyfill
73+
// behavior for any plugins that require one.
74+
useBuiltIns: true,
75+
},
76+
],
77+
isFlowEnabled && [require('@babel/preset-flow').default],
78+
].filter(Boolean),
79+
plugins: [
80+
// Experimental macros support. Will be documented after it's had some time
81+
// in the wild.
82+
require('babel-plugin-macros'),
83+
// Necessary to include regardless of the environment because
84+
// in practice some other transforms (such as object-rest-spread)
85+
// don't work without it: https://github.com/babel/babel/issues/7215
86+
require('@babel/plugin-transform-destructuring').default,
87+
// class { handleClick = () => { } }
88+
// Enable loose mode to use assignment instead of defineProperty
89+
// See discussion in https://github.com/facebook/create-react-app/issues/4263
90+
[
91+
require('@babel/plugin-proposal-class-properties').default,
92+
{
93+
loose: true,
94+
},
95+
],
96+
// The following two plugins use Object.assign directly, instead of Babel's
97+
// extends helper. Note that this assumes `Object.assign` is available.
98+
// { ...todo, completed: true }
99+
[
100+
require('@babel/plugin-proposal-object-rest-spread').default,
101+
{
102+
useBuiltIns: true,
103+
},
104+
],
105+
// Polyfills the runtime needed for async/await and generators
106+
[
107+
require('@babel/plugin-transform-runtime').default,
108+
{
109+
helpers: false,
110+
polyfill: false,
111+
regenerator: true,
112+
},
113+
],
114+
isEnvProduction && [
115+
// Remove PropTypes from production build
116+
require('babel-plugin-transform-react-remove-prop-types').default,
117+
{
118+
removeImport: true,
119+
},
120+
],
121+
// function* () { yield 42; yield 43; }
122+
!isEnvTest && [
123+
require('@babel/plugin-transform-regenerator').default,
124+
{
125+
// Async functions are converted to generators by @babel/preset-env
126+
async: false,
127+
},
128+
],
129+
// Adds syntax support for import()
130+
require('@babel/plugin-syntax-dynamic-import').default,
131+
isEnvTest &&
132+
// Transform dynamic import to require
133+
require('babel-plugin-transform-dynamic-import').default,
134+
].filter(Boolean),
135+
};
136+
};
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
const create = require('./create');
10+
11+
module.exports = function(api, opts) {
12+
return create(api, opts, 'development');
13+
};

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

+3-126
Original file line numberDiff line numberDiff line change
@@ -6,138 +6,15 @@
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-
};
9+
const create = require('./create');
2010

2111
module.exports = function(api, opts) {
22-
if (!opts) {
23-
opts = {};
24-
}
25-
2612
// This is similar to how `env` works in Babel:
2713
// https://babeljs.io/docs/usage/babelrc/#env-option
2814
// We are not using `env` because it’s ignored in versions > babel-core@6.10.4:
2915
// https://github.com/babel/babel/issues/4539
3016
// https://github.com/facebook/create-react-app/issues/720
3117
// It’s also nice that we can enforce `NODE_ENV` being specified.
32-
var env = process.env.BABEL_ENV || process.env.NODE_ENV;
33-
var isEnvDevelopment = env === 'development';
34-
var isEnvProduction = env === 'production';
35-
var isEnvTest = env === 'test';
36-
var isFlowEnabled = validateBoolOption('flow', opts.flow, true);
37-
38-
if (!isEnvDevelopment && !isEnvProduction && !isEnvTest) {
39-
throw new Error(
40-
'Using `babel-preset-react-app` requires that you specify `NODE_ENV` or ' +
41-
'`BABEL_ENV` environment variables. Valid values are "development", ' +
42-
'"test", and "production". Instead, received: ' +
43-
JSON.stringify(env) +
44-
'.'
45-
);
46-
}
47-
48-
return {
49-
presets: [
50-
isEnvTest && [
51-
// ES features necessary for user's Node version
52-
require('@babel/preset-env').default,
53-
{
54-
targets: {
55-
node: '6.12',
56-
},
57-
},
58-
],
59-
(isEnvProduction || isEnvDevelopment) && [
60-
// Latest stable ECMAScript features
61-
require('@babel/preset-env').default,
62-
{
63-
// `entry` transforms `@babel/polyfill` into individual requires for
64-
// the targeted browsers. This is safer than `usage` which performs
65-
// static code analysis to determine what's required.
66-
// This is probably a fine default to help trim down bundles when
67-
// end-users inevitably import '@babel/polyfill'.
68-
useBuiltIns: 'entry',
69-
// Do not transform modules to CJS
70-
modules: false,
71-
},
72-
],
73-
[
74-
require('@babel/preset-react').default,
75-
{
76-
// Adds component stack to warning messages
77-
// Adds __self attribute to JSX which React will use for some warnings
78-
development: isEnvDevelopment || isEnvTest,
79-
// Will use the native built-in instead of trying to polyfill
80-
// behavior for any plugins that require one.
81-
useBuiltIns: true,
82-
},
83-
],
84-
isFlowEnabled && [require('@babel/preset-flow').default],
85-
].filter(Boolean),
86-
plugins: [
87-
// Experimental macros support. Will be documented after it's had some time
88-
// in the wild.
89-
require('babel-plugin-macros'),
90-
// Necessary to include regardless of the environment because
91-
// in practice some other transforms (such as object-rest-spread)
92-
// don't work without it: https://github.com/babel/babel/issues/7215
93-
require('@babel/plugin-transform-destructuring').default,
94-
// class { handleClick = () => { } }
95-
// Enable loose mode to use assignment instead of defineProperty
96-
// See discussion in https://github.com/facebook/create-react-app/issues/4263
97-
[
98-
require('@babel/plugin-proposal-class-properties').default,
99-
{
100-
loose: true,
101-
},
102-
],
103-
// The following two plugins use Object.assign directly, instead of Babel's
104-
// extends helper. Note that this assumes `Object.assign` is available.
105-
// { ...todo, completed: true }
106-
[
107-
require('@babel/plugin-proposal-object-rest-spread').default,
108-
{
109-
useBuiltIns: true,
110-
},
111-
],
112-
// Polyfills the runtime needed for async/await and generators
113-
[
114-
require('@babel/plugin-transform-runtime').default,
115-
{
116-
helpers: false,
117-
polyfill: false,
118-
regenerator: true,
119-
},
120-
],
121-
isEnvProduction && [
122-
// Remove PropTypes from production build
123-
require('babel-plugin-transform-react-remove-prop-types').default,
124-
{
125-
removeImport: true,
126-
},
127-
],
128-
// function* () { yield 42; yield 43; }
129-
!isEnvTest && [
130-
require('@babel/plugin-transform-regenerator').default,
131-
{
132-
// Async functions are converted to generators by @babel/preset-env
133-
async: false,
134-
},
135-
],
136-
// Adds syntax support for import()
137-
require('@babel/plugin-syntax-dynamic-import').default,
138-
isEnvTest &&
139-
// Transform dynamic import to require
140-
require('babel-plugin-transform-dynamic-import').default,
141-
].filter(Boolean),
142-
};
18+
const env = process.env.BABEL_ENV || process.env.NODE_ENV;
19+
return create(api, opts, env);
14320
};

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
},
1010
"files": [
1111
"index.js",
12-
"dependencies.js"
12+
"create.js",
13+
"dependencies.js",
14+
"dev.js",
15+
"prod.js",
16+
"test.js"
1317
],
1418
"dependencies": {
1519
"@babel/core": "7.0.0-beta.46",
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
const create = require('./create');
10+
11+
module.exports = function(api, opts) {
12+
return create(api, opts, 'production');
13+
};
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
const create = require('./create');
10+
11+
module.exports = function(api, opts) {
12+
return create(api, opts, 'test');
13+
};

0 commit comments

Comments
 (0)