replace-method
JavaScript post-processing step to replace specific function/method calls with
other bits of JavaScript. It doesn't take scope into account, but is otherwise
a good starting point for writing your own inlinifying transform modules like
brfs
.
Usage
replace = require('replace-method')(ast)
Returns a function you can use to replace methods with other things.
Where ast
is either a string of source code, or an AST object such as one
generated by esprima.
replace(method, found)
Where method
is an array of variable names, such that:
['__filename']
will match__filename
.['fs', 'readFileSync']
will matchfs.readFileSync
.['a', 'nested', 'property']
will matcha.nested.property
.
found
is then called once per every node in the AST found – if you return
nothing or undefined
, nothing will change. However, if you return an AST
object that content will be replaced, e.g.:
returntype: 'Literal'value: 'hello world'
Will replace the matched variable with the string "hello world"
.
replace.code()
Returns the updated version of your code after having made the transformations
you needed. Note that the supplied ast
object you provided will be modified
directly so you can convert it to JavaScript yourself or hand it off to other
processing steps if need be.
Example
var replaceMethod =var evaluate =var escodegen =var esprima =var fs =var src =srcconsole
License
MIT. See LICENSE.md for details.