forked from babel/babelify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (63 loc) · 2 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
64
65
66
67
68
69
70
71
72
73
74
75
76
var assign = require("object-assign");
var stream = require("stream");
var babel = require("babel-core");
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._filename = filename;
this._opts = assign({filename: filename}, opts);
}
Babelify.prototype._transform = function (buf, enc, callback) {
this._data += buf;
callback();
};
Babelify.prototype._flush = function (callback) {
try {
var result = babel.transform(this._data, this._opts);
this.emit("babelify", result, this._filename);
var code = result.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 sourceMapsAbsolute = opts.sourceMapsAbsolute;
if (opts.sourceMaps !== false) opts.sourceMaps = "inline";
// babelify specific options
delete opts.sourceMapsAbsolute;
delete opts.extensions;
delete opts.filename;
// babelify backwards-compat
delete opts.sourceMapRelative;
// browserify specific options
delete opts._flags;
delete opts.basedir;
delete opts.global;
// browserify cli options
delete opts._;
// "--opt [ a b ]" and "--opt a --opt b" are allowed:
if (opts.ignore && opts.ignore._) opts.ignore = opts.ignore._;
if (opts.only && opts.only._) opts.only = opts.only._;
if (opts.plugins && opts.plugins._) opts.plugins = opts.plugins._;
if (opts.presets && opts.presets._) opts.presets = opts.presets._;
return function (filename) {
if (!babel.util.canCompile(filename, extensions)) {
return stream.PassThrough();
}
var _opts = sourceMapsAbsolute
? assign({sourceFileName: filename}, opts)
: opts;
return new Babelify(filename, _opts);
};
};