npm.io
0.3.0 • Published 12 years ago

callify

Licence
MIT
Version
0.3.0
Deps
4
Vulns
0
Weekly
0
Stars
2

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"

Keywords