@@ -44,121 +44,126 @@ const createConfigItems = (type, items) => {
4444
4545const presetEnvRegex = RegExp ( / @ b a b e l \/ ( p r e s e t - ) ? e n v / ) ;
4646
47- export default babelPlugin . custom ( babelCore => {
48- return {
49- // Passed the plugin options.
50- options ( { custom : customOptions , ...pluginOptions } ) {
51- return {
52- // Pull out any custom options that the plugin might have.
53- customOptions,
54-
55- // Pass the options back with the two custom options removed.
56- pluginOptions,
57- } ;
58- } ,
59-
60- config ( config , { customOptions } ) {
61- const defaultPlugins = createConfigItems (
62- 'plugin' ,
63- [
64- {
65- name : '@babel/plugin-transform-react-jsx' ,
66- pragma : customOptions . pragma || 'h' ,
67- pragmaFrag : customOptions . pragmaFrag || 'Fragment' ,
68- } ,
69- ! customOptions . typescript && {
70- name : '@babel/plugin-transform-flow-strip-types' ,
71- } ,
72- isTruthy ( customOptions . defines ) && {
73- name : 'babel-plugin-transform-replace-expressions' ,
74- replace : customOptions . defines ,
75- } ,
76- ! customOptions . modern && {
77- name : 'babel-plugin-transform-async-to-promises' ,
78- inlineHelpers : true ,
79- externalHelpers : true ,
80- } ,
81- {
82- name : '@babel/plugin-proposal-class-properties' ,
83- loose : true ,
84- } ,
85- ! customOptions . modern && {
86- name : '@babel/plugin-transform-regenerator' ,
87- async : false ,
88- } ,
89- {
90- name : 'babel-plugin-macros' ,
91- } ,
92- ] . filter ( Boolean ) ,
93- ) ;
94-
95- const babelOptions = config . options || { } ;
47+ export default ( ) => {
48+ return babelPlugin . custom ( babelCore => {
49+ return {
50+ // Passed the plugin options.
51+ options ( { custom : customOptions , ...pluginOptions } ) {
52+ return {
53+ // Pull out any custom options that the plugin might have.
54+ customOptions,
55+
56+ // Pass the options back with the two custom options removed.
57+ pluginOptions,
58+ } ;
59+ } ,
60+
61+ config ( config , { customOptions } ) {
62+ const defaultPlugins = createConfigItems (
63+ 'plugin' ,
64+ [
65+ {
66+ name : '@babel/plugin-transform-react-jsx' ,
67+ pragma : customOptions . pragma || 'h' ,
68+ pragmaFrag : customOptions . pragmaFrag || 'Fragment' ,
69+ } ,
70+ ! customOptions . typescript && {
71+ name : '@babel/plugin-transform-flow-strip-types' ,
72+ } ,
73+ isTruthy ( customOptions . defines ) && {
74+ name : 'babel-plugin-transform-replace-expressions' ,
75+ replace : customOptions . defines ,
76+ } ,
77+ ! customOptions . modern && {
78+ name : 'babel-plugin-transform-async-to-promises' ,
79+ inlineHelpers : true ,
80+ externalHelpers : true ,
81+ } ,
82+ {
83+ name : '@babel/plugin-proposal-class-properties' ,
84+ loose : true ,
85+ } ,
86+ ! customOptions . modern && {
87+ name : '@babel/plugin-transform-regenerator' ,
88+ async : false ,
89+ } ,
90+ {
91+ name : 'babel-plugin-macros' ,
92+ } ,
93+ ] . filter ( Boolean ) ,
94+ ) ;
9695
97- const envIdx = ( babelOptions . presets || [ ] ) . findIndex ( preset =>
98- presetEnvRegex . test ( preset . file . request ) ,
99- ) ;
96+ const babelOptions = config . options || { } ;
10097
101- const environmentPreset = customOptions . modern
102- ? '@babel/ preset-modules'
103- : '@babel/preset-env' ;
98+ const envIdx = ( babelOptions . presets || [ ] ) . findIndex ( preset =>
99+ presetEnvRegex . test ( preset . file . request ) ,
100+ ) ;
104101
105- if ( envIdx !== - 1 ) {
106- const preset = babelOptions . presets [ envIdx ] ;
107- babelOptions . presets [ envIdx ] = createConfigItem (
108- [
109- environmentPreset ,
110- Object . assign (
111- merge (
112- {
113- loose : true ,
114- useBuiltIns : false ,
115- targets : customOptions . targets ,
116- } ,
117- preset . options ,
118- {
119- modules : false ,
120- exclude : merge (
121- [ 'transform-async-to-generator' , 'transform-regenerator' ] ,
122- ( preset . options && preset . options . exclude ) || [ ] ,
123- ) ,
124- } ,
102+ const environmentPreset = customOptions . modern
103+ ? '@babel/preset-modules'
104+ : '@babel/preset-env' ;
105+
106+ if ( envIdx !== - 1 ) {
107+ const preset = babelOptions . presets [ envIdx ] ;
108+ babelOptions . presets [ envIdx ] = createConfigItem (
109+ [
110+ environmentPreset ,
111+ Object . assign (
112+ merge (
113+ {
114+ loose : true ,
115+ useBuiltIns : false ,
116+ targets : customOptions . targets ,
117+ } ,
118+ preset . options ,
119+ {
120+ modules : false ,
121+ exclude : merge (
122+ [ 'transform-async-to-generator' , 'transform-regenerator' ] ,
123+ ( preset . options && preset . options . exclude ) || [ ] ,
124+ ) ,
125+ } ,
126+ ) ,
127+ customOptions . modern ? { targets : ESMODULES_TARGET } : { } ,
125128 ) ,
126- customOptions . modern ? { targets : ESMODULES_TARGET } : { } ,
127- ) ,
128- ] ,
129- {
130- type : `preset` ,
131- } ,
129+ ] ,
130+ {
131+ type : `preset` ,
132+ } ,
133+ ) ;
134+ } else {
135+ babelOptions . presets = createConfigItems ( 'preset' , [
136+ {
137+ name : environmentPreset ,
138+ targets : customOptions . modern
139+ ? ESMODULES_TARGET
140+ : customOptions . targets ,
141+ modules : false ,
142+ loose : true ,
143+ useBuiltIns : false ,
144+ exclude : [
145+ 'transform-async-to-generator' ,
146+ 'transform-regenerator' ,
147+ ] ,
148+ } ,
149+ ] ) ;
150+ }
151+
152+ // Merge babelrc & our plugins together
153+ babelOptions . plugins = mergeConfigItems (
154+ 'plugin' ,
155+ defaultPlugins ,
156+ babelOptions . plugins || [ ] ,
132157 ) ;
133- } else {
134- babelOptions . presets = createConfigItems ( 'preset' , [
135- {
136- name : environmentPreset ,
137- targets : customOptions . modern
138- ? ESMODULES_TARGET
139- : customOptions . targets ,
140- modules : false ,
141- loose : true ,
142- useBuiltIns : false ,
143- exclude : [ 'transform-async-to-generator' , 'transform-regenerator' ] ,
144- } ,
145- ] ) ;
146- }
147-
148- // Merge babelrc & our plugins together
149- babelOptions . plugins = mergeConfigItems (
150- 'plugin' ,
151- defaultPlugins ,
152- babelOptions . plugins || [ ] ,
153- ) ;
154158
155- babelOptions . generatorOpts = {
156- minified : customOptions . compress ,
157- compact : customOptions . compress ,
158- shouldPrintComment : comment => / [ @ # ] _ _ P U R E _ _ / . test ( comment ) ,
159- } ;
159+ babelOptions . generatorOpts = {
160+ minified : customOptions . compress ,
161+ compact : customOptions . compress ,
162+ shouldPrintComment : comment => / [ @ # ] _ _ P U R E _ _ / . test ( comment ) ,
163+ } ;
160164
161- return babelOptions ;
162- } ,
163- } ;
164- } ) ;
165+ return babelOptions ;
166+ } ,
167+ } ;
168+ } ) ;
169+ } ;
0 commit comments