pax_global_header00006660000000000000000000000064134610307140014511gustar00rootroot0000000000000052 comment=eb40edcf6242280c68289758db9c22c5ecb455d6 import-lazy-4.0.0/000077500000000000000000000000001346103071400140015ustar00rootroot00000000000000import-lazy-4.0.0/.editorconfig000066400000000000000000000002571346103071400164620ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 import-lazy-4.0.0/.gitattributes000066400000000000000000000000231346103071400166670ustar00rootroot00000000000000* text=auto eol=lf import-lazy-4.0.0/.gitignore000066400000000000000000000000271346103071400157700ustar00rootroot00000000000000node_modules yarn.lock import-lazy-4.0.0/.npmrc000066400000000000000000000000231346103071400151140ustar00rootroot00000000000000package-lock=false import-lazy-4.0.0/.travis.yml000066400000000000000000000000651346103071400161130ustar00rootroot00000000000000language: node_js node_js: - '12' - '10' - '8' import-lazy-4.0.0/fixtures/000077500000000000000000000000001346103071400156525ustar00rootroot00000000000000import-lazy-4.0.0/fixtures/baz.js000066400000000000000000000000741346103071400167650ustar00rootroot00000000000000'use strict'; module.exports = (ho, ge) => `baz${ho}${ge}`; import-lazy-4.0.0/fixtures/class.js000066400000000000000000000001521346103071400173130ustar00rootroot00000000000000'use strict'; module.exports = class TestClass { constructor(message) { this.message = message; } }; import-lazy-4.0.0/fixtures/fail.js000066400000000000000000000000761346103071400171260ustar00rootroot00000000000000'use strict'; require('assert')(false, 'require is delayed'); import-lazy-4.0.0/fixtures/foo.bar.js000066400000000000000000000001761346103071400175420ustar00rootroot00000000000000'use strict'; module.exports.foo = () => 'foo'; module.exports.bar = (ho, ge) => `bar${ho}${ge}`; module.exports.baz = 'baz'; import-lazy-4.0.0/fixtures/foo.js000066400000000000000000000000541346103071400167720ustar00rootroot00000000000000'use strict'; module.exports = () => 'foo'; import-lazy-4.0.0/index.d.ts000066400000000000000000000012001346103071400156730ustar00rootroot00000000000000/** Import a module lazily. @example ``` // Pass in `require` or a custom import function import importLazy = require('import-lazy'); const _ = importLazy(require)('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(require)('./math-lib'); console.log(stuff.sum(1, 2)); // => 3 console.log(stuff.PHI); // => 1.618033 ``` */ declare function importLazy( importFn: (moduleId: string) => T ): (moduleId: string) => T; export = importLazy; import-lazy-4.0.0/index.js000066400000000000000000000015331346103071400154500ustar00rootroot00000000000000'use strict'; const lazy = (importedModule, importFn, moduleId) => importedModule === undefined ? importFn(moduleId) : importedModule; module.exports = importFn => { return moduleId => { let importedModule; const handler = { get: (target, property) => { importedModule = lazy(importedModule, importFn, moduleId); return Reflect.get(importedModule, property); }, apply: (target, thisArgument, argumentsList) => { importedModule = lazy(importedModule, importFn, moduleId); return Reflect.apply(importedModule, thisArgument, argumentsList); }, construct: (target, argumentsList) => { importedModule = lazy(importedModule, importFn, moduleId); return Reflect.construct(importedModule, argumentsList); } }; // eslint-disable-next-line prefer-arrow-callback return new Proxy(function () {}, handler); }; }; import-lazy-4.0.0/index.test-d.ts000066400000000000000000000002721346103071400166600ustar00rootroot00000000000000import {expectType} from 'tsd'; import importLazy = require('.'); expectType(importLazy(require)('lodash')); expectType(importLazy((moduleId: string) => 1)('lodash')); import-lazy-4.0.0/license000066400000000000000000000021251346103071400153460ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. import-lazy-4.0.0/package.json000066400000000000000000000011521346103071400162660ustar00rootroot00000000000000{ "name": "import-lazy", "version": "4.0.0", "description": "Import a module lazily", "license": "MIT", "repository": "sindresorhus/import-lazy", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=8" }, "scripts": { "test": "xo && ava && tsd" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "import", "require", "load", "module", "modules", "lazy", "lazily", "defer", "deferred", "proxy", "proxies" ], "devDependencies": { "ava": "^1.4.1", "tsd": "^0.7.2", "xo": "^0.24.0" } } import-lazy-4.0.0/readme.md000066400000000000000000000032101346103071400155540ustar00rootroot00000000000000# import-lazy [![Build Status](https://travis-ci.org/sindresorhus/import-lazy.svg?branch=master)](https://travis-ci.org/sindresorhus/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 ``` ### 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 - [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 ## License MIT © [Sindre Sorhus](https://sindresorhus.com) import-lazy-4.0.0/test.js000066400000000000000000000017321346103071400153210ustar00rootroot00000000000000import test from 'ava'; import importLazy from '.'; const importLazyBound = importLazy(require); test('main', t => { const foo = importLazyBound('./fixtures/foo'); t.is(foo(), 'foo'); const baz = importLazyBound('./fixtures/baz'); t.is(baz('j', 's'), 'bazjs'); }); test('lazy', t => { importLazyBound('./fixtures/fail'); t.pass(); }); test('props', t => { const object = importLazyBound('./fixtures/foo.bar.js'); t.is(object.foo(), 'foo'); t.is(object.bar('j', 's'), 'barjs'); t.is(object.baz, 'baz'); }); test('class', t => { const Class = importLazyBound('./fixtures/class.js'); let instance; t.notThrows(() => { instance = new Class('42'); }); t.true(instance instanceof Class); t.is(instance.message, '42'); }); test('destructuring makes it eager', t => { let invoked = false; const fakeRequire = () => { invoked = true; return {foo: 'bar'}; }; const {foo} = importLazy(fakeRequire)('fake-module-name'); t.is(foo, 'bar'); t.true(invoked); });