@@ -41,47 +41,50 @@ module.exports = {
4141
4242// returns plugin functions grouped by bucket and filtered by relevance (based
4343// on configuration)
44- function pluginsByBucket ( config , defaults ) {
45- let plugins = determinePlugins ( config . plugins , defaults ) ;
44+ async function pluginsByBucket ( config , defaults ) {
45+ let plugins = await determinePlugins ( config . plugins , defaults ) ;
4646 let buckets = [ ...BUCKETS ] . reduce ( ( memo , bucket ) => {
4747 memo [ bucket ] = [ ] ;
4848 return memo ;
4949 } , { } ) ;
50- Object . entries ( plugins ) . forEach ( ( [ key , _plugin ] ) => {
50+ for ( let [ key , _plugin ] of Object . entries ( plugins ) ) {
5151 let pluginConfig = config [ key ] ;
5252 if ( ! pluginConfig ) {
53- return ;
53+ continue ;
5454 }
5555
5656 let { bucket, plugin } = _plugin ;
57+ if ( ! plugin . call ) {
58+ ( { plugin } = await loadPlugin ( plugin ) ) ;
59+ }
5760 buckets [ bucket ] . push ( {
58- plugin : plugin . call ? plugin : loadPlugin ( plugin ) . plugin ,
61+ plugin,
5962 config : pluginConfig
6063 } ) ;
61- } ) ;
64+ }
6265 return buckets ;
6366}
6467
6568// `plugins` is an array of plugins, each either a package identifier or a
6669// `{ key, bucket, plugin }` object`, with `key` being the configuration key and
6770// `plugin` being either a function or a package identifier
68- function determinePlugins ( plugins = [ ] , defaults = DEFAULTS ) {
71+ async function determinePlugins ( plugins = [ ] , defaults = DEFAULTS ) {
6972 let registry = { } ;
7073 // NB: default plugins are resolved lazily because eager loading would
7174 // result in them becoming a hard dependency rather than a convenience
7275 // preset - however, that requires us to duplicate the respective
7376 // configuration keys and buckets here
74- defaults . forEach ( plugin => {
75- registerPlugin ( registry , plugin , false ) ;
76- } ) ;
77- plugins . forEach ( plugin => {
78- registerPlugin ( registry , plugin , true ) ;
79- } ) ;
77+ for ( let plugin of defaults ) {
78+ await registerPlugin ( registry , plugin , false ) ;
79+ }
80+ for ( let plugin of plugins ) {
81+ await registerPlugin ( registry , plugin , true ) ;
82+ }
8083 return registry ;
8184}
8285
83- function registerPlugin ( registry , _plugin , eager ) {
84- let { key, bucket, plugin } = resolvePlugin ( _plugin , eager ) ;
86+ async function registerPlugin ( registry , _plugin , eager ) {
87+ let { key, bucket, plugin } = await resolvePlugin ( _plugin , eager ) ;
8588 // NB: default plugins may be overridden
8689 if ( registry [ key ] && ! DEFAULT_KEYS . has ( key ) ) {
8790 abort ( `ERROR: duplicate plugin key ${ repr ( key , false ) } ` ) ;
@@ -92,14 +95,14 @@ function registerPlugin(registry, _plugin, eager) {
9295 }
9396}
9497
95- function resolvePlugin ( _plugin , eager ) {
98+ async function resolvePlugin ( _plugin , eager ) {
9699 if ( _plugin . substr ) { // package identifier
97- _plugin = loadPlugin ( _plugin ) ;
100+ _plugin = await loadPlugin ( _plugin ) ;
98101 }
99102
100103 let { key, bucket, plugin } = _plugin ;
101104 if ( eager && plugin . substr && ( ! key || ! bucket ) ) { // auto-configuration
102- let _plugin = loadPlugin ( plugin ) ;
105+ let _plugin = await loadPlugin ( plugin ) ;
103106 plugin = _plugin . plugin ;
104107 // local configuration takes precedence
105108 key = key || _plugin . key ;
@@ -112,13 +115,13 @@ function resolvePlugin(_plugin, eager) {
112115 return { key, bucket, plugin } ;
113116}
114117
115- function loadPlugin ( pkg ) {
118+ async function loadPlugin ( pkg ) {
116119 let fail = prop => abort ( `ERROR: invalid plugin ${ repr ( pkg ) } ; ` +
117120 `missing ${ repr ( prop , false ) } ` ) ;
118121 let {
119122 key = fail ( "key" ) ,
120123 bucket = fail ( "bucket" ) ,
121124 plugin = fail ( "plugin" )
122- } = loadExtension ( pkg , "ERROR: missing plugin" ) ;
125+ } = await loadExtension ( pkg , "ERROR: missing plugin" ) ;
123126 return { key, bucket, plugin } ;
124127}
0 commit comments