Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

callify

mmckegg6MIT0.3.0

Create browserify transforms that change or inline external module function calls

browserify, inline, brfs, transform, precompile

readme

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"