root 3bf41bf355 update | há 1 ano atrás | |
---|---|---|
.. | ||
index.d.ts | há 1 ano atrás | |
index.js | há 1 ano atrás | |
license | há 1 ano atrás | |
package.json | há 1 ano atrás | |
readme.md | há 1 ano atrás |
Import a module lazily
$ npm install import-lazy
// 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
While you may be tempted to do leverage destructuring, like this:
const {isNumber, isString} = importLazy('lodash');
Note that this will cause immediate property access, negating the lazy loading, and is equivalent to:
import {isNumber, isString} from 'lodash';
MIT © Sindre Sorhus