Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/gulpjs/vinyl-contents

Utility to read the contents of a vinyl file.
https://github.com/gulpjs/vinyl-contents

gulp gulp-plugins gulpplugin javascript nodejs streams vinyl

Last synced: 5 days ago
JSON representation

Utility to read the contents of a vinyl file.

Awesome Lists containing this project

README

        





# vinyl-contents

[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][ci-image]][ci-url] [![Coveralls Status][coveralls-image]][coveralls-url]

Utility to read the contents of a vinyl file.

## Usage

```js
/*
WARNING: This is a very naive plugin implementation
It is only meant for demonstation purposes.
For a more complete implementation, see: https://github.com/gulp-community/gulp-pug
*/
var { Transform } = require('streamx');
var pug = require('pug');
var vinylContents = require('vinyl-contents');

function gulpPug(options) {
return new Transform({
transform: function (file, cb) {
vinylContents(file, function (err, contents) {
if (err) {
return cb(err);
}

if (!contents) {
return cb();
}

file.contents = pug.compile(contents.toString(), options)();
cb(null, file);
});
},
});
}
```

## API

### `vinylContents(file, callback)`

**Warning:** Only use this if interacting with a library that can **only** receive strings or buffers. This loads all streaming contents into memory which can cause unexpected results for your end-users.

Takes a Vinyl file and an error-first callback. Calls the callback with an error if one occur (or if the first argument is not a Vinyl file), or the file contents if no error occurs.

If the Vinyl contents are:

- A Buffer, will be returned directly.
- A Stream, will be buffered into a BufferList and returned.
- Empty, will be undefined.

## License

MIT

[downloads-image]: https://img.shields.io/npm/dm/vinyl-contents.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/vinyl-contents
[npm-image]: https://img.shields.io/npm/v/vinyl-contents.svg?style=flat-square

[ci-url]: https://github.com/gulpjs/vinyl-contents/actions?query=workflow:dev
[ci-image]: https://img.shields.io/github/actions/workflow/status/gulpjs/vinyl-contents/dev.yml?branch=master&style=flat-square

[coveralls-url]: https://coveralls.io/r/gulpjs/vinyl-contents
[coveralls-image]: https://img.shields.io/coveralls/gulpjs/vinyl-contents/master.svg?style=flat-square