# vinyl-map

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

Latest version **1.0.2** (published 2016-12-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install vinyl-map
pnpm add vinyl-map
yarn add vinyl-map
bun add vinyl-map
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2016-12-20 |
| First published | 2014-01-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Hugh Kennedy |
| Maintainers | affirmix |
| Keywords | vinyl, gulp, gulpfriendly, file, stream, map |

## Links

- npm: https://www.npmjs.com/package/vinyl-map
- Repository: https://github.com/affirmix/vinyl-map
- Issues: https://github.com/affirmix/vinyl-map/issues
- npm.io page: https://npm.io/package/vinyl-map

## Dependencies (3)

- [bl](https://npm.io/package/bl.md) ^1.1.2
- [new-from](https://npm.io/package/new-from.md) 0.0.3
- [through2](https://npm.io/package/through2.md) ^0.4.1

## Alternatives

- [unionfs](https://npm.io/package/unionfs.md) — 2.2M weekly downloads
- [path-starts-with](https://npm.io/package/path-starts-with.md) — 35.9K weekly downloads
- [redzip](https://npm.io/package/redzip.md) — 1.2K weekly downloads
- [vscode-anymatch](https://npm.io/package/vscode-anymatch.md) — 848 weekly downloads
- [@ledgerhq/coin-filecoin](https://npm.io/package/@ledgerhq/coin-filecoin.md) — 793 weekly downloads

## Recent versions

- 1.0.2 (latest) — 2016-12-20
- 1.0.1 — 2014-02-17
- 1.0.0 — 2014-02-08
- 0.1.0 — 2014-01-23
- 0.0.1 — 2014-01-11
- 0.0.0 — 2014-01-11

## README

# vinyl-map

Map [vinyl](https://github.com/wearefractal/vinyl) files' contents as strings,
so you can easily use existing code without needing yet another
[gulp](https://github.com/gulpjs/gulp) plugin!

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

``` javascript
var through = require('through')
var 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

Here's a simple example, using gulp:

``` javascript
var uglify = require('uglify-js')
var map = require('vinyl-map')
var 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))` ###

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 two
arguments:

* `contents` is a string or [Buffer](http://nodejs.org/api/buffer.html)
* `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.

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.

## License ##

MIT. See [LICENSE.md](http://github.com/hughsk/vinyl-map/blob/master/LICENSE.md) for details.

---
_Source: https://npm.io/package/vinyl-map · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
