1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | var fs = require("fs");
|
9 | var through = require("through");
|
10 | var transform = require("./lib/visit").transform;
|
11 | var utils = require("./lib/util");
|
12 |
|
13 | function exports(file, options) {
|
14 | var data = [];
|
15 | return through(write, end);
|
16 |
|
17 | function write(buf) {
|
18 | data.push(buf);
|
19 | }
|
20 |
|
21 | function end() {
|
22 | try {
|
23 | this.queue(compile(data.join(""), options).code);
|
24 | this.queue(null);
|
25 | } catch (e) { this.emit('error', e); }
|
26 | }
|
27 | }
|
28 |
|
29 |
|
30 |
|
31 | module.exports = exports;
|
32 |
|
33 |
|
34 |
|
35 | function runtime() {
|
36 | regeneratorRuntime = require("regenerator-runtime");
|
37 | }
|
38 | exports.runtime = runtime;
|
39 | runtime.path = require("regenerator-runtime/path.js").path;
|
40 |
|
41 | var cachedRuntimeCode;
|
42 | function getRuntimeCode() {
|
43 | return cachedRuntimeCode ||
|
44 | (cachedRuntimeCode = fs.readFileSync(runtime.path, "utf8"));
|
45 | }
|
46 |
|
47 | var transformOptions = {
|
48 | presets: [require("regenerator-preset")],
|
49 | parserOpts: {
|
50 | sourceType: "module",
|
51 | allowImportExportEverywhere: true,
|
52 | allowReturnOutsideFunction: true,
|
53 | allowSuperOutsideMethod: true,
|
54 | strictMode: false,
|
55 | plugins: ["*", "jsx", "flow"]
|
56 | }
|
57 | };
|
58 |
|
59 | function compile(source, options) {
|
60 | var result;
|
61 |
|
62 | options = utils.defaults(options || {}, {
|
63 | includeRuntime: false
|
64 | });
|
65 |
|
66 | var result = require("@babel/core").transformSync(
|
67 | source,
|
68 | transformOptions
|
69 | );
|
70 |
|
71 | if (options.includeRuntime === true) {
|
72 | result.code = getRuntimeCode() + "\n" + result.code;
|
73 | }
|
74 |
|
75 | return result;
|
76 | }
|
77 |
|
78 |
|
79 |
|
80 | exports.types = require("recast").types;
|
81 |
|
82 |
|
83 | exports.compile = compile;
|
84 |
|
85 |
|
86 | exports.transform = transform;
|