File tree Expand file tree Collapse file tree 5 files changed +79
-7
lines changed Expand file tree Collapse file tree 5 files changed +79
-7
lines changed Original file line number Diff line number Diff line change @@ -1106,10 +1106,18 @@ class Encore {
11061106     * 
11071107     * https://babeljs.io/docs/plugins/preset-react/ 
11081108     * 
1109+      * You can configure the preset by passing a callback: 
1110+      * ``` 
1111+      * Encore.enableReactPreset(function(options) { 
1112+      *     // https://babeljs.io/docs/babel-preset-react/#options 
1113+      *     options.development = !Encore.isProduction(); 
1114+      * }); 
1115+      * ``` 
1116+      * 
11091117     * @returns  {Encore } 
11101118     */ 
1111-     enableReactPreset ( )  { 
1112-         webpackConfig . enableReactPreset ( ) ; 
1119+     enableReactPreset ( callback   =   ( )   =>   { } )  { 
1120+         webpackConfig . enableReactPreset ( callback ) ; 
11131121
11141122        return  this ; 
11151123    } 
Original file line number Diff line number Diff line change @@ -159,6 +159,8 @@ class WebpackConfig {
159159        /** @type  {OptionsCallback<object> } */ 
160160        this . babelPresetEnvOptionsCallback  =  ( )  =>  { } ; 
161161        /** @type  {OptionsCallback<object> } */ 
162+         this . babelReactPresetOptionsCallback  =  ( )  =>  { } ; 
163+         /** @type  {OptionsCallback<object> } */ 
162164        this . cssLoaderConfigurationCallback  =  ( )  =>  { } ; 
163165        /** @type  {OptionsCallback<object> } */ 
164166        this . styleLoaderConfigurationCallback  =  ( )  =>  { } ; 
@@ -793,8 +795,16 @@ class WebpackConfig {
793795        this . persistentCacheCallback  =  callback ; 
794796    } 
795797
796-     enableReactPreset ( )  { 
798+     /** 
799+      * @param  {OptionsCallback<object> } callback 
800+      */ 
801+     enableReactPreset ( callback  =  ( )  =>  { } )  { 
802+         if  ( typeof  callback  !==  'function' )  { 
803+             throw  new  Error ( 'Argument 1 to enableForkedTypeScriptTypesChecking() must be a callback function.' ) ; 
804+         } 
805+ 
797806        this . useReact  =  true ; 
807+         this . babelReactPresetOptionsCallback  =  callback ; 
798808    } 
799809
800810    enablePreactPreset ( options  =  { } )  { 
Original file line number Diff line number Diff line change @@ -70,10 +70,13 @@ module.exports = {
7070            if  ( webpackConfig . useReact )  { 
7171                loaderFeatures . ensurePackagesExistAndAreCorrectVersion ( 'react' ) ; 
7272
73-                 babelConfig . presets . push ( [ require . resolve ( '@babel/preset-react' ) ,  { 
74-                     // TODO: To remove when Babel 8, "automatic" will become the default value 
75-                     runtime : 'automatic' , 
76-                 } ] ) ; 
73+                 babelConfig . presets . push ( [ 
74+                     require . resolve ( '@babel/preset-react' ) , 
75+                     applyOptionsCallback ( webpackConfig . babelReactPresetOptionsCallback ,  { 
76+                         // TODO: To remove when Babel 8, "automatic" will become the default value 
77+                         runtime : 'automatic' , 
78+                     } ) 
79+                 ] ) ; 
7780            } 
7881
7982            if  ( webpackConfig . usePreact )  { 
Original file line number Diff line number Diff line change @@ -982,6 +982,23 @@ describe('WebpackConfig object', () => {
982982        } ) ; 
983983    } ) ; 
984984
985+     describe ( 'enableReactPreset' ,  ( )  =>  { 
986+         it ( 'Calling method sets it' ,  ( )  =>  { 
987+             const  config  =  createConfig ( ) ; 
988+             const  testCallback  =  ( )  =>  { } ; 
989+             config . enableReactPreset ( testCallback ) ; 
990+             expect ( config . babelReactPresetOptionsCallback ) . to . equal ( testCallback ) ; 
991+         } ) ; 
992+ 
993+         it ( 'Calling with non-callback throws an error' ,  ( )  =>  { 
994+             const  config  =  createConfig ( ) ; 
995+ 
996+             expect ( ( )  =>  { 
997+                 config . enableReactPreset ( 'FOO' ) ; 
998+             } ) . to . throw ( 'must be a callback function' ) ; 
999+         } ) ; 
1000+     } ) 
1001+ 
9851002    describe ( 'enablePreactPreset' ,  ( )  =>  { 
9861003        it ( 'Without preact-compat' ,  ( )  =>  { 
9871004            const  config  =  createConfig ( ) ; 
Original file line number Diff line number Diff line change @@ -94,6 +94,40 @@ describe('loaders/babel', () => {
9494        expect ( actualLoaders [ 0 ] . options . presets [ 2 ] ) . to . equal ( 'foo' ) ; 
9595    } ) ; 
9696
97+     it ( 'getLoaders() with react and callback' ,  ( )  =>  { 
98+         const  config  =  createConfig ( ) ; 
99+         config . enableReactPreset ( ( options )  =>  { 
100+             options . development  =  ! config . isProduction ( ) ; 
101+         } ) ; 
102+ 
103+         config . configureBabel ( function ( babelConfig )  { 
104+             babelConfig . presets . push ( 'foo' ) ; 
105+         } ) ; 
106+ 
107+         const  actualLoaders  =  babelLoader . getLoaders ( config ) ; 
108+ 
109+         // env, react & foo 
110+         expect ( actualLoaders [ 0 ] . options . presets ) . to . have . lengthOf ( 3 ) ; 
111+         expect ( actualLoaders [ 0 ] . options . presets [ 0 ] ) . to . deep . equal ( [ 
112+             require . resolve ( '@babel/preset-env' ) , 
113+             { 
114+                 corejs : null , 
115+                 modules : false , 
116+                 targets : { } , 
117+                 useBuiltIns : false , 
118+             } , 
119+         ] ) ; 
120+         expect ( actualLoaders [ 0 ] . options . presets [ 1 ] ) . to . deep . equal ( [ 
121+             require . resolve ( '@babel/preset-react' ) , 
122+             { 
123+                 runtime : 'automatic' , 
124+                 development : true , 
125+             } 
126+         ] ) ; 
127+         // foo is also still there, not overridden 
128+         expect ( actualLoaders [ 0 ] . options . presets [ 2 ] ) . to . equal ( 'foo' ) ; 
129+     } ) ; 
130+ 
97131    it ( 'getLoaders() with preact' ,  ( )  =>  { 
98132        const  config  =  createConfig ( ) ; 
99133        config . enablePreactPreset ( ) ; 
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments