You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

readme.md 1.9KB

4 anni fa
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # import-lazy [![Build Status](https://travis-ci.org/sindresorhus/import-lazy.svg?branch=master)](https://travis-ci.org/sindresorhus/import-lazy)
  2. > Import modules lazily
  3. ## Install
  4. ```
  5. $ npm install --save import-lazy
  6. ```
  7. ## Usage
  8. ```js
  9. // Pass in `require` or a custom import function
  10. const importLazy = require('import-lazy')(require);
  11. const _ = importLazy('lodash');
  12. // Where you would normally do
  13. _.isNumber(2);
  14. // You now instead call it as a function
  15. _().isNumber(2);
  16. // It's cached on consecutive calls
  17. _().isString('unicorn');
  18. // Extract lazy variations of the props you need
  19. const members = importLazy('lodash')('isNumber', 'isString');
  20. // Useful when using destructuring assignment in ES2015
  21. const {isNumber, isString} = importLazy('lodash')('isNumber', 'isString');
  22. // Works out of the box for functions and regular properties
  23. const stuff = importLazy('./math-lib')('sum', 'PHI');
  24. console.log(stuff.sum(1, 2)); // => 3
  25. console.log(stuff.PHI); // => 1.618033
  26. ```
  27. ### Proxy support in Node.js 6 or later
  28. 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.
  29. ```js
  30. const importLazy = require('import-lazy').proxy(require);
  31. const _ = importLazy('lodash');
  32. // No need to call it as a function but still lazily imported
  33. _.isNumber(2);
  34. ```
  35. ## Related
  36. - [resolve-from](https://github.com/sindresorhus/resolve-from) - Resolve the path of a module from a given path
  37. - [import-from](https://github.com/sindresorhus/import-from) - Import a module from a given path
  38. - [resolve-pkg](https://github.com/sindresorhus/resolve-pkg) - Resolve the path of a package regardless of it having an entry point
  39. - [lazy-value](https://github.com/sindresorhus/lazy-value) - Create a lazily evaluated value
  40. - [define-lazy-prop](https://github.com/sindresorhus/define-lazy-prop) - Define a lazily evaluated property on an object
  41. ## License
  42. MIT © [Sindre Sorhus](https://sindresorhus.com)