@@ -14,12 +14,18 @@ const RollupConfigContainer = require('./RollupConfigContainer');
1414 * @return {Promise<any> }
1515 */
1616async function _callAsyncHook ( plugin , hook , args ) {
17- if ( typeof plugin . execute [ hook ] === 'string' ) {
18- return plugin . execute [ hook ] ;
17+ let handler = plugin . execute [ hook ] ;
18+
19+ if ( typeof handler === 'string' ) {
20+ return handler ;
21+ }
22+
23+ if ( typeof handler === 'object' ) {
24+ handler = handler . handler ;
1925 }
2026
21- if ( plugin . execute [ hook ] ) {
22- let hr = plugin . execute [ hook ] . apply ( plugin . context , args ) ;
27+ if ( handler ) {
28+ let hr = handler . apply ( plugin . context , args ) ;
2329
2430 if ( hr instanceof Promise ) {
2531 return ( await plugin . error . wrapAsync ( hr ) ) ;
@@ -36,11 +42,29 @@ async function _callAsyncHook (plugin, hook, args) {
3642 * @return {any }
3743 */
3844function _callSyncHook ( plugin , hook , args ) {
39- if ( plugin . execute [ hook ] ) {
40- return plugin . execute [ hook ] . apply ( plugin . context , args ) ;
45+ let handler = plugin . execute [ hook ] ;
46+
47+ if ( typeof handler === 'object' ) {
48+ handler = handler . handler ;
49+ }
50+
51+ if ( handler ) {
52+ return handler . apply ( plugin . context , args ) ;
4153 }
4254}
4355
56+ function _getSortedPlugins ( plugins , hook ) {
57+ plugins = plugins . slice ( 0 ) ;
58+
59+ return plugins . filter ( p => {
60+ return typeof p . execute [ hook ] === 'object' && p . execute [ hook ] . order === 'pre' ;
61+ } ) . concat ( plugins . filter ( p => {
62+ return typeof p . execute [ hook ] === 'function' || typeof p . execute [ hook ] === 'string' || ( typeof p . execute [ hook ] === 'object' && ! p . execute [ hook ] . order ) ;
63+ } ) ) . concat ( plugins . filter ( p => {
64+ return typeof p . execute [ hook ] === 'object' && p . execute [ hook ] . order === 'post' ;
65+ } ) ) ;
66+ }
67+
4468/**
4569 * @param {PluginContainer } container
4670 * @param {string } hook
@@ -50,8 +74,10 @@ function _callSyncHook (plugin, hook, args) {
5074async function callAsyncFirstHook ( container , hook , args ) {
5175 // hook may return a promise.
5276 // waits for hook to return value other than null or undefined.
53- for ( let i = 0 ; i < container . __plugins . length ; i ++ ) {
54- let hr = await _callAsyncHook ( container . __plugins [ i ] , hook , args ) ;
77+ let plugins = _getSortedPlugins ( container . __plugins , hook ) ;
78+
79+ for ( let i = 0 ; i < plugins . length ; i ++ ) {
80+ let hr = await _callAsyncHook ( plugins [ i ] , hook , args ) ;
5581
5682 if ( hr !== null && hr !== undefined ) {
5783 return hr ;
@@ -70,11 +96,12 @@ async function callAsyncFirstHook (container, hook, args) {
7096async function callAsyncSequentialHook ( container , hook , toArgs , fromResult , start ) {
7197 // hook may return a promise.
7298 // all plugins that implement this hook will run, passing data onwards
99+ let plugins = _getSortedPlugins ( container . __plugins , hook ) ;
73100 let output = start ;
74101
75- for ( let i = 0 ; i < container . __plugins . length ; i ++ ) {
102+ for ( let i = 0 ; i < plugins . length ; i ++ ) {
76103 let args = toArgs ( output ) ;
77- let hr = await _callAsyncHook ( container . __plugins [ i ] , hook , args ) ;
104+ let hr = await _callAsyncHook ( plugins [ i ] , hook , args ) ;
78105
79106 if ( hr !== null && hr !== undefined ) {
80107 output = fromResult ( hr , output ) ;
@@ -95,12 +122,25 @@ async function callAsyncParallelHook (container, hook, args) {
95122 // all hooks are executed at the same time without waiting
96123 // will wait for all hooks to complete before returning
97124 let hookResults = [ ] ;
125+ let plugins = _getSortedPlugins ( container . __plugins , hook ) ;
126+ let previous = [ ] ;
127+
128+ for ( let i = 0 ; i < plugins . length ; i ++ ) {
129+ if ( typeof plugins [ i ] . execute [ hook ] === 'object' && plugins [ i ] . execute [ hook ] . sequential ) {
130+ let values = await Promise . all ( previous ) ;
131+ hookResults . push ( ...values ) ;
132+ previous = [ ] ;
133+ let v = await _callAsyncHook ( plugins [ i ] , hook , args ) ;
134+ hookResults . push ( v ) ;
135+ continue ;
136+ }
98137
99- for ( let i = 0 ; i < container . __plugins . length ; i ++ ) {
100- hookResults . push ( _callAsyncHook ( container . __plugins [ i ] , hook , args ) ) ;
138+ previous . push ( _callAsyncHook ( plugins [ i ] , hook , args ) ) ;
101139 }
102140
103- return Promise . all ( hookResults ) ;
141+ let values = await Promise . all ( previous ) ;
142+ hookResults . push ( ...values ) ;
143+ return hookResults ;
104144}
105145
106146/**
@@ -110,9 +150,11 @@ async function callAsyncParallelHook (container, hook, args) {
110150 * @return {any }
111151 */
112152function callSyncFirstHook ( container , hook , args ) {
153+ let plugins = _getSortedPlugins ( container . __plugins , hook ) ;
154+
113155 // waits for hook to return value other than null of undefined
114- for ( let i = 0 ; i < container . __plugins . length ; i ++ ) {
115- let hr = _callSyncHook ( container . __plugins [ i ] , hook , args ) ;
156+ for ( let i = 0 ; i < plugins . length ; i ++ ) {
157+ let hr = _callSyncHook ( plugins [ i ] , hook , args ) ;
116158
117159 if ( hr !== null && hr !== undefined ) {
118160 return hr ;
@@ -128,10 +170,11 @@ function callSyncFirstHook (container, hook, args) {
128170 */
129171function callSyncSequentialHook ( container , hook , args ) {
130172 // all plugins that implement this hook will run, passing data onwards
173+ let plugins = _getSortedPlugins ( container . __plugins , hook ) ;
131174 let output = args [ 0 ] ;
132175
133- for ( let i = 0 ; i < container . __plugins . length ; i ++ ) {
134- let hr = _callSyncHook ( container . __plugins [ i ] , hook , [ output ] ) ;
176+ for ( let i = 0 ; i < plugins . length ; i ++ ) {
177+ let hr = _callSyncHook ( plugins [ i ] , hook , [ output ] ) ;
135178 if ( hr !== null && hr !== undefined ) {
136179 output = hr ;
137180 }
0 commit comments