@@ -7,8 +7,11 @@ const genPluginMethod = (orig, pluginName, smp, type) =>
77 const timeEventName = pluginName + "/" + type + "/" + method ;
88 // we don't know if there's going to be a callback applied to a particular
99 // call, so we just set it multiple times, letting each one override the last
10- const addEndEvent = ( ) =>
10+ let endCallCount = 0 ;
11+ const addEndEvent = ( ) => {
12+ endCallCount ++ ;
1113 smp . addTimeEvent ( "plugins" , timeEventName , "end" , { id } ) ;
14+ } ;
1215
1316 smp . addTimeEvent ( "plugins" , timeEventName , "start" , {
1417 id,
@@ -18,7 +21,11 @@ const genPluginMethod = (orig, pluginName, smp, type) =>
1821 this ,
1922 args . map ( a => wrap ( a , pluginName , smp , addEndEvent ) )
2023 ) ;
21- addEndEvent ( ) ;
24+
25+ // If the end event was invoked as a callback immediately, we can
26+ // don't want to add another end event here (and it can actually cause
27+ // errors, if webpack has finished compilation entirely)
28+ if ( ! endCallCount ) addEndEvent ( ) ;
2229
2330 return ret ;
2431 } ;
@@ -45,19 +52,19 @@ const wrap = (orig, pluginName, smp, addEndEvent) => {
4552 return construcNamesToWrap . includes ( origConstrucName ) ;
4653 } ;
4754 const shouldWrap = getShouldWrap ( orig ) ;
48- const shouldSoftWrap = Object . values ( orig ) . some ( getShouldWrap ) ;
55+ const shouldSoftWrap = Object . keys ( orig )
56+ . map ( k => orig [ k ] )
57+ . some ( getShouldWrap ) ;
4958
5059 if ( ! shouldWrap && ! shouldSoftWrap ) {
51- const vanillaFunc =
52- typeof orig === "function" &&
53- orig &&
54- orig . prototype &&
55- orig . prototype . constructor !== orig ;
60+ const vanillaFunc = orig . name === "next" ;
5661 return vanillaFunc
5762 ? function ( ) {
58- const ret = orig ( ) ;
63+ // do this before calling the callback, since the callback can start
64+ // the next plugin step
5965 addEndEvent ( ) ;
60- return ret ;
66+
67+ return orig . apply ( this , arguments ) ;
6168 }
6269 : orig ;
6370 }
0 commit comments