@@ -2,16 +2,31 @@ let idInc = 0;
22
33const genPluginMethod = ( orig , pluginName , smp , type ) =>
44 function ( method , func ) {
5- const id = idInc ++ ;
6- const timeEventName = type + "/" + method ;
7-
85 const wrappedFunc = ( ...args ) => {
6+ const id = idInc ++ ;
7+ const timeEventName = pluginName + "/" + type + "/" + method ;
8+ // we don't know if there's going to be a callback applied to a particular
9+ // call, so we just set it multiple times, letting each one override the last
10+ let endCallCount = 0 ;
11+ const addEndEvent = ( ) => {
12+ endCallCount ++ ;
13+ smp . addTimeEvent ( "plugins" , timeEventName , "end" , { id } ) ;
14+ } ;
15+
916 smp . addTimeEvent ( "plugins" , timeEventName , "start" , {
1017 id,
1118 name : pluginName ,
1219 } ) ;
13- const ret = func . apply ( this , args . map ( a => wrap ( a , pluginName , smp ) ) ) ;
14- smp . addTimeEvent ( "plugins" , timeEventName , "end" , { id } ) ;
20+ const ret = func . apply (
21+ this ,
22+ args . map ( a => wrap ( a , pluginName , smp , addEndEvent ) )
23+ ) ;
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 ( ) ;
29+
1530 return ret ;
1631 } ;
1732
@@ -27,27 +42,52 @@ const construcNamesToWrap = [
2742 "ContextModuleFactory" ,
2843] ;
2944
30- const wrap = ( orig , pluginName , smp ) => {
31- const origConstrucName = orig && orig . constructor && orig . constructor . name ;
32- const shouldWrap = construcNamesToWrap . includes ( origConstrucName ) ;
33- if ( ! shouldWrap ) return orig ;
45+ const wrap = ( orig , pluginName , smp , addEndEvent ) => {
46+ if ( ! orig ) return orig ;
47+
48+ const getOrigConstrucName = target =>
49+ target && target . constructor && target . constructor . name ;
50+ const getShouldWrap = target => {
51+ const origConstrucName = getOrigConstrucName ( target ) ;
52+ return construcNamesToWrap . includes ( origConstrucName ) ;
53+ } ;
54+ const shouldWrap = getShouldWrap ( orig ) ;
55+ const shouldSoftWrap = Object . keys ( orig )
56+ . map ( k => orig [ k ] )
57+ . some ( getShouldWrap ) ;
58+
59+ if ( ! shouldWrap && ! shouldSoftWrap ) {
60+ const vanillaFunc = orig . name === "next" ;
61+ return vanillaFunc
62+ ? function ( ) {
63+ // do this before calling the callback, since the callback can start
64+ // the next plugin step
65+ addEndEvent ( ) ;
66+
67+ return orig . apply ( this , arguments ) ;
68+ }
69+ : orig ;
70+ }
3471
3572 const proxy = new Proxy ( orig , {
3673 get : ( target , property ) => {
37- if ( property === "plugin" )
38- return genPluginMethod ( orig , pluginName , smp , origConstrucName ) . bind (
39- proxy
40- ) ;
74+ if ( shouldWrap && property === "plugin" )
75+ return genPluginMethod (
76+ orig ,
77+ pluginName ,
78+ smp ,
79+ getOrigConstrucName ( target )
80+ ) . bind ( proxy ) ;
4181
4282 if ( typeof orig [ property ] === "function" )
4383 return orig [ property ] . bind ( proxy ) ;
4484 return wrap ( orig [ property ] , pluginName , smp ) ;
4585 } ,
4686 set : ( target , property , value ) => {
47- target [ property ] = value ;
87+ return Reflect . set ( target , property , value ) ;
4888 } ,
4989 deleteProperty : ( target , property ) => {
50- delete target [ property ] ;
90+ return Reflect . deleteProperty ( target , property ) ;
5191 } ,
5292 } ) ;
5393
0 commit comments