forked from babel/babelify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (52 loc) · 1.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
var assign = require("object-assign");
var stream = require("stream");
var babel = require("babel-core");
var path = require("path");
var util = require("util");
module.exports = Babelify;
util.inherits(Babelify, stream.Transform);
function Babelify(filename, opts) {
if (!(this instanceof Babelify)) {
return Babelify.configure(opts)(filename);
}
stream.Transform.call(this);
this._data = "";
this._opts = assign({filename: filename}, opts);
}
Babelify.prototype._transform = function (buf, enc, callback) {
this._data += buf;
callback();
};
Babelify.prototype._flush = function (callback) {
try {
var code = babel.transform(this._data, this._opts).code;
this.push(code);
} catch(err) {
this.emit("error", err);
return;
}
callback();
};
Babelify.configure = function (opts) {
opts = assign({}, opts);
var extensions = opts.extensions ? babel.util.arrayify(opts.extensions) : null;
var sourceMapRelative = opts.sourceMapRelative;
if (opts.sourceMap !== false) opts.sourceMap = "inline";
// babelify specific options
delete opts.sourceMapRelative;
delete opts.extensions;
delete opts.filename;
// browserify specific options
delete opts._flags;
delete opts.basedir;
delete opts.global;
return function (filename) {
if (!babel.canCompile(filename, extensions)) {
return stream.PassThrough();
}
if (sourceMapRelative) {
filename = path.relative(sourceMapRelative, filename);
}
return new Babelify(filename, opts);
};
};