@fontoxml/fontoxml-development-tools
Version:
Development tools for Fonto.
23 lines (20 loc) • 716 B
JavaScript
import path from 'path';
import { pathToFileURL } from 'url';
/**
* Factory for a lazy loaded function that lazily imports a module from an absolute path.
*
* @param {string} absolutePath The absolute path of the module to be lazily imported.
*
* @return {(...args: unknown[]) => Promise<unknown>} The lazy loaded function wrapper.
*/
export default function createLazyLoadedFunction(absolutePath) {
if (!path.isAbsolute(absolutePath)) {
throw new Error(
`"${absolutePath}" should be registered using an absolute path instead of a relative one.`,
);
}
return async (...args) => {
const loadedFunction = (await import(pathToFileURL(absolutePath))).default;
return loadedFunction(...args);
};
}