8
8
9
9
const path = require ( 'path' ) ;
10
10
11
- const validateBoolOption = ( name , value , defaultValue ) => {
12
- if ( typeof value === 'undefined' ) {
13
- value = defaultValue ;
14
- }
15
-
16
- if ( typeof value !== 'boolean' ) {
17
- throw new Error ( `Preset react-app: '${ name } ' option must be a boolean.` ) ;
11
+ const assert = ( assertion , message ) => {
12
+ if ( ! assertion ) {
13
+ throw new Error ( message ) ;
18
14
}
15
+ } ;
19
16
20
- return value ;
17
+ const assertBool = ( value , name ) => {
18
+ assert (
19
+ typeof value === 'boolean' ,
20
+ `Preset react-app: '${ name } ' option must be a boolean.`
21
+ ) ;
21
22
} ;
22
23
23
- module . exports = function ( api , opts , env ) {
24
- if ( ! opts ) {
25
- opts = { } ;
26
- }
24
+ const modules = [ 'amd' , 'umd' , 'systemjs' , 'commonjs' , 'cjs' , 'auto' , false ] ;
27
25
26
+ module . exports = function ( api , opts , env ) {
28
27
var isEnvDevelopment = env === 'development' ;
29
28
var isEnvProduction = env === 'production' ;
30
29
var isEnvTest = env === 'test' ;
31
30
32
- var useESModules = validateBoolOption (
33
- 'useESModules' ,
34
- opts . useESModules ,
35
- isEnvDevelopment || isEnvProduction
36
- ) ;
37
- var isFlowEnabled = validateBoolOption ( 'flow' , opts . flow , true ) ;
38
- var isTypeScriptEnabled = validateBoolOption (
39
- 'typescript' ,
40
- opts . typescript ,
41
- true
42
- ) ;
43
- var areHelpersEnabled = validateBoolOption ( 'helpers' , opts . helpers , true ) ;
44
- var useAbsoluteRuntime = validateBoolOption (
45
- 'absoluteRuntime' ,
46
- opts . absoluteRuntime ,
47
- true
31
+ const defaults = {
32
+ modules : false ,
33
+ useESModules : isEnvDevelopment || isEnvProduction ,
34
+ absoluteRuntime : true ,
35
+ typescript : true ,
36
+ helpers : true ,
37
+ flow : true ,
38
+ } ;
39
+
40
+ opts = Object . assign ( { } , defaults , opts || { } ) ;
41
+
42
+ assert (
43
+ modules . includes ( opts . modules ) ,
44
+ `Preset react-app: 'modules' option must be one of: ${ modules . join ( ', ' ) } .`
48
45
) ;
46
+ assertBool ( opts . useESModules , 'useESModules' ) ;
47
+ assertBool ( opts . absoluteRuntime , 'absoluteRuntime' ) ;
48
+ assertBool ( opts . typescript , 'typescript' ) ;
49
+ assertBool ( opts . helpers , 'helpers' ) ;
50
+ assertBool ( opts . flow , 'flow' ) ;
49
51
50
52
var absoluteRuntimePath = undefined ;
51
- if ( useAbsoluteRuntime ) {
53
+ if ( opts . absoluteRuntime ) {
52
54
absoluteRuntimePath = path . dirname (
53
55
require . resolve ( '@babel/runtime/package.json' )
54
56
) ;
@@ -90,8 +92,8 @@ module.exports = function(api, opts, env) {
90
92
// If users import all core-js they're probably not concerned with
91
93
// bundle size. We shouldn't rely on magic to try and shrink it.
92
94
useBuiltIns : false ,
93
- // Do not transform modules to CJS
94
- modules : false ,
95
+ // Do not transform modules to CJS by default
96
+ modules : opts . modules ,
95
97
// Exclude transforms that make all code slower
96
98
exclude : [ 'transform-typeof-symbol' ] ,
97
99
} ,
@@ -107,7 +109,7 @@ module.exports = function(api, opts, env) {
107
109
useBuiltIns : true ,
108
110
} ,
109
111
] ,
110
- isTypeScriptEnabled && [ require ( '@babel/preset-typescript' ) . default ] ,
112
+ opts . typescript && [ require ( '@babel/preset-typescript' ) . default ] ,
111
113
] . filter ( Boolean ) ,
112
114
plugins : [
113
115
// Strip flow types before any other transform, emulating the behavior
@@ -116,7 +118,7 @@ module.exports = function(api, opts, env) {
116
118
// We will conditionally enable this plugin below in overrides as it clashes with
117
119
// @babel /plugin-proposal-decorators when using TypeScript.
118
120
// https://github.com/facebook/create-react-app/issues/5741
119
- isFlowEnabled && [
121
+ opts . flow && [
120
122
require ( '@babel/plugin-transform-flow-strip-types' ) . default ,
121
123
false ,
122
124
] ,
@@ -128,7 +130,7 @@ module.exports = function(api, opts, env) {
128
130
// don't work without it: https://github.com/babel/babel/issues/7215
129
131
require ( '@babel/plugin-transform-destructuring' ) . default ,
130
132
// Turn on legacy decorators for TypeScript files
131
- isTypeScriptEnabled && [
133
+ opts . typescript && [
132
134
require ( '@babel/plugin-proposal-decorators' ) . default ,
133
135
false ,
134
136
] ,
@@ -156,12 +158,12 @@ module.exports = function(api, opts, env) {
156
158
require ( '@babel/plugin-transform-runtime' ) . default ,
157
159
{
158
160
corejs : false ,
159
- helpers : areHelpersEnabled ,
161
+ helpers : opts . helpers ,
160
162
regenerator : true ,
161
163
// https://babeljs.io/docs/en/babel-plugin-transform-runtime#useesmodules
162
164
// We should turn this on once the lowest version of Node LTS
163
165
// supports ES Modules.
164
- useESModules,
166
+ useESModules : opts . useESModules ,
165
167
// Undocumented option that lets us encapsulate our runtime, ensuring
166
168
// the correct version is used
167
169
// https://github.com/babel/babel/blob/090c364a90fe73d36a30707fc612ce037bdbbb24/packages/babel-plugin-transform-runtime/src/index.js#L35-L42
@@ -182,11 +184,11 @@ module.exports = function(api, opts, env) {
182
184
require ( 'babel-plugin-dynamic-import-node' ) ,
183
185
] . filter ( Boolean ) ,
184
186
overrides : [
185
- isFlowEnabled && {
187
+ opts . flow && {
186
188
exclude : / \. t s x ? $ / ,
187
189
plugins : [ require ( '@babel/plugin-transform-flow-strip-types' ) . default ] ,
188
190
} ,
189
- isTypeScriptEnabled && {
191
+ opts . typescript && {
190
192
test : / \. t s x ? $ / ,
191
193
plugins : [
192
194
[
0 commit comments