bahmutov / really-need

Node require wrapper with options for cache busting, pre- and post-processing
MIT License
109 stars 4 forks source link

Improve `args` to support nested functions #29

Open mateatslc opened 8 years ago

mateatslc commented 8 years ago

Functions that aren't defined as top-level properties of args are not making through to the loaded module source. So for example, this works fine:

// another-require.js
require('something');

// index.js
require = require('really-need');
require('./another-require', {
  args: {
    require: function (name) {
      console.log('no requires allowed');
    }
  }
});
// prints "no requires allowed"

... but this doesn't:

// stuff-with-lodash.js
console.log( _.yam );
_.filter();

// index.js
require = require('really-need');
require('./stuff-with-lodash', {
  args: {
    _: {
      'yam': 678,
      'filter': function () {
        console.log('look! a fake lodash filter!');
      }
    }
  }
});
// prints:
// 678
// _.filter is not a function

I already hacked together something that works using a dusty gist by @cowboy but I'm not in love with it. Let me know if this is something you think worth looking into.