Skip to content

Instantly share code, notes, and snippets.

@willhlaw
Created November 22, 2014 20:36
Show Gist options
  • Save willhlaw/48ecfa741b8896beb515 to your computer and use it in GitHub Desktop.
Save willhlaw/48ecfa741b8896beb515 to your computer and use it in GitHub Desktop.
NPM Workflow to Preprocess a Javascript File
var escodegen = require('escodegen')
var through = require('through')
var esprima = require('esprima')
var modify = require('./modify') // Custom code that you write to preprocess the file
module.exports = function(file, opts) {
var buffer = []
return through(function(data) {
buffer.push(data)
}, function() {
buffer = buffer.join('\n')
var ast = esprima.parse(buffer)
// Whatever functionality goes here...
modify(ast)
buffer = escodegen.generate(ast)
this.queue(buffer)
this.queue(null)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment