index.js 859 B

123456789101112131415161718192021222324252627
  1. 'use strict';
  2. const lazy = (importedModule, importFn, moduleId) =>
  3. importedModule === undefined ? importFn(moduleId) : importedModule;
  4. module.exports = importFn => {
  5. return moduleId => {
  6. let importedModule;
  7. const handler = {
  8. get: (target, property) => {
  9. importedModule = lazy(importedModule, importFn, moduleId);
  10. return Reflect.get(importedModule, property);
  11. },
  12. apply: (target, thisArgument, argumentsList) => {
  13. importedModule = lazy(importedModule, importFn, moduleId);
  14. return Reflect.apply(importedModule, thisArgument, argumentsList);
  15. },
  16. construct: (target, argumentsList) => {
  17. importedModule = lazy(importedModule, importFn, moduleId);
  18. return Reflect.construct(importedModule, argumentsList);
  19. }
  20. };
  21. // eslint-disable-next-line prefer-arrow-callback
  22. return new Proxy(function () {}, handler);
  23. };
  24. };