Git Product home page Git Product logo

vinyl-map2's Introduction

vinyl-map2

Circle CI

Map vinyl files' contents as strings, so you can easily use existing code without needing yet another gulp plugin!

Essentially, with the hope of reducing the number of gulp plugins out there which are just doing this:

var through = require('through'),
  uglify = require('uglify-js');

module.exports = function() {
  return through(function(file) {
    if (file.isNull()) return this.queue(file);
    if (file.isStream()) throw new Error('no support');

    file.contents = file.contents.toString();

    var minified = uglify.minify(file.contents, {
      fromString: true
    })

    file = file.clone();
    file.contents = new Buffer(minified.code);
    this.queue(file);
  });
}

Of course, sometimes that's fine too, but this might help save some complexity for when it's too much hassle. It also takes care of the differences between handling Buffer, Stream and null values for your file.contents.

Usage

npm install --save vinyl-map2

Here's a simple example, using gulp:

var uglify = require('uglify-js'),
  map = require('vinyl-map2'),
  gulp = require('gulp');

gulp.task('minify', function() {
  var minify = map(function(code, filename) {
    // file contents are handed
    // over as buffers
    code = code.toString();

    return uglify.minify(code, {
      fromString: true
    }).code;
  })

  return gulp.src(['./index.js'])
    .pipe(minify)
    .pipe(gulp.dest('./dist'));
})

API

map(mapper(contents, file[, done]))

Returns a transform stream that takes vinyl files as input and spits out their modified copies as output.

mapper is a function which will be called once for each file, with three arguments:

  • contents is a string or Buffer
  • file is the vinyl file object
  • done is an optional callback function. If your mapper function has a third argument, it will be called asynchronously. If not, the mapper will be called synchronously.

The mapper function is expected to return a modified string value for the updated file contents. If nothing is returned, no modifications will be made to the file contents, but the output file will be cloned.

If you run the mapper function asynchronously (by passing in a third done argument), you must call it instead of returning the file contents. It is a node-style callback: done(err, contents)

License

MIT. See LICENSE.md for details.

Contributing

This project was forked from hughsk/vinyl-map, and will be actively maintained. I welcome contributions in the form of both bug reports and pull requests!

Fork the project and submit a PR on a branch that is not named master. We use linting tools and unit tests, which are built constantly using continuous integration. If you find a bug, it would be appreciated if you could also submit a branch with a failing unit test to show your case.

vinyl-map2's People

Contributors

nelsonpecora avatar hughsk avatar cryptoquick avatar shama avatar

Watchers

Seth Lesky avatar James Cloos 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.