Skip to content

Commit abac251

Browse files
committed
Merge pull request #30 from shinnn/readable-stream
Use readable-stream v2 instead of through2 v0.6
2 parents df4c479 + ff7a860 commit abac251

File tree

7 files changed

+100
-115
lines changed

7 files changed

+100
-115
lines changed

Diff for: lib/common.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,6 @@ module.exports = {
5656
return tasks;
5757
},
5858

59-
extend: function (source, dest) {
60-
Object.keys(dest).forEach(function (key) {
61-
source[key] = dest[key];
62-
});
63-
return source;
64-
},
65-
6659
regexMatchAll: function (string, regexp) {
6760
var matches = [];
6861
string.replace(regexp, function () {
@@ -71,4 +64,4 @@ module.exports = {
7164
});
7265
return matches;
7366
}
74-
};
67+
};

Diff for: lib/index.js

+24-24
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict';
22

3-
var Parser = require('./parser');
4-
var through = require('through2');
53
var common = require('./common');
4+
var objectAssign = require('object-assign');
5+
var Parser = require('./parser');
6+
var Transform = require('readable-stream/transform');
67

78
module.exports = function (options, userConfig) {
89
var tasks = common.parseTasks(options);
@@ -16,35 +17,34 @@ module.exports = function (options, userConfig) {
1617
if (typeof userConfig === 'boolean') {
1718
config.keepUnassigned = userConfig;
1819
} else if (typeof userConfig === 'object') {
19-
config = common.extend(config, userConfig);
20+
objectAssign(config, userConfig);
2021
}
2122

22-
return through.obj(function (file, enc, callback) {
23-
var parser = new Parser(tasks, config, file);
24-
25-
if (file.isBuffer()) {
26-
parser.write(file.contents);
27-
parser.end();
28-
29-
var contents = new Buffer(0);
30-
parser.on('data', function (data) {
31-
contents = Buffer.concat([contents, data]);
32-
});
33-
parser.once('end', function () {
34-
file.contents = contents;
23+
return new Transform({
24+
objectMode: true,
25+
transform: function (file, enc, callback) {
26+
var parser = new Parser(tasks, config, file);
27+
28+
if (file.isBuffer()) {
29+
parser.write(file.contents);
30+
parser.end();
31+
32+
var contents = new Buffer(0);
33+
parser.on('data', function (data) {
34+
contents = Buffer.concat([contents, data]);
35+
});
36+
parser.once('end', function () {
37+
file.contents = contents;
38+
callback(null, file);
39+
});
40+
return;
41+
}
3542

36-
this.push(file);
37-
return callback();
38-
}.bind(this));
39-
} else {
4043
if (file.isStream()) {
4144
file.contents = file.contents.pipe(parser);
4245
}
4346

44-
this.push(file);
45-
return callback();
47+
callback(null, file);
4648
}
4749
});
4850
};
49-
50-

Diff for: lib/parser.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
var util = require('util');
4-
var Transform = require('stream').Transform;
4+
var Transform = require('readable-stream/transform');
55
var Block = require('./block');
66
var common = require('./common');
77

@@ -35,9 +35,7 @@ Parser.prototype._transform = function (chunk, enc, done) {
3535
content = content.replace(block.replacement, block.compile(this.tasks));
3636
}.bind(this));
3737

38-
this.push(content);
39-
40-
done();
38+
done(null, content);
4139
};
4240

4341
module.exports = Parser;

Diff for: package.json

+11-8
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,26 @@
2626
],
2727
"main": "./lib/index.js",
2828
"scripts": {
29-
"test": "mocha --reporter spec",
30-
"coverage": "istanbul cover node_modules/mocha/bin/_mocha -- -R dot",
31-
"coveralls": "istanbul cover ./node_modules/mocha/bin/_mocha -- -R dot && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
29+
"test": "mocha",
30+
"coverage": "istanbul cover _mocha -- -R dot",
31+
"coveralls": "istanbul cover _mocha && istanbul-coveralls"
3232
},
3333
"engines": {
3434
"node": ">= 0.9"
3535
},
3636
"dependencies": {
37-
"slash": "^1.0.0",
38-
"through2": "^0.6.5"
37+
"object-assign": "^3.0.0",
38+
"readable-stream": "^2.0.1",
39+
"slash": "^1.0.0"
3940
},
4041
"devDependencies": {
41-
"coveralls": "^2.11.2",
42-
"event-stream": "^3.3.1",
42+
"concat-stream": "^1.5.0",
43+
"from2-string": "^1.0.2",
4344
"gulp-util": "^3.0.5",
4445
"istanbul": "^0.3.14",
45-
"mocha": "^2.2.5"
46+
"istanbul-coveralls": "^1.0.3",
47+
"mocha": "^2.2.5",
48+
"vinyl": "^0.5.0"
4649
},
4750
"license": "MIT"
4851
}

