UNPKG

@sap/cds-compiler

Version:

CDS (Core Data Services) compiler and backends

38 lines (33 loc) 1.12 kB
// Provide some on-demand `require` // To use: // const lazyload = require('./base/lazyload')( module ); // const My = lazyload('../My/Module'); // replaces `require('../My/Module')` // // The required module must export an object. Do not def-struct, i.e. // const { Foo, bar } = lazyload('../My/Module'); // DO NOT DO THIS 'use strict'; function createLazyload( callingModule ) { /** * Load the module on-demand and not immediately. * * @param {string} moduleName Name of the module to load - like with require * @returns {object} A Proxy that handles the on-demand loading */ return function lazyload( moduleName ) { let module; return new Proxy(((...args) => { if (!module) module = callingModule.require(moduleName); if (module.apply && typeof module.apply === 'function') return module.apply(this, args); return module; // for destructured calls }), { get(target, name) { if (!module) module = callingModule.require(moduleName); return module[name]; }, }); }; } module.exports = createLazyload;