npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vinyl-map2

v1.2.1

Published

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

Downloads

13,918

Readme

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, filename[, 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
  • filename is the value of file.path, which should generally be the file's absolute path. Might be convenient if you want to filter based on file extension etc.
  • 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.