JSPM

lazreq

0.1.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3
  • Score
    100M100P100Q22890F
  • License ISC

Lazy require / require on demand while keeping dependencies in module headers

Package Exports

  • lazreq

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (lazreq) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

lazreq

Lazy require implementation

Goal

Load modules only when they are actually needed WHILE keeping dependencies listed on top of your files

Why

In a bigger project with a lot of modules or with big modules, initialization time may get increased if all modules get loaded and parsed before they are needed or even without them being needed at all (like conditionally used modules)

How to

Install

npm install lazreq

Use

JavaScript

var req = require('lazreq')({
  fs: 'fs',
  myLib: './lib/my-lib.js'
});

module.exports = function () {
  req.fs.exists('file_to_check.txt', function (exists) {
    if (exists) {
      req.myLib.amazingStuff();
    }
  });
}

CoffeeScript

req = require('lazreq')
  fs:    'fs'
  myLib: './lib/my-lib.js'

module.exports = ->
  req.fs.exists 'file_to_check.txt', (exists) ->
    if exists
      req.myLib.amazingStuff();

FAQ

How it works

It creates getters (using JavaScript standard standard Object.defineProperty methods) that will trigger loading modules on first read and return cached modules from the second read on

How do relative pathes work?

Just the way you would expect from require(): node_modules and pathes starting with './' or '../' will be looked up relative to the module file requiring them.