Skip to content

Vizel API / core/src / createLazyLoader

Function: createLazyLoader()

ts
function createLazyLoader<T>(moduleName, importFn): () => Promise<T>;

Defined in: packages/core/src/utils/lazy-import.ts:29

Creates a lazy loader for an optional dependency. Caches the loaded module to avoid repeated dynamic imports.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
moduleNamestringThe npm package name (used in error messages)
importFn() => Promise&lt;T&gt;A function that performs the dynamic import

Returns

A function that returns a Promise of the loaded module

() => Promise&lt;T&gt;

Example

ts
const loadMermaid = createLazyLoader("mermaid", async () => {
  const mod = await import("mermaid");
  return mod.default;
});

// Later, when needed:
const mermaid = await loadMermaid();

Released under the MIT License.