forked from babel/sandboxes
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstandalone.js
119 lines (102 loc) · 3.35 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
import * as Babel from "@babel/standalone";
// take from @babel/standalone
import { availablePlugins, availablePresets } from "@babel/standalone";
export function transpilePlugin(pluginString, presets) {
return Babel.transform(pluginString, {
babelrc: false,
configFile: false,
ast: false,
highlightCode: false,
presets: presets,
}).code;
}
// astexplorer
export default function compileModule(code, globals = {}) {
let exports = {};
let module = { exports };
let globalNames = Object.keys(globals);
let keys = ["module", "exports", ...globalNames];
let 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;
}
function trimBabelPluginPrefix(obj) {
// Given a string with (anything)plugin-(stuff) return
// another string containing (stuff) if (anything)plugin- was present.
// Otherwise return the original string.
const trimFunc = str => str.replace(/(.*plugin-)(.*)/, "$2");
// If it's an array return the same array with the first item trimmed.
if (Array.isArray(obj) && typeof obj[0] === "string") {
return [trimFunc(obj[0]), ...obj.slice(1)];
}
// If it's just a string, return the new string.
if (typeof obj === "string") {
return trimFunc(obj);
}
// Otherwise just return the original object.
return obj;
}
export function processOptions(options, customPlugin) {
if (typeof options === "string") options = JSON.parse(options);
// Parse preset names
const presets = (options.presets || []).map(presetName => {
let preset = loadBuiltin(availablePresets, presetName);
if (!preset) {
preset = loadBuiltin(availablePresets, trimBabelPluginPrefix(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 => {
let plugin = loadBuiltin(availablePlugins, pluginName);
if (!plugin) {
plugin = loadBuiltin(availablePlugins, trimBabelPluginPrefix(pluginName));
}
if (!plugin) {
throw new Error(
`Invalid plugin specified in Babel options: "${pluginName}"`
);
}
return plugin;
});
if (customPlugin) {
customPlugin = transpilePlugin(customPlugin, presets);
plugins.unshift(compileModule(customPlugin));
}
return {
babelrc: false,
configFile: false,
...options,
presets,
plugins,
};
}