forked from facebook/regenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
86 lines (70 loc) · 2.12 KB
/
main.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
77
78
79
80
81
82
83
84
85
86
/**
* Copyright (c) 2014-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
var fs = require("fs");
var through = require("through");
var transform = require("./lib/visit").transform;
var utils = require("./lib/util");
function exports(file, options) {
var data = [];
return through(write, end);
function write(buf) {
data.push(buf);
}
function end() {
try {
this.queue(compile(data.join(""), options).code);
this.queue(null);
} catch (e) { this.emit('error', e); }
}
}
// To get a writable stream for use as a browserify transform, call
// require("regenerator")().
module.exports = exports;
// To include the runtime globally in the current node process, call
// require("regenerator").runtime().
function runtime() {
regeneratorRuntime = require("regenerator-runtime");
}
exports.runtime = runtime;
runtime.path = require("regenerator-runtime/path.js").path;
var cachedRuntimeCode;
function getRuntimeCode() {
return cachedRuntimeCode ||
(cachedRuntimeCode = fs.readFileSync(runtime.path, "utf8"));
}
var transformOptions = {
presets: [require("regenerator-preset")],
parserOpts: {
sourceType: "module",
allowImportExportEverywhere: true,
allowReturnOutsideFunction: true,
allowSuperOutsideMethod: true,
strictMode: false,
plugins: ["*", "jsx", "flow"]
}
};
function compile(source, options) {
var result;
options = utils.defaults(options || {}, {
includeRuntime: false
});
var result = require("@babel/core").transformSync(
source,
transformOptions
);
if (options.includeRuntime === true) {
result.code = getRuntimeCode() + "\n" + result.code;
}
return result;
}
// Allow packages that depend on Regenerator to use the same copy of
// ast-types, in case multiple versions are installed by NPM.
exports.types = require("recast").types;
// Transforms a string of source code, returning a { code, map? } result.
exports.compile = compile;
// To modify an AST directly, call require("regenerator").transform(ast).
exports.transform = transform;