This repository was archived by the owner on Dec 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
188 lines (160 loc) · 4.74 KB
/
index.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
let _ = require('lodash');
let babelPresetEnv = require('@babel/preset-env');
let pluginToMinTargets = require('@babel/preset-env/data/plugins.json');
let targetsParser = require('@babel/preset-env/lib/targets-parser').default;
let isPluginRequired = function(targets, pluginName) {
targets = targetsParser(targets);
let result = babelPresetEnv.isPluginRequired(targets, pluginToMinTargets[pluginName]);
return result;
};
let hasBeenLogged = false;
let debug = function(options) {
if (!options.debug || hasBeenLogged) {
return;
}
hasBeenLogged = true;
// eslint-disable-next-line no-console
console.log('babel-preset-firecloud: `DEBUG` option');
// eslint-disable-next-line no-console
console.log(JSON.stringify({
options
}, undefined, 2));
};
let presets = {
'@babel/preset-env': undefined
};
let plugins = {
'@babel/plugin-proposal-class-properties': undefined,
'@babel/plugin-proposal-nullish-coalescing-operator': undefined,
'@babel/plugin-proposal-optional-chaining': undefined,
'@babel/plugin-syntax-dynamic-import': undefined,
'@babel/plugin-syntax-object-rest-spread': undefined,
'@babel/plugin-transform-async-to-generator': undefined,
'@babel/plugin-transform-exponentiation-operator': undefined,
'babel-plugin-preval': undefined
};
let firecloudPlugins = {
'babel-plugin-firecloud-await-trace': undefined,
'babel-plugin-firecloud-export-all': undefined,
'babel-plugin-firecloud-src-arg': undefined
};
presets = _.mapValues(presets, function(preset, name) {
preset = require(name);
if (preset.__esModule) {
preset = preset.default;
}
return preset;
});
plugins = _.mapValues(plugins, function(plugin, name) {
plugin = require(name);
if (plugin.__esModule) {
plugin = plugin.default;
}
return plugin;
});
firecloudPlugins = _.mapValues(firecloudPlugins, function(plugin, key) {
plugin = require(`./plugins/${key}`);
return plugin;
});
_.merge(plugins, firecloudPlugins);
module.exports = function(context, options) {
options = _.defaults(options || {}, {
spec: false,
loose: false,
useBuiltIns: 'entry',
corejs: undefined
});
options = _.defaults(options, {
'@babel/preset-env': {
targets: {
browsers: [
'last 2 Chrome versions'
],
node: 'current'
}
}
});
options = _.defaultsDeep(options, {
'@babel/preset-env': {
debug: options.debug,
loose: options.loose,
spec: options.spec,
useBuiltIns: options.useBuiltIns,
corejs: options.corejs || options.useBuiltIns ? 2 : undefined
},
'@babel/plugin-transform-async-to-generator': {
disabled: true,
module: 'bluebird/js/release/bluebird',
method: 'coroutine'
},
'babel-plugin-firecloud-src-arg': {
disabled: true
}
});
let asyncToGeneratorIsRequired = isPluginRequired(
options['@babel/preset-env'].targets,
'transform-async-to-generator'
);
if (asyncToGeneratorIsRequired) {
options['@babel/plugin-transform-async-to-generator'].disabled = false;
// disable vanilla plugin for supporting async/await syntax
// the same work is done now by 'babel-plugin-transform-async-to-module-method'
options['@babel/preset-env'].exclude = options['@babel/preset-env'].exclude || [];
options['@babel/preset-env'].exclude.push('transform-async-to-generator');
}
_.forEach(options, function(_options, name) {
if (_.includes([
'debug',
'loose',
'spec',
'useBuiltIns',
'corejs'
], name)) {
return;
}
if (presets[name] || plugins[name]) {
return;
}
let knownPP = _.concat(_.keys(presets), _.keys(plugins));
throw new Error(`Preset/plugin ${name} is unknown to babel-preset-firecloud. I know of ${_.join(knownPP, ',')}.`);
});
debug(options);
let configPresets = _.filter(_.map(presets, function(preset, name) {
let disabled = _.get(options, `${name}.disabled`);
switch (disabled) {
case true:
return false;
case false:
delete options[name].disabled;
break;
case undefined:
break;
default:
throw new Error(`Unknown option for ${name}.disabled: ${disabled}.`);
}
return preset(context, options[name]);
}), Boolean);
let configPlugins = _.without(_.map(plugins, function(plugin, name) {
let disabled = _.get(options, `${name}.disabled`);
switch (disabled) {
case true:
return undefined;
case false:
delete options[name].disabled;
break;
case undefined:
break;
default:
throw new Error(`Unknown option for ${name}.disabled: ${disabled}.`);
}
return [
plugin,
options[name]
];
}), undefined);
let config = {
presets: configPresets,
plugins: configPlugins
};
return config;
};