@@ -25,19 +25,28 @@ export default class MagicStringStack implements MagicString {
2525 this . _stack . unshift ( this . _current )
2626
2727 // We proxy every function to `this._current` so we can swap it out
28- return new Proxy ( new MagicString ( '' ) , {
28+ const proxy = new Proxy ( new MagicString ( '' ) , {
2929 get : ( _ , p , receiver ) => {
3030 if ( Reflect . has ( this , p ) )
3131 return Reflect . get ( this , p , receiver )
32- let parent = Reflect . get ( this . _current , p , receiver )
33- if ( typeof parent === 'function' )
34- parent = parent . bind ( this . _current )
32+ const parent = Reflect . get ( this . _current , p , receiver )
33+ if ( typeof parent === 'function' ) {
34+ return ( ...args : any ) => {
35+ const result = parent . apply ( this . _current , args )
36+ // If the function returns the current instance (chainable), we return the proxy instead
37+ if ( result === this . _current )
38+ return proxy
39+ return result
40+ }
41+ }
3542 return parent
3643 } ,
3744 set : ( _ , p , value ) => {
3845 return Reflect . set ( this , p , value , this )
3946 } ,
4047 } ) as any
48+
49+ return proxy
4150 }
4251
4352 /**
0 commit comments