/
compile.ts
46 lines (42 loc) · 1.54 KB
/
compile.ts
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
import * as babel from "@babel/core";
import plugin from "./babelPlugin";
import * as prettier from "prettier";
import getBabelOptions, { getAndResetLocs } from "./getBabelOptions";
var sourceMapRegex = /\/\/#[\W]*sourceMappingURL=.*$/;
function removeSourceMapIfAny(code) {
// In theory we might be able to use this source map, but right now
// 1) parsing source maps on the frontend is hard, because FE JS doesn't
// natively support parsing UTF-8 base64 which is used for inline source maps
// 2) It could break things if we don't take it out, so need to do some work
// to handle the existing source map properly
if (sourceMapRegex.test(code)) {
var sourceMapComment = code.match(/\/\/#[\W]*sourceMappingURL=.*$/)[0];
code = code.replace(sourceMapComment, "");
}
return code;
}
export default function transform(code, extraBabelOptions = {}) {
return new Promise((resolve, reject) => {
let result;
try {
result = compileSync(code, extraBabelOptions);
} catch (err) {
reject(err);
}
// prettify code is helpful for debugging, but it's slow
// result.code = prettier.format(result.code, { parser: "babylon" });
resolve(result);
});
}
export function compileSync(code, extraBabelOptions = {}, url = "/no_url.js") {
code = removeSourceMapIfAny(code);
const babelResult = babel.transform(
code,
getBabelOptions(plugin, extraBabelOptions, url)
);
return {
map: babelResult.map,
code: babelResult.code + "\n//# sourceMappingURL=" + url + ".map",
locs: getAndResetLocs()
};
}