Seamlessly pipe between text transform streams and AST transforms
Seamlessly pipe between text transform streams and AST transforms.
var escodegen = require'escodegen'var through = require'through'var esprima = require'esprima'var modify = require'./modify'var buffer =return throughbufferpushdatabuffer = bufferjoin'\n'var ast = esprimaparsebuffer// Whatever functionality goes here...modifyastbuffer = escodegengenerateastthisqueuebufferthisqueuenull
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.
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.
Specify how to convert from a buffered text stream to an AST:
var css = require'css'pipelinedecodevar ast = cssparsecontentsnextnull ast
Specify how to convert from an AST object to a transform stream:
var css = require'css'pipelineencodevar contents = cssstringifyastnextnull contents
Takes an array of
Function transforms, returning a duplex
stream which will take the inital file input and output the transformed
var pipeline = require'ast-pipeline'var css = require'css'var fs = require'fs'var bl = require'bl'return pipelineencodedonenull cssstringifyastdecodedonenull cssparseasttransforms = transformsmapreturn trfilenamefscreateReadStreamfilenamepipereworkertransformspipeblcallback
AST transforms are simply functions with the signature
(ast, done). They take
an object (generally, an AST), modify it, and pass it back to
If no new value is passed through the
done callback, the previous value will
be used instead.
var variables = require'rework-variables'returnvariableshello: 'world'aststylesheetdonenull ast
MIT. See LICENSE.md for details.