@@ -27,6 +27,7 @@ module.exports = function(api, opts, env) {
27
27
var isEnvProduction = env === 'production' ;
28
28
var isEnvTest = env === 'test' ;
29
29
var isFlowEnabled = validateBoolOption ( 'flow' , opts . flow , true ) ;
30
+ var isModern = validateBoolOption ( 'modern' , opts . modern , false ) ;
30
31
31
32
if ( ! isEnvDevelopment && ! isEnvProduction && ! isEnvTest ) {
32
33
throw new Error (
@@ -38,7 +39,30 @@ module.exports = function(api, opts, env) {
38
39
) ;
39
40
}
40
41
41
- return {
42
+ function getEnvOptions ( { isModern } ) {
43
+ const defaultOpts = {
44
+ // `entry` transforms `@babel/polyfill` into individual requires for
45
+ // the targeted browsers. This is safer than `usage` which performs
46
+ // static code analysis to determine what's required.
47
+ // This is probably a fine default to help trim down bundles when
48
+ // end-users inevitably import '@babel/polyfill'.
49
+ useBuiltIns : 'entry' ,
50
+ // Do not transform modules to CJS
51
+ modules : false ,
52
+ } ;
53
+
54
+ if ( isModern ) {
55
+ return Object . assign ( defaultOpts , {
56
+ targets : {
57
+ esmodules : true ,
58
+ } ,
59
+ } ) ;
60
+ }
61
+
62
+ return defaultOpts ;
63
+ }
64
+
65
+ const babelPreset = {
42
66
presets : [
43
67
isEnvTest && [
44
68
// ES features necessary for user's Node version
@@ -52,16 +76,7 @@ module.exports = function(api, opts, env) {
52
76
( isEnvProduction || isEnvDevelopment ) && [
53
77
// Latest stable ECMAScript features
54
78
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
- } ,
79
+ getEnvOptions ( { isModern } ) ,
65
80
] ,
66
81
[
67
82
require ( '@babel/preset-react' ) . default ,
@@ -103,7 +118,7 @@ module.exports = function(api, opts, env) {
103
118
} ,
104
119
] ,
105
120
// Polyfills the runtime needed for async/await and generators
106
- [
121
+ ! isModern && [
107
122
require ( '@babel/plugin-transform-runtime' ) . default ,
108
123
{
109
124
helpers : false ,
@@ -119,18 +134,23 @@ module.exports = function(api, opts, env) {
119
134
} ,
120
135
] ,
121
136
// 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
- ] ,
137
+ ! isEnvTest &&
138
+ ! isModern && [
139
+ require ( '@babel/plugin-transform-regenerator' ) . default ,
140
+ {
141
+ // Async functions are converted to generators by @babel /preset-env
142
+ async : false ,
143
+ } ,
144
+ ] ,
129
145
// Adds syntax support for import()
130
146
require ( '@babel/plugin-syntax-dynamic-import' ) . default ,
131
147
isEnvTest &&
132
148
// Transform dynamic import to require
133
149
require ( 'babel-plugin-transform-dynamic-import' ) . default ,
134
150
] . filter ( Boolean ) ,
135
151
} ;
152
+
153
+ // console.log('PRESET', JSON.stringify(babelPreset)
154
+
155
+ return babelPreset ;
136
156
} ;
0 commit comments