Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sindresorhus/import-lazy
Import a module lazily
https://github.com/sindresorhus/import-lazy
Last synced: about 1 month ago
JSON representation
Import a module lazily
- Host: GitHub
- URL: https://github.com/sindresorhus/import-lazy
- Owner: sindresorhus
- License: mit
- Created: 2014-08-16T15:56:46.000Z (about 10 years ago)
- Default Branch: main
- Last Pushed: 2022-07-11T08:45:05.000Z (over 2 years ago)
- Last Synced: 2024-04-14T09:52:38.516Z (7 months ago)
- Language: JavaScript
- Homepage:
- Size: 28.3 KB
- Stars: 247
- Watchers: 11
- Forks: 18
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- License: license
- Security: .github/security.md
Awesome Lists containing this project
- awesome-nodejs-cn - import-lazy - 懒加载一个模块 (包 / 其他)
- awesome-github-star - import-lazy
- awesome-nodejs - import-lazy - Import a module lazily - ★ 151 (Miscellaneous)
- awesome-node - import-lazy - Import a module lazily. (Packages / Miscellaneous)
README
# import-lazy
> Import a module lazily
## Install
```
$ npm install import-lazy
```## Usage
```js
// Pass in `require` or a custom import function
const importLazy = require('import-lazy')(require);
const _ = importLazy('lodash');// Instead of referring to its exported properties directly…
_.isNumber(2);// …it's cached on consecutive calls
_.isNumber('unicorn');// Works out of the box for functions and regular properties
const stuff = importLazy('./math-lib');
console.log(stuff.sum(1, 2)); // => 3
console.log(stuff.PHI); // => 1.618033
```## Note
### Destructuring will cause it to fetch eagerly
While you may be tempted to do leverage destructuring, like this:
```js
const {isNumber, isString} = importLazy('lodash');
```Note that this will cause immediate property access, negating the lazy loading, and is equivalent to:
```js
import {isNumber, isString} from 'lodash';
```### Usage with bundlers
If you're using a bundler, like Webpack, you'll have to [import your modules like this](https://github.com/webpack/webpack/issues/9155) in order to have them properly bundled:
```js
const importLazy = require('import-lazy');const _ = importLazy(() => require('lodash'))();
```## Related
- [resolve-from](https://github.com/sindresorhus/resolve-from) - Resolve the path of a module from a given path
- [import-from](https://github.com/sindresorhus/import-from) - Import a module from a given path
- [resolve-pkg](https://github.com/sindresorhus/resolve-pkg) - Resolve the path of a package regardless of it having an entry point
- [lazy-value](https://github.com/sindresorhus/lazy-value) - Create a lazily evaluated value
- [define-lazy-prop](https://github.com/sindresorhus/define-lazy-prop) - Define a lazily evaluated property on an object---
Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.