1'use strict'; 2const lazy = (mod, fn, id) => mod === undefined ? fn(id) : mod; 3 4module.exports = fn => { 5 return id => { 6 let mod; 7 8 return function () { 9 if (arguments.length === 0) { 10 mod = lazy(mod, fn, id); 11 return mod; 12 } 13 14 const ret = {}; 15 16 [].forEach.call(arguments, prop => { 17 Object.defineProperty(ret, prop, { 18 get: () => { 19 mod = lazy(mod, fn, id); 20 if (typeof mod[prop] === 'function') { 21 return function () { 22 return mod[prop].apply(mod, arguments); 23 }; 24 } 25 26 return mod[prop]; 27 } 28 }); 29 }); 30 31 return ret; 32 }; 33 }; 34}; 35 36module.exports.proxy = fn => { 37 return id => { 38 let mod; 39 40 const handler = { 41 get: (target, property) => { 42 mod = lazy(mod, fn, id); 43 return Reflect.get(mod, property); 44 }, 45 apply: (target, thisArg, argumentsList) => { 46 mod = lazy(mod, fn, id); 47 return Reflect.apply(mod, thisArg, argumentsList); 48 } 49 }; 50 51 return new Proxy(() => {}, handler); 52 }; 53}; 54