File tree 6 files changed +84
-7
lines changed 6 files changed +84
-7
lines changed Original file line number Diff line number Diff line change 4
4
5
5
This is a new major version that contains several backwards-compatibility breaks.
6
6
7
+ ### Features
8
+
9
+ * #1344 Add options configuration callback to ` Encore.enableReactPreset() ` (@Kocal )
10
+
7
11
### BC Breaks
8
12
9
13
* #1321 Drop support of Node.js 19 and 21 (@Kocal )
Original file line number Diff line number Diff line change @@ -1106,10 +1106,19 @@ class Encore {
1106
1106
*
1107
1107
* https://babeljs.io/docs/plugins/preset-react/
1108
1108
*
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
+ *
1117
+ * @param {OptionsCallback<object> } callback
1109
1118
* @returns {Encore }
1110
1119
*/
1111
- enableReactPreset ( ) {
1112
- webpackConfig . enableReactPreset ( ) ;
1120
+ enableReactPreset ( callback = ( ) => { } ) {
1121
+ webpackConfig . enableReactPreset ( callback ) ;
1113
1122
1114
1123
return this ;
1115
1124
}
Original file line number Diff line number Diff line change @@ -159,6 +159,8 @@ class WebpackConfig {
159
159
/** @type {OptionsCallback<object> } */
160
160
this . babelPresetEnvOptionsCallback = ( ) => { } ;
161
161
/** @type {OptionsCallback<object> } */
162
+ this . babelReactPresetOptionsCallback = ( ) => { } ;
163
+ /** @type {OptionsCallback<object> } */
162
164
this . cssLoaderConfigurationCallback = ( ) => { } ;
163
165
/** @type {OptionsCallback<object> } */
164
166
this . styleLoaderConfigurationCallback = ( ) => { } ;
@@ -793,8 +795,16 @@ class WebpackConfig {
793
795
this . persistentCacheCallback = callback ;
794
796
}
795
797
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
+
797
806
this . useReact = true ;
807
+ this . babelReactPresetOptionsCallback = callback ;
798
808
}
799
809
800
810
enablePreactPreset ( options = { } ) {
Original file line number Diff line number Diff line change @@ -70,10 +70,13 @@ module.exports = {
70
70
if ( webpackConfig . useReact ) {
71
71
loaderFeatures . ensurePackagesExistAndAreCorrectVersion ( 'react' ) ;
72
72
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
+ ] ) ;
77
80
}
78
81
79
82
if ( webpackConfig . usePreact ) {
Original file line number Diff line number Diff line change @@ -982,6 +982,23 @@ describe('WebpackConfig object', () => {
982
982
} ) ;
983
983
} ) ;
984
984
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
+
985
1002
describe ( 'enablePreactPreset' , ( ) => {
986
1003
it ( 'Without preact-compat' , ( ) => {
987
1004
const config = createConfig ( ) ;
Original file line number Diff line number Diff line change @@ -94,6 +94,40 @@ describe('loaders/babel', () => {
94
94
expect ( actualLoaders [ 0 ] . options . presets [ 2 ] ) . to . equal ( 'foo' ) ;
95
95
} ) ;
96
96
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
+
97
131
it ( 'getLoaders() with preact' , ( ) => {
98
132
const config = createConfig ( ) ;
99
133
config . enablePreactPreset ( ) ;
You can’t perform that action at this time.
0 commit comments