Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including vinyl-ast with all npm packages installed. Try it out:

var vinylAst = require("vinyl-ast")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

vinyl-ast v0.2.0

Parse-once and generate-once AST tool bridge for Gulp plugins

vinyl-ast NPM version build status Dependency Status

Parse-once and generate-once AST tool bridge for Gulp plugins.

Inspired by gulp-sourcemaps. Just PoC now.

Usage

Just use Gulp and the plugins.

var gulp = require('gulp');
var plugin1usingVinylAst = require('gulp-plugin1-using-vinyl-ast');
var plugin2usingVinylAst = require('gulp-plugin2-using-vinyl-ast');
var plugin3 = require('gulp-plugin3');

gulp.task('javascript', function() {
  gulp.src('src/**/*.js')
    // parse source to AST
    .pipe(plugin1usingVinylAst())
    // AST to AST (no conversion!)
    .pipe(plugin2usingVinylAst())
    // generate souce from AST
    .pipe(plugin3())
    .pipe(gulp.dest('dist'));
});

Because plugin1 and plugin2 support vinyl-ast,
the source is parsed at the beginning of plugin1 at once and generated once internally.

Even if plugin3 doesn't know about vinyl-ast, no problem.
vinyl-ast generate sources as a file#contents property for general gulp plugins.

VS.

aster is an AST-based code builder.
To use it with Gulp, additional confugiration is needed.

vinyl-ast is primary designed for working with Gulp.
Gulp users don't have to concern about such an AST.
Only plugin developers need to do.

For Gulp plugin developer

Check file.ast property, use it and set your result ast to it.

var through = require('through2');
var vinylAst = require('vinyl-ast');
var myTransform = require('myTransform');

module.exports = function(options) {
  return through.obj(function(file, encoding, callback) {
    var resutl;

    if (file.ast) {
      // use parsed ast!
      result = myTransformFromAst(file.ast, options);
    } else {
      // convert vinyl file to vinyl-ast file
      vinylAst.apply(file);
      // do normal plugin logic
      result = myTransformFromSource(file.contents, options);
    }

    // set result AST to file.ast
    file.ast = result.ast

    this.push(file);
    callback();
  }
};

Features

Select AST parser

TBD

Specify generator options

TBD

Sourcemap

Use gulp-sourcemaps.

TBD

License

MIT License: Teppei Sato teppeis@gmail.com

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free