/
build-compat-data.mjs
81 lines (72 loc) · 2.5 KB
/
build-compat-data.mjs
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
/* https://github.com/import-js/eslint-plugin-import/issues/2181 */
import { dataWithIgnored as data, ignored, modules } from 'core-js-compat/src/data.mjs';
import external from 'core-js-compat/src/external.mjs';
import mappings from 'core-js-compat/src/mapping.mjs';
import helpers from 'core-js-compat/helpers.js';
for (const scope of [data, external]) {
for (const [key, module] of Object.entries(scope)) {
const { chrome, ie } = module;
function map(mappingKey) {
const [engine, targetKey] = mappingKey.split('To')
.map(it => it.replace(/([a-z])([A-Z])/, (_, a, b) => `${ a }_${ b }`).toLowerCase());
const version = module[engine];
if (!version || module[targetKey]) return;
const mapping = mappings[mappingKey];
if (typeof mapping == 'function') {
return module[targetKey] = String(mapping(version));
}
const source = helpers.semver(version);
for (const [from, to] of mapping) {
if (helpers.compare(source, '<=', from)) {
return module[targetKey] = String(to);
}
}
}
map('ChromeToAndroid');
if (!module.android && chrome) {
module.android = String(Math.max(chrome, 37));
}
if (key.startsWith('es')) {
map('ChromeToDeno');
}
if (/^(?:es|esnext|web)\./.test(key)) {
map('ChromeToElectron');
}
if (!module.edge) {
if (ie && !key.includes('immediate')) {
module.edge = '12';
} else if (chrome) {
module.edge = String(Math.max(chrome, 74));
}
}
if (key.startsWith('es')) {
map('ChromeToNode');
}
map('ChromeToOpera');
if (!module.opera_mobile && module.opera <= 42) {
module.opera_mobile = module.opera;
} else {
map('ChromeToOperaMobile');
}
map('ChromeToSamsung');
map('AndroidToOculus');
map('SafariToIOS');
map('SafariToPhantom');
scope[key] = helpers.sortObjectByKey(module);
}
}
function write(filename, content) {
return fs.writeJson(`packages/core-js-compat/${ filename }.json`, content, { spaces: ' ' });
}
const dataWithoutIgnored = { ...data };
for (const ignore of ignored) delete dataWithoutIgnored[ignore];
await Promise.all([
write('data', dataWithoutIgnored),
write('modules', modules),
write('external', external),
// version for compat data tests
fs.writeFile('tests/compat/compat-data.js', `;(typeof global != 'undefined' ? global : Function('return this')()).data = ${
JSON.stringify(data, null, ' ')
}`),
]);
echo(chalk.green('compat data rebuilt'));