@@ -9,7 +9,13 @@ import emptyResponseHandler from 'oc-empty-response-handler';
99import { fromPromise } from 'universalify' ;
1010import strings from '../../../resources' ;
1111import settings from '../../../resources/settings' ;
12- import type { Component , Config , Template } from '../../../types' ;
12+ import type {
13+ Component ,
14+ Config ,
15+ PluginContext ,
16+ Plugins ,
17+ Template
18+ } from '../../../types' ;
1319import isTemplateLegacy from '../../../utils/is-template-legacy' ;
1420import eventsHandler from '../../domain/events-handler' ;
1521import NestedRenderer from '../../domain/nested-renderer' ;
@@ -62,12 +68,57 @@ const noopConsole = Object.fromEntries(
6268 Object . keys ( console ) . map ( ( key ) => [ key , noop ] )
6369) ;
6470
71+ /**
72+ * Converts the plugins to a function that returns a record of plugins with the context applied
73+ * Caches the plugins without context and applies the context to the plugins that need it
74+ * to avoid creating a new function for each component if possible
75+ * @param plugins - The plugins to convert
76+ * @returns A function that returns a record of plugins with the context applied
77+ */
78+ function pluginConverter ( plugins : Plugins = { } ) {
79+ const pluginsMap = {
80+ withoutContext : { } as Record < string , ( ...args : any [ ] ) => any > ,
81+ withContext : { } as Record <
82+ string ,
83+ ( ctx : PluginContext ) => ( ...args : any [ ] ) => any
84+ > ,
85+ needsContext : false
86+ } ;
87+ for ( const [ name , { handler, context } ] of Object . entries ( plugins ) ) {
88+ if ( context ) {
89+ pluginsMap . withContext [ name ] = handler as any ;
90+ pluginsMap . needsContext = true ;
91+ } else {
92+ pluginsMap . withoutContext [ name ] = handler ;
93+ }
94+ }
95+
96+ return ( ctx : PluginContext ) => {
97+ if ( ! pluginsMap . needsContext ) {
98+ return pluginsMap . withoutContext ;
99+ }
100+ const pluginsWithContextApplied = { } as Record <
101+ string ,
102+ ( ...args : any [ ] ) => any
103+ > ;
104+ for ( const [ name , handler ] of Object . entries ( pluginsMap . withContext ) ) {
105+ pluginsWithContextApplied [ name ] = handler ( ctx ) ;
106+ }
107+
108+ return {
109+ ...pluginsMap . withoutContext ,
110+ ...pluginsWithContextApplied
111+ } ;
112+ } ;
113+ }
114+
65115export default function getComponent ( conf : Config , repository : Repository ) {
66116 const client = Client ( { templates : conf . templates } ) ;
67117 const cache = new Cache ( {
68118 verbose : ! ! conf . verbosity ,
69119 refreshInterval : conf . refreshInterval
70120 } ) ;
121+ const convertPlugins = pluginConverter ( conf . plugins ) ;
71122
72123 const getEnv = async (
73124 component : Component
@@ -504,7 +555,10 @@ export default function getComponent(conf: Config, repository: Repository) {
504555 baseUrl : conf . baseUrl ,
505556 env : { ...conf . env , ...env } ,
506557 params,
507- plugins : conf . plugins ,
558+ plugins : convertPlugins ( {
559+ name : component . name ,
560+ version : component . version
561+ } ) ,
508562 renderComponent : fromPromise ( nestedRenderer . renderComponent ) ,
509563 renderComponents : fromPromise ( nestedRenderer . renderComponents ) ,
510564 requestHeaders : options . headers ,
0 commit comments