@@ -40,14 +40,14 @@ function prependImport(filePath: string, importLine: string, dryRun: boolean): s
4040 return filePath ;
4141}
4242
43- function removeImport ( filePath : string , dryRun : boolean ) : string | null {
43+ function removeImport ( filePath : string , importLine : string , dryRun : boolean ) : string | null {
4444 const content = readFileSync ( filePath , 'utf-8' ) ;
45- if ( ! content . includes ( 'agent-react-devtools' ) ) {
45+ if ( ! content . includes ( importLine ) ) {
4646 return null ; // not configured
4747 }
4848 const newContent = content
4949 . split ( '\n' )
50- . filter ( ( line ) => ! line . includes ( 'agent-react-devtools' ) )
50+ . filter ( ( line ) => line !== importLine )
5151 . join ( '\n' ) ;
5252 if ( ! dryRun ) {
5353 writeFileSync ( filePath , newContent , 'utf-8' ) ;
@@ -207,7 +207,7 @@ function unpatchViteConfig(cwd: string, dryRun: boolean): string[] {
207207
208208 let newContent = content
209209 . split ( '\n' )
210- . filter ( ( line ) => ! line . includes ( 'agent-react-devtools' ) )
210+ . filter ( ( line ) => line !== "import { reactDevtools } from 'agent-react-devtools/vite';" )
211211 . join ( '\n' ) ;
212212
213213 // Remove reactDevtools() call from plugins array (with optional trailing comma)
@@ -235,27 +235,32 @@ function unpatchNextJs(cwd: string, dryRun: boolean): string[] {
235235
236236 if ( layoutPath ) {
237237 const devtoolsPath = join ( dirname ( layoutPath ) , 'devtools.ts' ) ;
238+ let devtoolsIsOurs = false ;
238239 if ( existsSync ( devtoolsPath ) ) {
239240 const content = readFileSync ( devtoolsPath , 'utf-8' ) ;
240241 if ( content . includes ( 'agent-react-devtools' ) ) {
242+ devtoolsIsOurs = true ;
241243 if ( ! dryRun ) {
242244 unlinkSync ( devtoolsPath ) ;
243245 }
244246 modified . push ( devtoolsPath ) ;
245247 }
246248 }
247249
248- // Remove the import of ./devtools from layout
249- const layoutContent = readFileSync ( layoutPath , 'utf-8' ) ;
250- if ( layoutContent . includes ( "'./devtools'" ) || layoutContent . includes ( 'agent-react-devtools' ) ) {
250+ // Only remove the layout import if we confirmed devtools.ts was created by us,
251+ // to avoid corrupting a pre-existing import './devtools' that we don't own.
252+ if ( devtoolsIsOurs ) {
253+ const layoutContent = readFileSync ( layoutPath , 'utf-8' ) ;
251254 const newContent = layoutContent
252255 . split ( '\n' )
253- . filter ( ( line ) => ! line . includes ( " './devtools'" ) && ! line . includes ( 'agent-react-devtools' ) )
256+ . filter ( ( line ) => line !== "import './devtools';" )
254257 . join ( '\n' ) ;
255- if ( ! dryRun ) {
256- writeFileSync ( layoutPath , newContent , 'utf-8' ) ;
258+ if ( newContent !== layoutContent ) {
259+ if ( ! dryRun ) {
260+ writeFileSync ( layoutPath , newContent , 'utf-8' ) ;
261+ }
262+ modified . push ( layoutPath ) ;
257263 }
258- modified . push ( layoutPath ) ;
259264 }
260265 }
261266
@@ -270,7 +275,7 @@ function unpatchNextJs(cwd: string, dryRun: boolean): string[] {
270275 'src/pages/_app.js' ,
271276 ) ;
272277 if ( pagesEntry ) {
273- const result = removeImport ( pagesEntry , dryRun ) ;
278+ const result = removeImport ( pagesEntry , "import 'agent-react-devtools/connect';" , dryRun ) ;
274279 if ( result ) modified . push ( result ) ;
275280 }
276281
@@ -286,7 +291,7 @@ function unpatchCRA(cwd: string, dryRun: boolean): string[] {
286291 ) ;
287292 if ( ! entryPath ) return [ ] ;
288293
289- const result = removeImport ( entryPath , dryRun ) ;
294+ const result = removeImport ( entryPath , "import 'agent-react-devtools/connect';" , dryRun ) ;
290295 return result ? [ result ] : [ ] ;
291296}
292297
0 commit comments