/
build-data.js
80 lines (71 loc) · 2.25 KB
/
build-data.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
'use strict';
const { writeFileSync } = require('fs');
const { resolve } = require('path');
const { green } = require('chalk');
const { compare, sortObjectByKey, semver } = require('../helpers');
const data = require('./data');
const external = require('./external');
const {
ChromeToNode,
ChromeToSamsung,
ChromeToOperaMobile,
ChromeToAndroid,
ChromeToElectron,
SafariToIOS,
SafariToPhantomJS,
} = require('./mapping');
for (const scope of [data, external]) {
for (const [key, module] of Object.entries(scope)) {
const { chrome, ie, safari } = module;
const map = function (mapping, version, targetKey) {
if (module[targetKey]) return;
const source = semver(version);
for (const [from, to] of mapping) {
if (compare(source, '<=', from)) {
module[targetKey] = String(to);
return;
}
}
};
if (!module.edge && ie && key !== 'web.immediate') {
module.edge = '12';
}
if (chrome) {
if (!module.edge) {
module.edge = String(Math.max(chrome, 74));
}
if (!module.opera) {
module.opera = String(chrome <= 23 ? 15 : chrome <= 29 ? 16 : chrome <= 82 ? chrome - 13 : chrome - 14);
}
if (!module.opera_mobile && module.opera && module.opera <= 42) {
module.opera_mobile = module.opera;
} else {
map(ChromeToOperaMobile, chrome, 'opera_mobile');
}
if (key.startsWith('es')) {
map(ChromeToNode, chrome, 'node');
}
map(ChromeToSamsung, chrome, 'samsung');
map(ChromeToAndroid, chrome, 'android');
if (!module.android) {
module.android = String(Math.max(chrome, 37));
}
if (/^(?:es|esnext|web)\./.test(key)) {
map(ChromeToElectron, chrome, 'electron');
}
}
if (safari) {
map(SafariToIOS, safari, 'ios');
map(SafariToPhantomJS, safari, 'phantom');
}
scope[key] = sortObjectByKey(module);
}
}
function writeJSON(filename, content) {
writeFileSync(resolve(__dirname, filename), JSON.stringify(content, null, ' '));
}
writeJSON('../data.json', data);
writeJSON('../modules.json', Object.keys(data));
writeJSON('../external.json', external);
// eslint-disable-next-line no-console -- output
console.log(green('compat data rebuilt'));