Giter VIP home page Giter VIP logo

ast-pipeline's Introduction

ast-pipeline Flattr this!experimental

Seamlessly pipe between text transform streams and AST transforms.

Say, for example, you have a source transform API similar to browserify's: each file goes through a series of transforms streams, the output of one becoming the input of the next. Recently an increasing number of transform streams have appeared that operate on plain old parseable JavaScript, generally using the following pattern:

var escodegen = require('escodegen')
var through = require('through')
var esprima = require('esprima')
var modify = require('./modify')

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)
  })
}

This is straightforward and pretty repeatable, but if you start chaining together multiple transforms together that work like this, you get increasing overhead from parsing/deparsing the JavaScript AST. In larger projects, this can make builds take considerably longer than they would without the transforms.

So, ast-pipeline lets you pass in an array of streams as normal, but also allows you to pass in functions too. These functions are expected to take an AST and transform it, calling a callback when they're ready to pass it on to the next transform. If you chain a few AST transforms together, you'll only have to parse/deparse once!

Switching between text transform streams and AST modifiers is handled transparently, so you can still pass transforms in any order and they'll be converted to the right format as required.

Usage

ast-pipeline

createPipeline = astPipeline([opts])

Creates a new pipeline "factory". Takes the following options:

  • opts.preferAST: expect a single AST object as both the input and ouptut of these pipelines.

createPipeline.decode(handle)

Specify how to convert from a buffered text stream to an AST:

var css = require('css')

pipeline.decode(function(contents, next) {
  var ast = css.parse(contents)
  next(null, ast)
})

createPipeline.encode(handle)

Specify how to convert from an AST object to a transform stream:

var css = require('css')

pipeline.encode(function(ast, next) {
  var contents = css.stringify(ast)
  next(null, contents)
})

createPipeline(transforms)

Takes an array of Stream and Function transforms, returning a duplex stream which will take the inital file input and output the transformed contents.

var pipeline = require('ast-pipeline')
var css = require('css')
var fs = require('fs')
var bl = require('bl')

function reworker() {
  return pipeline().encode(function(ast, done) {
    done(null, css.stringify(ast))
  }).decode(function(ast, done) {
    done(null, css.parse(ast))
  })
}

function transformFile(filename, transforms, callback) {
  transforms = transforms.map(function(tr) {
    return tr(filename)
  })

  fs.createReadStream(filename)
    .pipe(reworker()(transforms))
    .pipe(bl(callback))
}

AST Transforms

AST transforms are simply functions with the signature (ast, done). They take an object (generally, an AST), modify it, and pass it back to done(err, ast).

If no new value is passed through the done callback, the previous value will be used instead.

var variables = require('rework-variables')

module.exports = function(file) {
  return function(ast, done) {
    variables({
      hello: 'world'
    })(ast.stylesheet)

    done(null, ast)
  }
}

License

MIT. See LICENSE.md for details.

ast-pipeline's People

Contributors

hughsk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.