Narcoleptic's Patch Mangler

    require-with-global

    2.0.1 • Public • Published

    require-with-global

    inject a global variable into a module require() tree

    npm travis standard

    Install

    npm install require-with-global
    

    Usage

    var withGlobal = require('require-with-global')
    var uppercaseStream = require('through2')(function (chunk, enc, next) {
      next(null, chunk.toUpperCase())
    })
    var virtualConsole = require('console').Console(uppercaseStream)
    var r = withGlobal()
    // `console` in ./app.js and all child modules will be `virtualConsole`.
    var app = r('./app', {
      console: virtualConsole
    })
     
    app.run()
     
    uppercaseStream.pipe(process.stdout)
     
    r.remove() // Remove the require hook

    API

    req = withGlobal()

    Create a require function that can be used to inject module-global variables.

    req(specifier, vars)

    Require the module at specifier, injecting vars into all its deeply required modules. All the variables specified in the vars object will be available in modules required using the req function. Calls to require() inside the module will be wrapped so that vars are also recursively injected in dependencies of the module.

    req.remove()

    Remove the compile hooks and global state that withGlobal uses to inject variables into modules.

    Note that globals that were already injected will continue to work; it's perfectly safe to call req.remove() immediately after requiring some modules, if you are certain that there are no lazy require() calls left.

    License

    Apache-2.0

    Install

    npm i require-with-global

    DownloadsWeekly Downloads

    10

    Version

    2.0.1

    License

    Apache-2.0

    Unpacked Size

    8.09 kB

    Total Files

    12

    Last publish

    Collaborators

    • goto-bus-stop