1 | 'use strict';
|
---|
2 | const browserslist = require('browserslist');
|
---|
3 | const { compare, has } = require('./helpers');
|
---|
4 | const external = require('./external');
|
---|
5 |
|
---|
6 | const aliases = new Map([
|
---|
7 | ['and_chr', 'chrome-android'],
|
---|
8 | ['and_ff', 'firefox-android'],
|
---|
9 | ['ie_mob', 'ie'],
|
---|
10 | ['ios_saf', 'ios'],
|
---|
11 | ['oculus', 'quest'],
|
---|
12 | ['op_mob', 'opera-android'],
|
---|
13 | // TODO: Remove from `core-js@4`
|
---|
14 | ['opera_mobile', 'opera-android'],
|
---|
15 | ['react', 'react-native'],
|
---|
16 | ['reactnative', 'react-native'],
|
---|
17 | ]);
|
---|
18 |
|
---|
19 | const validTargets = new Set([
|
---|
20 | 'android',
|
---|
21 | 'bun',
|
---|
22 | 'chrome',
|
---|
23 | 'chrome-android',
|
---|
24 | 'deno',
|
---|
25 | 'edge',
|
---|
26 | 'electron',
|
---|
27 | 'firefox',
|
---|
28 | 'firefox-android',
|
---|
29 | 'hermes',
|
---|
30 | 'ie',
|
---|
31 | 'ios',
|
---|
32 | 'node',
|
---|
33 | 'opera',
|
---|
34 | 'opera-android',
|
---|
35 | 'phantom',
|
---|
36 | 'quest',
|
---|
37 | 'react-native',
|
---|
38 | 'rhino',
|
---|
39 | 'safari',
|
---|
40 | 'samsung',
|
---|
41 | ]);
|
---|
42 |
|
---|
43 | const toLowerKeys = function (object) {
|
---|
44 | return Object.entries(object).reduce((accumulator, [key, value]) => {
|
---|
45 | accumulator[key.toLowerCase()] = value;
|
---|
46 | return accumulator;
|
---|
47 | }, {});
|
---|
48 | };
|
---|
49 |
|
---|
50 | module.exports = function (targets) {
|
---|
51 | const { browsers, esmodules, node, ...rest } = (typeof targets != 'object' || Array.isArray(targets))
|
---|
52 | ? { browsers: targets } : toLowerKeys(targets);
|
---|
53 |
|
---|
54 | const list = Object.entries(rest);
|
---|
55 |
|
---|
56 | if (browsers) {
|
---|
57 | if (typeof browsers == 'string' || Array.isArray(browsers)) {
|
---|
58 | list.push(...browserslist(browsers).map(it => it.split(' ')));
|
---|
59 | } else {
|
---|
60 | list.push(...Object.entries(browsers));
|
---|
61 | }
|
---|
62 | }
|
---|
63 | if (esmodules) {
|
---|
64 | list.push(...Object.entries(external.modules));
|
---|
65 | }
|
---|
66 | if (node) {
|
---|
67 | list.push(['node', node === 'current' ? process.versions.node : node]);
|
---|
68 | }
|
---|
69 |
|
---|
70 | const normalized = list.map(([engine, version]) => {
|
---|
71 | if (has(browserslist.aliases, engine)) {
|
---|
72 | engine = browserslist.aliases[engine];
|
---|
73 | }
|
---|
74 | if (aliases.has(engine)) {
|
---|
75 | engine = aliases.get(engine);
|
---|
76 | }
|
---|
77 | return [engine, String(version)];
|
---|
78 | }).filter(([engine]) => {
|
---|
79 | return validTargets.has(engine);
|
---|
80 | }).sort(([a], [b]) => {
|
---|
81 | return a < b ? -1 : a > b ? 1 : 0;
|
---|
82 | });
|
---|
83 |
|
---|
84 | const reducedByMinVersion = new Map();
|
---|
85 | for (const [engine, version] of normalized) {
|
---|
86 | if (!reducedByMinVersion.has(engine) || compare(version, '<=', reducedByMinVersion.get(engine))) {
|
---|
87 | reducedByMinVersion.set(engine, version);
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | return reducedByMinVersion;
|
---|
92 | };
|
---|