_babel-node.js
4.81 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"use strict";
// istanbul ignore next
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj["default"] = obj; return newObj; } }
// istanbul ignore next
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
var _pathIsAbsolute = require("path-is-absolute");
var _pathIsAbsolute2 = _interopRequireDefault(_pathIsAbsolute);
var _commander = require("commander");
var _commander2 = _interopRequireDefault(_commander);
var _module2 = require("module");
var _module3 = _interopRequireDefault(_module2);
var _util = require("util");
var _path = require("path");
var _path2 = _interopRequireDefault(_path);
var _repl = require("repl");
var _repl2 = _interopRequireDefault(_repl);
var _babelCore = require("babel-core");
var babel = _interopRequireWildcard(_babelCore);
var _vm = require("vm");
var _vm2 = _interopRequireDefault(_vm);
var _lodash = require("lodash");
var _lodash2 = _interopRequireDefault(_lodash);
var program = new _commander2["default"].Command("babel-node");
program.option("-e, --eval [script]", "Evaluate script");
program.option("-p, --print [code]", "Evaluate script and print result");
program.option("-i, --ignore [regex]", "Ignore all files that match this regex when using the require hook");
program.option("-x, --extensions [extensions]", "List of extensions to hook into [.es6,.js,.es,.jsx]");
program.option("-r, --stage [stage]", "Enable support for specific ECMAScript stages");
program.option("-w, --whitelist [whitelist]", "Whitelist of transformers separated by comma to ONLY use", _babelCore.util.list);
program.option("-b, --blacklist [blacklist]", "Blacklist of transformers separated by comma to NOT use", _babelCore.util.list);
program.option("-o, --optional [optional]", "List of optional transformers separated by comma to enable", _babelCore.util.list);
var pkg = require("../package.json");
program.version(pkg.version);
program.usage("[options] [ -e script | script.js ] [arguments]");
program.parse(process.argv);
//
babel.register({
extensions: program.extensions,
blacklist: program.blacklist,
whitelist: program.whitelist,
optional: program.optional,
ignore: program.ignore,
stage: program.stage
});
//
var _eval = function _eval(code, filename) {
code = code.trim();
if (!code) return undefined;
code = babel.transform(code, {
filename: filename,
blacklist: program.blacklist,
whitelist: program.whitelist,
optional: program.optional,
stage: program.stage
}).code;
return _vm2["default"].runInThisContext(code, {
filename: filename
});
};
if (program.eval || program.print) {
var code = program.eval;
if (!code || code === true) code = program.print;
global.__filename = "[eval]";
global.__dirname = process.cwd();
var _module = new _module3["default"](global.__filename);
_module.filename = global.__filename;
_module.paths = _module3["default"]._nodeModulePaths(global.__dirname);
global.exports = _module.exports;
global.module = _module;
global.require = _module.require.bind(_module);
var result = _eval(code, global.__filename);
if (program.print) {
var output = _lodash2["default"].isString(result) ? result : _util.inspect(result);
process.stdout.write(output + "\n");
}
} else {
if (program.args.length) {
// slice all arguments up to the first filename since they're babel args that we handle
var args = process.argv.slice(2);
var i = 0;
var ignoreNext = false;
_lodash2["default"].each(args, function (arg, i2) {
if (ignoreNext) {
ignoreNext = false;
return;
}
if (arg[0] === "-") {
var parsedArg = program[arg.slice(2)];
if (parsedArg && parsedArg !== true) {
ignoreNext = true;
}
} else {
i = i2;
return false;
}
});
args = args.slice(i);
// make the filename absolute
var filename = args[0];
if (!_pathIsAbsolute2["default"](filename)) args[0] = _path2["default"].join(process.cwd(), filename);
// add back on node and concat the sliced args
process.argv = ["node"].concat(args);
process.execArgv.unshift(__filename);
_module3["default"].runMain();
} else {
replStart();
}
}
function replStart() {
_repl2["default"].start({
prompt: "> ",
input: process.stdin,
output: process.stdout,
eval: replEval,
useGlobal: true
});
}
function replEval(code, context, filename, callback) {
var err;
var result;
try {
if (code[0] === "(" && code[code.length - 1] === ")") {
code = code.slice(1, -1); // remove "(" and ")"
}
result = _eval(code, filename);
} catch (e) {
err = e;
}
callback(err, result);
}