@@ -60,6 +60,11 @@ data-testid` attributes. If your
6060 * to avoid any unexpected results.
6161 */
6262 buildReplacePatterns ?: ReplacePattern [ ] ;
63+
64+ /**
65+ * In case you face any errors, or you want to speed up build a bit, try disabling deduplication of require("react/jsx-runtime")
66+ */
67+ disableJSXRequireDedup ?: boolean ;
6368}
6469
6570function removeTests ( build : PluginBuild , options : React18PluginOptions ) {
@@ -125,7 +130,7 @@ function replaceBuild(buildReplacePattern: ReplacePattern, result: BuildResult)
125130
126131const useClientRegExp = / ^ ( [ " ' ] u s e s t r i c t [ " ' ] ; ) ? [ " ' ] u s e c l i e n t [ " ' ] ; ? / i;
127132const useServerRegExp = / ^ ( [ " ' ] u s e s t r i c t [ " ' ] ; ) ? [ " ' ] u s e s e r v e r [ " ' ] ; ? / i;
128- const jsxImportRegExp = / ( v a r | , ) ? [ a - z A - Z _ $ ] [ \w $ ] * = r e q u i r e \( " r e a c t \/ j s x - r u n t i m e " \) [ ; , ] ? / g;
133+ const jsxImportRegExp = / ( v a r | , ) [ a - z A - Z _ $ ] [ \w $ ] * = r e q u i r e \( " r e a c t \/ j s x - r u n t i m e " \) [ ; , ] ? / g;
129134const regExp2replace2GetVar0 = / ( v a r | , ) / ;
130135const regExp2replace2GetVar = / [ = ] r e q u i r e \( [ ' " ] r e a c t \/ j s x - r u n t i m e [ ' " ] \) [ ; , ] ? / ;
131136
@@ -154,16 +159,17 @@ function onEndCallBack(result: BuildResult, options: React18PluginOptions, write
154159 txt = txt . replace ( emptyChunkImportRegExp , "" ) ;
155160
156161 /** remove extra jsx-runtime imports */
157- if ( f . path . endsWith ( ".js" ) ) {
162+ if ( ! options . disableJSXRequireDedup && f . path . endsWith ( ".js" ) ) {
158163 const jsxMatches = txt . match ( jsxImportRegExp ) ;
159164 if ( jsxMatches !== null && jsxMatches . length > 1 ) {
160165 const importVarName = jsxMatches [ 0 ]
161166 . replace ( regExp2replace2GetVar , "" )
162167 . replace ( regExp2replace2GetVar0 , "" ) ;
163168 for ( let index = 1 ; index < jsxMatches . length ; index ++ ) {
164- const token = jsxMatches [ index ] ;
165- const toReplace = / ^ , .* , $ / . test ( token ) ? token . slice ( 1 ) : token ;
166- txt = txt . replace ( toReplace , "" ) ;
169+ let token = jsxMatches [ index ] ;
170+ if ( / ^ , .* , $ / . test ( token ) ) token = token . slice ( 1 ) ;
171+ else if ( token . startsWith ( "var " ) && token . endsWith ( "," ) ) token = token . slice ( 4 ) ;
172+ txt = txt . replace ( token , "" ) ;
167173 const v1 = jsxMatches [ index ]
168174 . replace ( regExp2replace2GetVar , "" )
169175 . replace ( regExp2replace2GetVar0 , "" ) ;
@@ -196,6 +202,8 @@ function onEndCallBack(result: BuildResult, options: React18PluginOptions, write
196202function setup ( build : PluginBuild , options : React18PluginOptions = { } ) {
197203 const write = build . initialOptions . write ;
198204 build . initialOptions . write = false ;
205+ // Avoid addiitonal computation when in watch mode or minification is not required
206+ if ( ! build . initialOptions . minify ) options . disableJSXRequireDedup = true ;
199207
200208 if ( ! options . keepTests ) removeTests ( build , options ) ;
201209
0 commit comments