0.2.0 • Published 3 years ago
@istok/lazy-load v0.2.0
@istok/lazy-load
Load all objects from async map.
loadMap(map: AsyncLoadMap | undefined): Promise<ResolvedMap>
Aawits for all promises in the map. Maps structure of form:
const map = {
a: () => asyncLoadA(),
b: () => asyncLoadB(),
};
to:
const resolvedMap = {
a: awaitedA,
b: awaitedB,
};
Usage
const map = {
a: () => Promise.resolve('a is loaded'),
b: () => Promise.resolve('b is loaded'),
};
const resolvedMap = await loadMap(map);
makeLoadMap(elements: string[], loader: (element: string) => Promise)
Usefull for dynamic imports of an array of files when using Webpack. Webpack can not resolve dynamic imports without static first part in the path. This will throw an error:
const PATH = '../components/';
const component = 'a';
import(PATH + component);
While this is okay:
const component = 'a';
import('../components/' + component);
Usage:
const map = makeLoadMap(['Button', 'Input'], component => import('../components/' + component).then(m => m.default));