https://github.com/devaman/importable
Safely lazy load and initialize packages across your app
https://github.com/devaman/importable
Last synced: about 2 months ago
JSON representation
Safely lazy load and initialize packages across your app
- Host: GitHub
- URL: https://github.com/devaman/importable
- Owner: devaman
- License: mit
- Created: 2019-03-03T20:56:42.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-03-03T20:50:34.000Z (over 7 years ago)
- Last Synced: 2025-03-12T19:32:35.270Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 2.68 MB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# importable
[](https://www.npmjs.com/package/importable)
Safely lazy load and initialize packages across your app
## Why?
Lazy loading is great, but as soon as you need to do anything more than import a single package, it can get messy. With Importable, you can import the packages you need, initialize them, and use them like normal; let Importable take care of the rest.
## Usage
Install using your favorite package manager
```bash
yarn add importable
```
Create an Importable package
```js
import Importable from 'importable';
const LazyPackage = new Importable(
// import the packages you need
[import('lazy-package'), import('lazy-config')],
// initialize them
async modules => {
const [lazyPackage, lazyConfig] = modules;
lazyPackage.initialize({ apiKey: lazyConfig.apiKey });
},
// and return them ready to use
async modules => {
const [lazyPackage] = modules;
return lazyPackage;
},
);
export default LazyPackage;
```
Import your package with `await` and use it like normal
```js
import LazyPackage from '../importables/LazyPackage';
const asyncFunction = async () => {
// get your package
const lazyPackage = await LazyPackage.import();
// use it like normal
lazyPackage.doSomething();
};
// and do it as often as you like
asyncFunction();
asyncFunction();
asyncFunction();
```
See the [test cases](https://github.com/michaelwm/importable/blob/master/src/Importable.test.js) for more in depth usage.