Diff for: test/buffer.js

+23-26
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
11
'use strict';
22

3-
var plugin = require('../lib/index');
3+
var plugin = require('..');
44
var fs = require('fs');
55
var path = require('path');
6-
var gutil = require('gulp-util');
6+
var File = require('vinyl');
77
var assert = require('assert');
88

99
function compare(fixture, expected, stream, done) {
10-
var fakeFile = new gutil.File({
11-
contents: fixture
12-
});
13-
14-
fakeFile.base = path.join(fakeFile.cwd, 'pages');
15-
fakeFile.path = path.join(fakeFile.cwd, path.join('pages', 'index.html'));
16-
17-
stream.write(fakeFile);
18-
19-
stream.once('data', function (file) {
10+
stream
11+
.once('data', function (file) {
2012
assert(file.isBuffer());
21-
assert.equal(String(file.contents), String(expected));
13+
assert.strictEqual(String(file.contents), expected);
2214
done();
23-
});
15+
})
16+
.end(new File({
17+
base: path.resolve('pages'),
18+
path: path.join('pages', 'index.html'),
19+
contents: fixture
20+
}));
2421
}
2522

2623
describe('Buffer mode', function () {
2724
it('should replace blocks', function (done) {
2825
var fixture = fs.readFileSync(path.join('test', 'fixture.html'));
29-
var expected = fs.readFileSync(path.join('test', 'expected.html'));
26+
var expected = fs.readFileSync(path.join('test', 'expected.html'), 'utf8');
3027

3128
var stream = plugin({
3229
css: 'css/combined.css',
@@ -78,14 +75,14 @@ describe('Buffer mode', function () {
7875
var expected = '<!DOCTYPE html><head><link rel="stylesheet" href="css/combined.css"></head>';
7976

8077
var stream = plugin({css: 'css/combined.css'});
81-
compare(new Buffer(fixture), new Buffer(expected), stream, done);
78+
compare(new Buffer(fixture), expected, stream, done);
8279
});
8380

8481
it('should not fail if there are no build tags at all', function (done) {
8582
var fixture = '<!DOCTYPE html><head><link rel="stylesheet" href="_index.prefix.css"></head>';
8683

8784
var stream = plugin({css: 'css/combined.css'});
88-
compare(new Buffer(fixture), new Buffer(fixture), stream, done);
85+
compare(new Buffer(fixture), fixture, stream, done);
8986
});
9087

9188
describe('Options', function () {
@@ -96,15 +93,15 @@ describe('Buffer mode', function () {
9693
var expected = '<html>\nSome text\n</html>';
9794

9895
var stream = plugin({}, {keepUnassigned: true});
99-
compare(new Buffer(fixture), new Buffer(expected), stream, done);
96+
compare(new Buffer(fixture), expected, stream, done);
10097
});
10198

10299
it('Should remove empty blocks', function (done) {
103100
var fixture = '<html>\n<!-- build:js -->\nSome text\n<!-- endbuild -->\n</html>';
104101
var expected = '<html>\n</html>';
105102

106103
var stream = plugin();
107-
compare(new Buffer(fixture), new Buffer(expected), stream, done);
104+
compare(new Buffer(fixture), expected, stream, done);
108105
});
109106
});
110107

@@ -114,31 +111,31 @@ describe('Buffer mode', function () {
114111
var expected = '<html>\n<!-- build:js -->\n<!-- endbuild -->\n</html>';
115112

116113
var stream = plugin({}, {keepBlockTags: true});
117-
compare(new Buffer(fixture), new Buffer(expected), stream, done);
114+
compare(new Buffer(fixture), expected, stream, done);
118115
});
119116

120117
it('Should keep placeholder tags with arguments', function (done) {
121118
var fixture = '<html>\n<!-- build:lorem -->\nSome text\n<!-- endbuild -->\n</html>';
122119
var expected = '<html>\n<!-- build:lorem -->\nipsum\n<!-- endbuild -->\n</html>';
123120

124121
var stream = plugin({lorem: 'ipsum'}, {keepBlockTags: true});
125-
compare(new Buffer(fixture), new Buffer(expected), stream, done);
122+
compare(new Buffer(fixture), expected, stream, done);
126123
});
127124

128125
it('Should remove placeholder tags without arguments', function (done) {
129126
var fixture = '<html>\n<!-- build:js -->\nSome text\n<!-- endbuild -->\n</html>';
130127
var expected = '<html>\n</html>';
131128

132129
var stream = plugin();
133-
compare(new Buffer(fixture), new Buffer(expected), stream, done);
130+
compare(new Buffer(fixture), expected, stream, done);
134131
});
135132

136133
it('Should remove placeholder tags with arguments', function (done) {
137134
var fixture = '<html>\n<!-- build:lorem -->\nSome text\n<!-- endbuild -->\n</html>';
138135
var expected = '<html>\nipsum\n</html>';
139136

140137
var stream = plugin({lorem: 'ipsum'});
141-
compare(new Buffer(fixture), new Buffer(expected), stream, done);
138+
compare(new Buffer(fixture), expected, stream, done);
142139
});
143140
});
144141

@@ -148,7 +145,7 @@ describe('Buffer mode', function () {
148145
var expected = '<html>\n<script src="../lib/script.js"></script>\n</html>';
149146

150147
var stream = plugin({js: 'lib/script.js'}, {resolvePaths: true});
151-
compare(new Buffer(fixture), new Buffer(expected), stream, done);
148+
compare(new Buffer(fixture), expected, stream, done);
152149
});
153150
});
154151
});
@@ -159,15 +156,15 @@ describe('Buffer mode', function () {
159156
var expected = '<html>\nThis should be removed if "keepUnassigned" is false\n</html>';
160157

161158
var stream = plugin({}, true);
162-
compare(new Buffer(fixture), new Buffer(expected), stream, done);
159+
compare(new Buffer(fixture), expected, stream, done);
163160
});
164161

165162
it('[version <1.2] should remove empty blocks (keepUnassigned = false)', function (done) {
166163
var fixture = '<html>\n<!-- build:js -->\nThis should be removed if "keepUnassigned" is false\n<!-- endbuild -->\n</html>';
167164
var expected = '<html>\n</html>';
168165

169166
var stream = plugin();
170-
compare(new Buffer(fixture), new Buffer(expected), stream, done);
167+
compare(new Buffer(fixture), expected, stream, done);
171168
});
172169
});
173170

Diff for: test/null.js

+7-13
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
'use strict';
22

3-
var plugin = require('../lib/index');
4-
var gutil = require('gulp-util');
3+
var plugin = require('..');
4+
var File = require('vinyl');
55
var assert = require('assert');
66

77
describe('null files', function () {
88
it('should be passed through', function (done) {
9-
10-
var fakeFile = new gutil.File({
11-
contents: null
12-
});
13-
14-
var stream = plugin();
15-
stream.write(fakeFile);
16-
17-
stream.once('data', function (file) {
9+
plugin()
10+
.once('data', function (file) {
1811
assert(file.isNull());
1912
done();
20-
});
13+
})
14+
.end(new File({contents: null}));
2115
});
22-
});
16+
});

0 commit comments

Comments
 (0)