Socket
Socket
Sign inDemoInstall

ast-transform

Package Overview
Dependencies
7
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

ast-transform

Convenience wrapper for performing AST transformations with browserify transform streams


Version published
Maintainers
1
Weekly downloads
760,018
decreased by-11.56%

Weekly downloads

Readme

Source

ast-transform Flattr this!experimental

Convenience wrapper for performing AST transformations with browserify transform streams. Note that it doesn't handle source maps nicely, though pull requests are welcome!

Usage

ast-transform

ast(transform[, opts])

Where transform is a function that takes a filename and returns a function, e.g.:

var replace = require('replace-method')
var ast = require('ast-transform')
var path = require('path')

module.exports = ast(function (file) {
  if (path.extname(file) !== '.js') return

  return function(ast, done) {
    // replace require calls with
    // strings for some reason
    replace(ast)(['require'], function(node) {
      return { type: 'Literal', value: 'replaced!' }
    })

    done()
  }
})

Note that you can return a falsey value instead of a function to bail the stream early and avoid the parse/deparse overhead. Here's an example of using the above with browserify:

var browserify = require('browserify')
var example = require('./example')
var fs = require('fs')

browserify('./index.js')
  .transform(example)
  .bundle()
  .pipe(fs.createWriteStream(__filename + '/bundle.js'))

License

MIT. See LICENSE.md for details.

Keywords

FAQs

Last updated on 27 Feb 2014

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc