callify

Create browserify transforms that change or inline external module function calls

npm install callify
8 downloads in the last week
8 downloads in the last month

callify

Create browserify transforms that change or inline external module function calls.

Uses falafel for scanning calls to specified modules.

NPM

Example

Find all fs.readFileSync calls and inline the result:

// transform.js

var callify = require('callify')
var staticEval = require('static-eval')

module.exports = callify({

  // specify in the module names you want to hook and what to do with the node

  'fs': function(node, params){
    if (params.calls[1] === 'readFileSync'){
      var args = getArgs(node, params)
      var content = readFileSync.apply(this, args)
      node.update(JSON.stringify(content))
    }
  }
})

function getArgs(node, params){
  return node.arguments.map(function(arg){ 
    return staticEval(arg, {
      __filename: params.file,
      __dirname: path.dirname(params.file)
    }) 
  })
}
// entry.js
var filesys = require("fs")
var content = filesys.readFileSync(__dirname + "/content.txt", "utf8")
$ browserify entry.js -t ./transform.js > output.js
// output.js
var filesys = require("fs")
var content = "file contents"
npm loves you