├── LICENSE.md ├── README.md ├── index.js ├── package.json └── test.js /LICENSE.md: -------------------------------------------------------------------------------- 1 | ## The MIT License (MIT) ## 2 | 3 | Copyright (c) 2014 Hugh Kennedy 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vinyl-source-stream [![Flattr this!](https://api.flattr.com/button/flattr-badge-large.png)](https://flattr.com/submit/auto?user_id=hughskennedy&url=http://github.com/hughsk/vinyl-source-stream&title=vinyl-source-stream&description=hughsk/vinyl-source-stream%20on%20GitHub&language=en_GB&tags=flattr,github,javascript&category=software)[![experimental](http://hughsk.github.io/stability-badges/dist/experimental.svg)](http://github.com/hughsk/stability-badges) # 2 | 3 | Use conventional text streams at the start of your 4 | [gulp](http://github.com/gulpjs/gulp) or 5 | [vinyl](http://github.com/wearefractal/vinyl) pipelines, making for nicer 6 | interoperability with the existing npm stream ecosystem. 7 | 8 | Take, for example, [browserify](http://browserify.org/). There are the 9 | [gulp-browserify](https://github.com/deepak1556/gulp-browserify) and 10 | [gulpify](https://github.com/hughsk/gulpify) plugins, which you can use in 11 | combination with gulp to get browserify working in your build. Unfortunately, 12 | these plugins come with additional overhead: an extra GitHub repository, npm 13 | module, maintainer, tests, semantics, etc. It's much simpler 14 | in this case to use the original module directly where you can, which is what 15 | `vinyl-source-stream` handles for you. 16 | 17 | ## Usage ## 18 | 19 | [![vinyl-source-stream](https://nodei.co/npm/vinyl-source-stream.png?mini=true)](https://nodei.co/npm/vinyl-source-stream) 20 | 21 | Our previous example, browserify, has a streaming API for its output bundles 22 | which you can use directly. This module is just a bridge that makes it 23 | simple to use conventional text streams such as this in combination with gulp. 24 | Here's an example of using `vinyl-source-stream` and `browserify`, compared to 25 | using `gulpify`: 26 | 27 | ``` javascript 28 | var source = require('vinyl-source-stream') 29 | var streamify = require('gulp-streamify') 30 | var browserify = require('browserify') 31 | var uglify = require('gulp-uglify') 32 | var gulpify = require('gulpify') 33 | var rename = require('gulp-rename') 34 | var gulp = require('gulp') 35 | 36 | // using gulpify: 37 | gulp.task('gulpify', function() { 38 | gulp.src('index.js') 39 | .pipe(gulpify()) 40 | .pipe(uglify()) 41 | .pipe(rename('bundle.js')) 42 | .pipe(gulp.dest('./')) 43 | }) 44 | 45 | // using vinyl-source-stream: 46 | gulp.task('browserify', function() { 47 | var bundleStream = browserify('./index.js').bundle() 48 | 49 | bundleStream 50 | .pipe(source('index.js')) 51 | .pipe(streamify(uglify())) 52 | .pipe(rename('bundle.js')) 53 | .pipe(gulp.dest('./')) 54 | }) 55 | ``` 56 | 57 | Not all that different, really! The nice thing here is that you're getting the 58 | up-to-date browserify API and don't have to worry about the plugin's available 59 | functionality. Of course, these same benefits apply for any readable text 60 | stream you can find on npm. 61 | 62 | ## API ## 63 | 64 | ### `stream = sourceStream([filename])` ### 65 | 66 | Creates a through stream which takes text as input, and emits a single 67 | vinyl file instance for streams down the pipeline to consume. 68 | 69 | `filename` is a "pretend" filename to use for your file, which some streams 70 | might use to determine various factors such as the final filename of your file. 71 | It should be a string, and though recommended, using this argument is optional. 72 | 73 | ## License ## 74 | 75 | MIT. See [LICENSE.md](http://github.com/hughsk/vinyl-source-stream/blob/master/LICENSE.md) for details. 76 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var through2 = require('through2') 2 | var File = require('vinyl') 3 | var path = require('path') 4 | 5 | module.exports = function (filename, baseDir) { 6 | var ins = through2() 7 | var out = false 8 | 9 | var opts = { 10 | contents: ins 11 | } 12 | 13 | if (filename) opts.path = path.resolve(baseDir || process.cwd(), filename) 14 | if (baseDir) opts.base = baseDir 15 | 16 | var file = new File(opts) 17 | 18 | return through2({ 19 | objectMode: true 20 | }, function(chunk, enc, next) { 21 | if (!out) { 22 | this.push(file) 23 | out = true 24 | } 25 | 26 | ins.push(chunk) 27 | next() 28 | }, function() { 29 | ins.push(null) 30 | this.push(null) 31 | }) 32 | } 33 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vinyl-source-stream", 3 | "description": "Use conventional text streams at the start of your gulp or vinyl pipelines", 4 | "version": "2.0.0", 5 | "main": "index.js", 6 | "browser": "index.js", 7 | "dependencies": { 8 | "vinyl": "^2.1.0", 9 | "through2": "^2.0.3" 10 | }, 11 | "devDependencies": { 12 | "gulp-rename": "~0.2.1", 13 | "tape": "~2.3.2", 14 | "vinyl-fs": "^3.0.0" 15 | }, 16 | "scripts": { 17 | "test": "node test" 18 | }, 19 | "author": "Hugh Kennedy (http://hughsk.io/)", 20 | "license": "MIT", 21 | "repository": { 22 | "type": "git", 23 | "url": "git://github.com/hughsk/vinyl-source-stream" 24 | }, 25 | "bugs": { 26 | "url": "https://github.com/hughsk/vinyl-source-stream/issues" 27 | }, 28 | "homepage": "https://github.com/hughsk/vinyl-source-stream", 29 | "keywords": [ 30 | "vinyl", 31 | "gulp", 32 | "gulpfriendly", 33 | "vanilla", 34 | "stream", 35 | "string", 36 | "text", 37 | "classic" 38 | ] 39 | } 40 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var rename = require('gulp-rename') 2 | var srcStream = require('./') 3 | var vfs = require('vinyl-fs') 4 | var test = require('tape') 5 | var path = require('path') 6 | var fs = require('fs') 7 | var through = require('through2'); 8 | 9 | function upper() { 10 | return through(function(chunk, _, cb) { 11 | var str = chunk.toString().toUpperCase(); 12 | cb(null, new Buffer(str)); 13 | }); 14 | } 15 | 16 | test('capitalizing test file', function(t) { 17 | fs.createReadStream(__filename) 18 | .pipe(srcStream(__filename)) 19 | .pipe(through.obj(function(file, _, cb) { 20 | file.contents = file.contents.pipe(upper()); 21 | cb(null, file); 22 | })) 23 | .pipe(rename("fixture.js")) 24 | .pipe(vfs.dest('.')) 25 | .once('end', function() { 26 | // gulp.dest finishes before writing 27 | // the file is complete... 28 | setTimeout(function() { 29 | t.pass('reached pipline "end" event') 30 | t.equal( 31 | fs.readFileSync(__dirname + '/fixture.js', 'utf8') 32 | , fs.readFileSync(__filename, 'utf8').toUpperCase() 33 | , 'transformed contents as expected' 34 | ) 35 | 36 | fs.unlink(__dirname + '/fixture.js', function(err) { 37 | t.ifError(err, 'removed fixture successfully') 38 | t.end() 39 | }) 40 | }, 1500) 41 | }) 42 | }) 43 | 44 | test('baseDir: defaults to process.cwd()', function(t) { 45 | process.chdir(path.resolve(__dirname, '..', '..')) 46 | 47 | fs.createReadStream(__filename) 48 | .pipe(srcStream(path.basename(__filename))) 49 | .on('data', function(file) { 50 | t.equal(process.cwd(), path.dirname(file.path), 'defaults to process.cwd()') 51 | 52 | process.chdir(__dirname) 53 | 54 | t.end() 55 | }) 56 | }) 57 | --------------------------------------------------------------------------------