Я строю структуру проекта, и я создавал несколько модулей, в которые мне нужно было импортировать в соответствии с маршрутом, по которому идет пользователь.
# my folder structure
modules
-- user
-- client
-- index
# my code
// get constructor
const const constructor = await getConstructor( 'user' ); // get the constructor
// index
export const getConstructor = async ( module ) => {
const constructor = await require(`./${module}`).create; // option 1
const constructor = await import(`./${module}`).then( constructor => constructor.create ); // option 2
return constructor;
}
// module - user
const create = ( data ) => {
// behavior
// ...
}
export {
create,
delete,
otherFunctions
}
Мой вопрос - как лучше всего с точки зрения производительности, для динамического импорта функции create
, будь то вариант 1 или 2, или даже если есть другой способ.
Любое предложение?