@@ -44,9 +44,13 @@ const construcNamesToWrap = [
4444 "ContextModuleFactory" ,
4545] ;
4646
47+ const wrappedObjs = [ ] ;
4748const wrap = ( orig , pluginName , smp , addEndEvent ) => {
48- if ( ! orig || orig . _smpWrapped ) return orig ;
49- orig . _smpWrapped = true ;
49+ if ( ! orig ) return orig ;
50+ const prevWrapped = wrappedObjs . find (
51+ w => w . pluginName === pluginName && ( w . orig === orig || w . wrapped === orig )
52+ ) ;
53+ if ( prevWrapped ) return prevWrapped . wrapped ;
5054
5155 const getOrigConstrucName = target =>
5256 target && target . constructor && target . constructor . name ;
@@ -59,9 +63,11 @@ const wrap = (orig, pluginName, smp, addEndEvent) => {
5963 . map ( k => orig [ k ] )
6064 . some ( getShouldWrap ) ;
6165
66+ let wrappedReturn ;
67+
6268 if ( ! shouldWrap && ! shouldSoftWrap ) {
6369 const vanillaFunc = orig . name === "next" ;
64- return vanillaFunc
70+ wrappedReturn = vanillaFunc
6571 ? function ( ) {
6672 // do this before calling the callback, since the callback can start
6773 // the next plugin step
@@ -70,35 +76,41 @@ const wrap = (orig, pluginName, smp, addEndEvent) => {
7076 return orig . apply ( this , arguments ) ;
7177 }
7278 : orig ;
79+ } else {
80+ const proxy = new Proxy ( orig , {
81+ get : ( target , property ) => {
82+ if ( shouldWrap && property === "plugin" )
83+ return genPluginMethod (
84+ target ,
85+ pluginName ,
86+ smp ,
87+ getOrigConstrucName ( target )
88+ ) . bind ( proxy ) ;
89+
90+ if ( typeof target [ property ] === "function" ) {
91+ const ret = target [ property ] . bind ( proxy ) ;
92+ if ( property === "constructor" )
93+ Object . defineProperty ( ret , "name" , {
94+ value : target . constructor . name ,
95+ } ) ;
96+ return ret ;
97+ }
98+
99+ return target [ property ] ;
100+ } ,
101+ set : ( target , property , value ) => {
102+ return Reflect . set ( target , property , value ) ;
103+ } ,
104+ deleteProperty : ( target , property ) => {
105+ return Reflect . deleteProperty ( target , property ) ;
106+ } ,
107+ } ) ;
108+
109+ wrappedReturn = proxy ;
73110 }
74111
75- const proxy = new Proxy ( orig , {
76- get : ( target , property ) => {
77- if ( shouldWrap && property === "plugin" )
78- return genPluginMethod (
79- orig ,
80- pluginName ,
81- smp ,
82- getOrigConstrucName ( target )
83- ) . bind ( proxy ) ;
84-
85- if ( typeof orig [ property ] === "function" ) {
86- const ret = orig [ property ] . bind ( proxy ) ;
87- if ( property === "constructor" )
88- Object . defineProperty ( ret , "name" , { value : orig . constructor . name } ) ;
89- return ret ;
90- }
91- return wrap ( orig [ property ] , pluginName , smp ) ;
92- } ,
93- set : ( target , property , value ) => {
94- return Reflect . set ( target , property , value ) ;
95- } ,
96- deleteProperty : ( target , property ) => {
97- return Reflect . deleteProperty ( target , property ) ;
98- } ,
99- } ) ;
100-
101- return proxy ;
112+ wrappedObjs . push ( { pluginName, orig, wrapped : wrappedReturn } ) ;
113+ return wrappedReturn ;
102114} ;
103115
104116module . exports . WrappedPlugin = class WrappedPlugin {
0 commit comments