Skip to content

Added support for streams. This resolves #1 #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,43 @@ module.exports = exports = function(options) {
resolver = new FileResolver(importPaths),
formatter = formatterFor(options.formatter);

function transformStream(stream, callback) {
var chunks = [],
length = 0;

function onRead() {
var chunk = stream.read();
if (chunk) {
length += chunk.length;
chunks.push(chunk);
}
}

stream.on('error', callback);
stream.on('readable', onRead);
stream.once('end', function onEnd() {
stream.removeListener('readable', onRead);

callback(null, Buffer.concat(chunks, length));
});
}

function transform(file, encoding, callback) {
if (file.isNull()) {
return callback(null, file);
}

if (file.isStream()) {
return callback(new gutil.PluginError(
'gulp-es6-module-transpiler',
'Streaming not supported'
));
/* jshint validthis:true */
return transformStream(file.contents, function(error, buffer) {
if (error) {
return callback(error);
}

file.contents = buffer;

transform.call(this, file, encoding, callback);
}.bind(this));
}

var container = new transpiler.Container({
Expand Down
18 changes: 18 additions & 0 deletions test/e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,22 @@ describe('gulp-es6-module-transpiler use cases', function() {
});
});
});

describe('streams', function() {
it('should accept stream input', function(done) {
var inputs = [inputDir + '/main/default.js', inputDir + '/main/foo.js'];

gulp.src(inputs, { buffer: false })
.pipe(transpile({
basePath: inputDir + '/main'
}))
.pipe(gulp.dest(outputDir))
.on('end', function() {
expect(fs.existsSync(outputDir + '/default.js')).to.be(true);
expect(fs.existsSync(outputDir + '/foo.js')).to.be(true);

done();
});
});
});
});
20 changes: 20 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require('mocha');
var fs = require('fs');
var tempWrite = require('temp-write');
var es6 = require('es6-module-transpiler');
var Stream = require('stream');

var expect = require('expect.js');

Expand Down Expand Up @@ -292,6 +293,25 @@ describe('gulp-es6-module-transpiler', function() {
' Looking in: \\["' + inputDir + '"\\]'
));
});

it('should throw error if input stream emitted error', function(done) {
var stream = new Stream.PassThrough('some bytes');
var file = new gutil.File({
path: 'streamed.js',
contents: stream
});

transpile({
sources: [file]
}, function(error) {
expect(error).to.not.be(null);
expect(error.message).to.be('A stream error');

done();
});

stream.emit('error', new Error('A stream error'));
});
});

it('should output one file for every input file', function(done) {
Expand Down