@@ -123,6 +123,12 @@ function replaceBuild(buildReplacePattern: ReplacePattern, result: BuildResult)
123123 ) ;
124124}
125125
126+ const useClientRegExp = / ^ ( [ " ' ] u s e s t r i c t [ " ' ] ; ) ? [ " ' ] u s e c l i e n t [ " ' ] ; ? / i;
127+ const 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;
129+ const regExp2replace2GetVar0 = / ( v a r | , ) / ;
130+ const regExp2replace2GetVar = / [ = ] r e q u i r e \( [ ' " ] r e a c t \/ j s x - r u n t i m e [ ' " ] \) ; ? / ;
131+
126132function onEndCallBack ( result : BuildResult , options : React18PluginOptions , write ?: boolean ) {
127133 /** remove empty file imports */
128134 const emptyChunkFiles = result . outputFiles
@@ -139,20 +145,30 @@ function onEndCallBack(result: BuildResult, options: React18PluginOptions, write
139145 ?. filter ( f => ! f . path . endsWith ( ".map" ) )
140146 . forEach ( f => {
141147 let txt = f . text ;
142- txt = txt . replace (
143- / ^ ( [ " ' ] u s e s t r i c t [ " ' ] ; ) ? [ " ' ] u s e c l i e n t [ " ' ] ; ? / i,
144- '"use client";\n"use strict";' ,
145- ) ;
148+ txt = txt . replace ( useClientRegExp , '"use client";\n"use strict";' ) ;
146149
147150 /** module level use server */
148- txt = txt . replace (
149- / ^ ( [ " ' ] u s e s t r i c t [ " ' ] ; ) ? [ " ' ] u s e s e r v e r [ " ' ] ; ? / i,
150- '"use server";\n"use strict";' ,
151- ) ;
151+ txt = txt . replace ( useServerRegExp , '"use server";\n"use strict";' ) ;
152152
153153 /** remove empty file imports */
154154 txt = txt . replace ( emptyChunkImportRegExp , "" ) ;
155155
156+ /** remove extra jsx-runtime imports */
157+ if ( f . path . endsWith ( ".js" ) ) {
158+ const jsxMatches = txt . match ( jsxImportRegExp ) ;
159+ if ( jsxMatches !== null && jsxMatches . length > 1 ) {
160+ const importVarName = jsxMatches [ 0 ]
161+ . replace ( regExp2replace2GetVar , "" )
162+ . replace ( regExp2replace2GetVar0 , "" ) ;
163+ for ( let index = 1 ; index < jsxMatches . length ; index ++ ) {
164+ txt = txt . replace ( jsxMatches [ index ] , "" ) ;
165+ const v1 = jsxMatches [ index ]
166+ . replace ( regExp2replace2GetVar , "" )
167+ . replace ( regExp2replace2GetVar0 , "" ) ;
168+ txt = txt . replace ( new RegExp ( `\\b${ v1 } \\b` , "g" ) , importVarName ) ;
169+ }
170+ }
171+ }
156172 f . contents = new TextEncoder ( ) . encode ( txt ) ;
157173 } ) ;
158174
0 commit comments