forked from babel/babel
/
standalone.js
121 lines (110 loc) · 3.16 KB
/
standalone.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
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
import * as Babel from "@babel/core";
import metadataPlugin from "./metadataPlugin";
// take from @babel/standalone
import {
presets as availablePresets,
plugins as availablePlugins,
} from "./plugins-list";
export function transpilePlugin(pluginString) {
return Babel.transform(pluginString, {
babelrc: false,
configFile: false,
ast: false,
highlightCode: false,
presets: [
[
availablePresets["@babel/preset-env"],
{ loose: true, shippedProposals: true },
],
[
availablePresets["@babel/preset-typescript"],
{
isTSX: true,
allExtensions: true,
allowDeclareFields: true,
allowNamespaces: true,
onlyRemoveTypeImports: true,
},
],
],
plugins: [metadataPlugin],
}).code;
}
// astexplorer
export default function compileModule(code, globals = {}) {
const exports = {};
const module = { exports };
const globalNames = Object.keys(globals);
const keys = ["module", "exports", ...globalNames];
const values = [module, exports, ...globalNames.map(key => globals[key])];
// eslint-disable-next-line no-new-func
new Function(keys.join(), code).apply(exports, values);
return module.exports;
}
export function loadBuiltin(builtinTable, name) {
if (Array.isArray(name) && typeof name[0] === "string") {
if (Object.prototype.hasOwnProperty.call(builtinTable, name[0])) {
return [builtinTable[name[0]]].concat(name.slice(1));
}
return;
} else if (typeof name === "string") {
return builtinTable[name];
}
// Could be an actual preset/plugin module
return name;
}
export function processOptions(options, customPlugin) {
if (typeof options === "string") options = JSON.parse(options);
// Parse preset names
const presets = (options.presets || []).map(presetName => {
const preset = loadBuiltin(availablePresets, presetName);
if (preset) {
// workaround for babel issue
// at some point, babel copies the preset, losing the non-enumerable
// buildPreset key; convert it into an enumerable key.
if (
Array.isArray(preset) &&
typeof preset[0] === "object" &&
Object.prototype.hasOwnProperty.call(preset[0], "buildPreset")
) {
preset[0] = { ...preset[0], buildPreset: preset[0].buildPreset };
}
} else {
throw new Error(
`Invalid preset specified in Babel options: "${presetName}"`
);
}
return preset;
});
// Parse plugin names
const plugins = (options.plugins || []).map(pluginName => {
const plugin = loadBuiltin(availablePlugins, pluginName);
if (!plugin) {
throw new Error(
`Invalid plugin specified in Babel options: "${pluginName}"`
);
}
return plugin;
});
if (customPlugin) {
customPlugin = transpilePlugin(customPlugin);
plugins.unshift(compileModule(customPlugin));
}
return {
ast: true,
parserOpts: {
plugins: [
"typescript",
"jsx",
"classProperties",
"classPrivateProperties",
"classPrivateMethods",
],
},
babelrc: false,
configFile: false,
...options,
presets,
plugins,
};
}