pax_global_header00006660000000000000000000000064131102612240014502gustar00rootroot0000000000000052 comment=ed6c2fac31aaf8a7d91a27295756383487f3965d import-lazy-2.1.0/000077500000000000000000000000001311026122400137715ustar00rootroot00000000000000import-lazy-2.1.0/.editorconfig000066400000000000000000000002571311026122400164520ustar00rootroot00000000000000root = 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-2.1.0/.gitattributes000066400000000000000000000000351311026122400166620ustar00rootroot00000000000000* text=auto *.js text eol=lf import-lazy-2.1.0/.gitignore000066400000000000000000000000151311026122400157550ustar00rootroot00000000000000node_modules import-lazy-2.1.0/.travis.yml000066400000000000000000000000671311026122400161050ustar00rootroot00000000000000sudo: false language: node_js node_js: - '6' - '4' import-lazy-2.1.0/fixtures/000077500000000000000000000000001311026122400156425ustar00rootroot00000000000000import-lazy-2.1.0/fixtures/baz.js000066400000000000000000000000741311026122400167550ustar00rootroot00000000000000'use strict'; module.exports = (ho, ge) => `baz${ho}${ge}`; import-lazy-2.1.0/fixtures/fail.js000066400000000000000000000000761311026122400171160ustar00rootroot00000000000000'use strict'; require('assert')(false, 'require is delayed'); import-lazy-2.1.0/fixtures/foo.bar.js000066400000000000000000000001761311026122400175320ustar00rootroot00000000000000'use strict'; module.exports.foo = () => 'foo'; module.exports.bar = (ho, ge) => `bar${ho}${ge}`; module.exports.baz = 'baz'; import-lazy-2.1.0/fixtures/foo.js000066400000000000000000000000541311026122400167620ustar00rootroot00000000000000'use strict'; module.exports = () => 'foo'; import-lazy-2.1.0/index.js000066400000000000000000000017201311026122400154360ustar00rootroot00000000000000'use strict'; const lazy = (mod, fn, id) => mod === undefined ? fn(id) : mod; module.exports = fn => { return id => { let mod; return function () { if (arguments.length === 0) { mod = lazy(mod, fn, id); return mod; } const ret = {}; [].forEach.call(arguments, prop => { Object.defineProperty(ret, prop, { get: () => { mod = lazy(mod, fn, id); if (typeof mod[prop] === 'function') { return function () { return mod[prop].apply(mod, arguments); }; } return mod[prop]; } }); }); return ret; }; }; }; module.exports.proxy = fn => { return id => { let mod; const handler = { get: (target, property) => { mod = lazy(mod, fn, id); return Reflect.get(mod, property); }, apply: (target, thisArg, argumentsList) => { mod = lazy(mod, fn, id); return Reflect.apply(mod, thisArg, argumentsList); } }; return new Proxy(() => {}, handler); }; }; import-lazy-2.1.0/license000066400000000000000000000021371311026122400153410ustar00rootroot00000000000000The MIT License (MIT) 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-2.1.0/package.json000066400000000000000000000013201311026122400162530ustar00rootroot00000000000000{ "name": "import-lazy", "version": "2.1.0", "description": "Import modules lazily", "license": "MIT", "repository": "sindresorhus/import-lazy", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "contributors": [ { "name": "Jorge Bucaran", "email": "jbucaran@me.com" } ], "engines": { "node": ">=4" }, "scripts": { "test": "xo && ava" }, "files": [ "index.js" ], "keywords": [ "import", "require", "load", "module", "modules", "lazy", "lazily", "defer", "deferred", "proxy", "proxies" ], "devDependencies": { "ava": "*", "xo": "*" } } import-lazy-2.1.0/readme.md000066400000000000000000000036371311026122400155610ustar00rootroot00000000000000# import-lazy [![Build Status](https://travis-ci.org/sindresorhus/import-lazy.svg?branch=master)](https://travis-ci.org/sindresorhus/import-lazy) > Import modules lazily ## Install ``` $ npm install --save import-lazy ``` ## Usage ```js // Pass in `require` or a custom import function const importLazy = require('import-lazy')(require); const _ = importLazy('lodash'); // 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 const members = importLazy('lodash')('isNumber', 'isString'); // Useful when using destructuring assignment in ES2015 const {isNumber, isString} = importLazy('lodash')('isNumber', 'isString'); // Works out of the box for functions and regular properties const stuff = importLazy('./math-lib')('sum', 'PHI'); 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 const importLazy = require('import-lazy').proxy(require); const _ = importLazy('lodash'); // No need to call it as a function but still lazily imported _.isNumber(2); ``` ## 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-2.1.0/test-proxy.js000066400000000000000000000010661311026122400164700ustar00rootroot00000000000000import test from 'ava'; import m from '.'; const testSkipNode4 = process.version.startsWith('v4') ? test.skip : test; const importLazy = m.proxy(require); testSkipNode4('main', t => { const f = importLazy('./fixtures/foo'); t.is(f(), 'foo'); const g = importLazy('./fixtures/baz'); t.is(g('j', 's'), 'bazjs'); }); testSkipNode4('lazy', t => { importLazy('./fixtures/fail'); t.pass(); }); testSkipNode4('props', t => { const obj = importLazy('./fixtures/foo.bar.js'); t.is(obj.foo(), 'foo'); t.is(obj.bar('j', 's'), 'barjs'); t.is(obj.baz, 'baz'); }); import-lazy-2.1.0/test.js000066400000000000000000000010141311026122400153020ustar00rootroot00000000000000import test from 'ava'; import m from '.'; const importLazy = m(require); test('main', t => { const f = importLazy('./fixtures/foo'); t.is(f(), f()); t.is(f()(), 'foo'); }); test('lazy', t => { // `require()` does not occur unless user tries to access `foo` importLazy('./fixtures/fail')('foo'); t.pass(); }); test('props', t => { const obj = importLazy('./fixtures/foo.bar.js')('foo', 'bar', 'baz'); t.is(obj.foo(), 'foo'); t.is(obj.foo(), 'foo'); t.is(obj.bar('j', 's'), 'barjs'); t.is(obj.baz, 'baz'); });