@@ -235,22 +235,52 @@ export function transformRsbuildConfig(
235235 }
236236
237237 if ( Array . isArray ( config . plugins ) ) {
238- // Check if already exists using a regex to avoid false positives and support aliases
239- const hasPlugin =
240- / \b a r k e n v (?: R s b u i l d ) ? P l u g i n \b / . test ( initialCode ) ||
241- / f r o m \s + [ ' " ] @ a r k e n v \/ r s b u i l d - p l u g i n (?: \/ s t a n d a r d ) ? [ ' " ] / . test (
242- initialCode ,
243- ) ;
238+ // Find identifier names imported from @arkenv /rsbuild-plugin (supports aliases)
239+ const rsbuildPluginImports = new Set < string > ( ) ;
240+ for ( const item of mod . imports . $items || [ ] ) {
241+ if ( item . from && item . from . startsWith ( "@arkenv/rsbuild-plugin" ) ) {
242+ rsbuildPluginImports . add ( item . local ) ;
243+ }
244+ }
245+ rsbuildPluginImports . add ( "arkenvRsbuildPlugin" ) ;
246+
247+ // Check if already registered in the plugins array AST
248+ const pluginsList = Array . from ( config . plugins as any [ ] ) ;
249+ const hasPlugin = pluginsList . some ( ( p ) => {
250+ if ( typeof p !== "object" || ! p || ! ( "$type" in p ) ) return false ;
251+ if ( p . $type === "function-call" ) {
252+ return (
253+ typeof p . $callee === "string" && rsbuildPluginImports . has ( p . $callee )
254+ ) ;
255+ }
256+ if ( p . $type === "identifier" ) {
257+ return (
258+ p . $ast ?. type === "Identifier" &&
259+ rsbuildPluginImports . has ( p . $ast . name )
260+ ) ;
261+ }
262+ return false ;
263+ } ) ;
244264
245265 if ( ! hasPlugin ) {
246- // Add imports
247- mod . imports . $add ( {
248- from : "@arkenv/rsbuild-plugin" ,
249- local : "arkenvRsbuildPlugin" ,
250- imported : "arkenvRsbuildPlugin" ,
251- } ) ;
266+ const existingImport = ( mod . imports . $items || [ ] ) . find (
267+ ( item ) => item . from && item . from . startsWith ( "@arkenv/rsbuild-plugin" ) ,
268+ ) ;
269+ const localName = existingImport ?. local || "arkenvRsbuildPlugin" ;
270+
271+ if ( ! existingImport ) {
272+ mod . imports . $add ( {
273+ from : "@arkenv/rsbuild-plugin" ,
274+ local : "arkenvRsbuildPlugin" ,
275+ imported : "arkenvRsbuildPlugin" ,
276+ } ) ;
277+ }
252278
253- config . plugins . push ( "__ARK_PLUGIN_PLACEHOLDER__" ) ;
279+ const placeholder =
280+ localName === "arkenvRsbuildPlugin"
281+ ? "__ARK_PLUGIN__"
282+ : `__ARK_PLUGIN__:${ localName } ` ;
283+ config . plugins . push ( placeholder ) ;
254284 } else {
255285 // Already has plugin, nothing to do
256286 return { success : true , updated : false } ;
@@ -266,8 +296,9 @@ export function transformRsbuildConfig(
266296 let code = generateCode ( mod , {
267297 format : detectCodeFormat ( initialCode ) ,
268298 } ) . code ;
269- const pluginCall = "arkenvRsbuildPlugin()" ;
270- code = code . replace ( / [ ' " ] _ _ A R K _ P L U G I N _ P L A C E H O L D E R _ _ [ ' " ] / g, pluginCall ) ;
299+ code = code . replace ( / [ ' " ] _ _ A R K _ P L U G I N _ _ (?: : ( .+ ?) ) ? [ ' " ] / g, ( _ , name ) =>
300+ name ? `${ name } ()` : "arkenvRsbuildPlugin()" ,
301+ ) ;
271302 code = normalizeImportSpacing ( code ) ;
272303 code = preserveTrailingNewline ( code , initialCode ) ;
273304
0 commit comments