|
6 | 6 | */
|
7 | 7 | 'use strict';
|
8 | 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 |
| -}; |
| 9 | +const create = require('./create'); |
20 | 10 |
|
21 | 11 | module.exports = function(api, opts) {
|
22 |
| - if (!opts) { |
23 |
| - opts = {}; |
24 |
| - } |
25 |
| - |
26 | 12 | // This is similar to how `env` works in Babel:
|
27 | 13 | // https://babeljs.io/docs/usage/babelrc/#env-option
|
28 | 14 | // We are not using `env` because it’s ignored in versions > babel-core@6.10.4:
|
29 | 15 | // https://github.com/babel/babel/issues/4539
|
30 | 16 | // https://github.com/facebook/create-react-app/issues/720
|
31 | 17 | // 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); |
143 | 20 | };
|
0 commit comments