forked from zloirock/core-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
105 lines (90 loc) · 2.66 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
'use strict';
const { coerce, lt, lte } = require('semver');
const browserslist = require('browserslist');
const data = require('./data');
const getModulesListForTargetVersion = require('./get-modules-list-for-target-version');
const has = Function.call.bind({}.hasOwnProperty);
const mapping = new Map([
['ios_saf', 'ios'],
['and_chr', 'chrome'],
['and_ff', 'firefox'],
]);
const validTargets = new Set([
'android',
'chrome',
'edge',
'electron',
'firefox',
'ie',
'ios',
'node',
'opera',
'phantom',
'safari',
'samsung',
]);
function coercedLte(a, b) {
return lte(coerce(a), coerce(b));
}
function coercedLt(a, b) {
return lt(coerce(a), coerce(b));
}
function normalizeBrowsersList(list) {
return list.map(it => {
let [engine, version] = it.split(' ');
if (mapping.has(engine)) engine = mapping.get(engine);
else if (engine === 'android' && !coercedLte(version, '4.4.4')) engine = 'chrome';
return [engine, version];
}).filter(([engine]) => validTargets.has(engine));
}
function reduceByMinVersion(list) {
const targets = new Map();
for (const [engine, version] of list) {
if (!targets.has(engine) || coercedLte(version, targets.get(engine))) {
targets.set(engine, version);
}
}
return targets;
}
function checkModule(name, targets) {
if (!has(data, name)) throw new TypeError(`Incorrect module: ${ name }`);
const requirements = data[name];
const result = {
required: false,
targets: {},
};
for (const [engine, version] of targets) {
if (!has(requirements, engine) || coercedLt(version, requirements[engine])) {
result.required = true;
result.targets[engine] = version;
}
}
return result;
}
function compat({ targets, filter, version }) {
const list = browserslist(targets);
const engines = normalizeBrowsersList(list);
const reducedTargets = reduceByMinVersion(engines);
const result = {
list: [],
targets: {},
};
let modules = Array.isArray(filter) ? filter : Object.keys(data);
if (filter instanceof RegExp) modules = modules.filter(it => filter.test(it));
else if (typeof filter == 'string') modules = modules.filter(it => it.startsWith(filter));
if (version) {
const availableModules = new Set(getModulesListForTargetVersion(version));
modules = modules.filter(name => availableModules.has(name));
}
modules.forEach(key => {
const check = checkModule(key, reducedTargets);
if (check.required) {
result.list.push(key);
result.targets[key] = check.targets;
}
});
return result;
}
module.exports = compat;
module.exports.compat = compat;
module.exports.getModulesListForTargetVersion = getModulesListForTargetVersion;