import-lazy
Advanced tools
Comparing version 3.1.0 to 4.0.0
23
index.js
'use strict'; | ||
const lazy = (mod, fn, id) => mod === undefined ? fn(id) : mod; | ||
const lazy = (importedModule, importFn, moduleId) => | ||
importedModule === undefined ? importFn(moduleId) : importedModule; | ||
module.exports = fn => { | ||
return id => { | ||
let mod; | ||
module.exports = importFn => { | ||
return moduleId => { | ||
let importedModule; | ||
const handler = { | ||
get: (target, property) => { | ||
mod = lazy(mod, fn, id); | ||
return Reflect.get(mod, property); | ||
importedModule = lazy(importedModule, importFn, moduleId); | ||
return Reflect.get(importedModule, property); | ||
}, | ||
apply: (target, thisArg, argumentsList) => { | ||
mod = lazy(mod, fn, id); | ||
return Reflect.apply(mod, thisArg, argumentsList); | ||
apply: (target, thisArgument, argumentsList) => { | ||
importedModule = lazy(importedModule, importFn, moduleId); | ||
return Reflect.apply(importedModule, thisArgument, argumentsList); | ||
}, | ||
construct: (target, argumentsList) => { | ||
mod = lazy(mod, fn, id); | ||
return Reflect.construct(mod, argumentsList); | ||
importedModule = lazy(importedModule, importFn, moduleId); | ||
return Reflect.construct(importedModule, argumentsList); | ||
} | ||
@@ -21,0 +22,0 @@ }; |
{ | ||
"name": "import-lazy", | ||
"version": "3.1.0", | ||
"version": "4.0.0", | ||
"description": "Import a module lazily", | ||
@@ -13,9 +13,10 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=6" | ||
"node": ">=8" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
"test": "xo && ava && tsd" | ||
}, | ||
"files": [ | ||
"index.js" | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
@@ -36,5 +37,6 @@ "keywords": [ | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
"ava": "^1.4.1", | ||
"tsd": "^0.7.2", | ||
"xo": "^0.24.0" | ||
} | ||
} |
@@ -5,5 +5,3 @@ # import-lazy [![Build Status](https://travis-ci.org/sindresorhus/import-lazy.svg?branch=master)](https://travis-ci.org/sindresorhus/import-lazy) | ||
Note: Version 3 is exclusively `Proxy`-based and requires Node.js 6+. Use [version 2](https://github.com/sindresorhus/import-lazy/tree/ed6c2fac31aaf8a7d91a27295756383487f3965d) if you need Node.js <=5 support. | ||
## Install | ||
@@ -29,5 +27,2 @@ | ||
// It also works using destructuring assignment in ES2015 | ||
const {isNumber, isString} = importLazy('lodash'); | ||
// Works out of the box for functions and regular properties | ||
@@ -39,3 +34,16 @@ const stuff = importLazy('./math-lib'); | ||
### Warning: 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'; | ||
``` | ||
## Related | ||
@@ -42,0 +50,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4898
5
45
58
3