2017-08-14 05:01:11 +02:00
|
|
|
# import-lazy [![Build Status](https://travis-ci.org/sindresorhus/import-lazy.svg?branch=master)](https://travis-ci.org/sindresorhus/import-lazy)
|
2017-05-28 00:38:50 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
> Import modules lazily
|
2017-05-28 00:38:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
## Install
|
|
|
|
|
|
|
|
```
|
2017-08-14 05:01:11 +02:00
|
|
|
$ npm install --save import-lazy
|
2017-05-28 00:38:50 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
```js
|
2017-08-14 05:01:11 +02:00
|
|
|
// Pass in `require` or a custom import function
|
|
|
|
const importLazy = require('import-lazy')(require);
|
|
|
|
const _ = importLazy('lodash');
|
2017-05-28 00:38:50 +02:00
|
|
|
|
|
|
|
// Where you would normally do
|
|
|
|
_.isNumber(2);
|
|
|
|
|
|
|
|
// You now instead call it as a function
|
|
|
|
_().isNumber(2);
|
|
|
|
|
|
|
|
// It's cached on consecutive calls
|
|
|
|
_().isString('unicorn');
|
|
|
|
|
|
|
|
// Extract lazy variations of the props you need
|
2017-08-14 05:01:11 +02:00
|
|
|
const members = importLazy('lodash')('isNumber', 'isString');
|
2017-05-28 00:38:50 +02:00
|
|
|
|
|
|
|
// Useful when using destructuring assignment in ES2015
|
2017-08-14 05:01:11 +02:00
|
|
|
const {isNumber, isString} = importLazy('lodash')('isNumber', 'isString');
|
2017-05-28 00:38:50 +02:00
|
|
|
|
|
|
|
// Works out of the box for functions and regular properties
|
2017-08-14 05:01:11 +02:00
|
|
|
const stuff = importLazy('./math-lib')('sum', 'PHI');
|
2017-05-28 00:38:50 +02:00
|
|
|
console.log(stuff.sum(1, 2)); // => 3
|
|
|
|
console.log(stuff.PHI); // => 1.618033
|
|
|
|
```
|
|
|
|
|
|
|
|
### Proxy support in Node.js 6 or later
|
|
|
|
|
|
|
|
If you use Node.js 6 or later, you can take advantage of ES2015 proxies and don't need to call it as a function.
|
|
|
|
|
|
|
|
```js
|
2017-08-14 05:01:11 +02:00
|
|
|
const importLazy = require('import-lazy').proxy(require);
|
|
|
|
const _ = importLazy('lodash');
|
2017-05-28 00:38:50 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
// No need to call it as a function but still lazily imported
|
2017-05-28 00:38:50 +02:00
|
|
|
_.isNumber(2);
|
|
|
|
```
|
|
|
|
|
|
|
|
## Related
|
|
|
|
|
|
|
|
- [resolve-from](https://github.com/sindresorhus/resolve-from) - Resolve the path of a module from a given path
|
2017-08-14 05:01:11 +02:00
|
|
|
- [import-from](https://github.com/sindresorhus/import-from) - Import a module from a given path
|
2017-05-28 00:38:50 +02:00
|
|
|
- [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
|
|
|
|
|
|
|
|
|
|
|
|
## License
|
|
|
|
|
|
|
|
MIT © [Sindre Sorhus](https://sindresorhus.com)
|