Skip to content

Commit 748faa1

Browse files
committed
Consider the stream class an implementation detail, but allow instanceof checks.
1 parent 9b2cbd0 commit 748faa1

File tree

1 file changed

+45
-40
lines changed

1 file changed

+45
-40
lines changed

index.js

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,45 +23,17 @@ if (/^6\./.test(babel.version)) {
2323
);
2424
}
2525

26-
module.exports = Babelify;
27-
util.inherits(Babelify, stream.Transform);
28-
29-
function Babelify(filename, opts) {
30-
if (!(this instanceof Babelify)) {
31-
return babelify(filename, opts);
32-
}
33-
34-
stream.Transform.call(this);
35-
this._data = [];
36-
this._filename = filename;
37-
this._opts = opts;
38-
}
39-
40-
Babelify.prototype._transform = function (buf, enc, callback) {
41-
this._data.push(buf);
42-
callback();
43-
};
44-
45-
Babelify.prototype._flush = function (callback) {
46-
// Merge the buffer pieces after all are available, instead of one at a time,
47-
// to avoid corrupting multibyte characters.
48-
const data = Buffer.concat(this._data).toString();
49-
50-
transform(data, this._opts, (err, result) => {
51-
if (err) {
52-
this.emit("error", err);
53-
} else {
54-
this.emit("babelify", result, this._filename);
55-
var code = result !== null ? result.code : data;
56-
this.push(code);
57-
callback();
58-
}
59-
});
60-
};
61-
62-
Babelify.configure = buildTransform;
63-
64-
const babelify = buildTransform();
26+
module.exports = buildTransform();
27+
module.exports.configure = buildTransform;
28+
29+
// Allow projects to import this module and check `foo instanceof babelify`
30+
// to see if the current stream they are working with is one created
31+
// by Babelify.
32+
Object.defineProperty(module.exports, Symbol.hasInstance, {
33+
value: function hasInstance(obj) {
34+
return obj instanceof BabelifyStream;
35+
},
36+
});
6537

6638
function buildTransform(opts) {
6739
return function (filename, transformOpts) {
@@ -70,7 +42,7 @@ function buildTransform(opts) {
7042
return stream.PassThrough();
7143
}
7244

73-
return new Babelify(filename, babelOpts);
45+
return new BabelifyStream(babelOpts);
7446
};
7547
}
7648

@@ -147,6 +119,39 @@ function normalizeTransformOpts(opts) {
147119
return opts;
148120
}
149121

122+
class BabelifyStream extends stream.Transform {
123+
constructor(opts) {
124+
super();
125+
this._data = [];
126+
this._opts = opts;
127+
}
128+
129+
_transform(buf, enc, callback) {
130+
this._data.push(buf);
131+
callback();
132+
}
133+
134+
_flush(callback) {
135+
// Merge the buffer pieces after all are available, instead of one at a time,
136+
// to avoid corrupting multibyte characters.
137+
const data = Buffer.concat(this._data).toString();
138+
139+
transform(data, this._opts, (err, result) => {
140+
if (err) {
141+
callback(err);
142+
} else {
143+
this.emit("babelify", result, this._opts.filename);
144+
var code = result !== null ? result.code : data;
145+
146+
// Note: Node 8.x allows passing 'code' to the callback instead of
147+
// manually pushing, but we need to support Node 6.x.
148+
this.push(code);
149+
callback();
150+
}
151+
});
152+
}
153+
}
154+
150155
function transform(data, inputOpts, done) {
151156
let cfg;
152157
try {

0 commit comments

Comments
 (0)