1 | 'use strict';
|
---|
2 | const { compare, filterOutStabilizedProposals, has, intersection } = require('./helpers');
|
---|
3 | const data = require('./data');
|
---|
4 | const entries = require('./entries');
|
---|
5 | const getModulesListForTargetVersion = require('./get-modules-list-for-target-version');
|
---|
6 | const allModules = require('./modules');
|
---|
7 | const targetsParser = require('./targets-parser');
|
---|
8 |
|
---|
9 | function throwInvalidFilter(filter) {
|
---|
10 | throw new TypeError(`Specified invalid module name or pattern: ${ filter }`);
|
---|
11 | }
|
---|
12 |
|
---|
13 | function atLeastSomeModules(modules, filter) {
|
---|
14 | if (!modules.length) throwInvalidFilter(filter);
|
---|
15 | return modules;
|
---|
16 | }
|
---|
17 |
|
---|
18 | function getModules(filter) {
|
---|
19 | if (typeof filter == 'string') {
|
---|
20 | if (has(entries, filter)) return entries[filter];
|
---|
21 | return atLeastSomeModules(allModules.filter(it => it.startsWith(filter)), filter);
|
---|
22 | }
|
---|
23 | if (filter instanceof RegExp) return atLeastSomeModules(allModules.filter(it => filter.test(it)), filter);
|
---|
24 | throwInvalidFilter(filter);
|
---|
25 | }
|
---|
26 |
|
---|
27 | function normalizeModules(option) {
|
---|
28 | // TODO: use `.flatMap` in core-js@4
|
---|
29 | return new Set(Array.isArray(option) ? [].concat(...option.map(getModules)) : getModules(option));
|
---|
30 | }
|
---|
31 |
|
---|
32 | function checkModule(name, targets) {
|
---|
33 | const result = {
|
---|
34 | required: !targets,
|
---|
35 | targets: {},
|
---|
36 | };
|
---|
37 |
|
---|
38 | if (!targets) return result;
|
---|
39 |
|
---|
40 | const requirements = data[name];
|
---|
41 |
|
---|
42 | for (const [engine, version] of targets) {
|
---|
43 | if (!has(requirements, engine) || compare(version, '<', requirements[engine])) {
|
---|
44 | result.required = true;
|
---|
45 | result.targets[engine] = version;
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | return result;
|
---|
50 | }
|
---|
51 |
|
---|
52 | module.exports = function ({
|
---|
53 | filter = null, // TODO: Obsolete, remove from `core-js@4`
|
---|
54 | modules = null,
|
---|
55 | exclude = [],
|
---|
56 | targets = null,
|
---|
57 | version = null,
|
---|
58 | inverse = false,
|
---|
59 | } = {}) {
|
---|
60 | if (modules === null || modules === undefined) modules = filter;
|
---|
61 | inverse = !!inverse;
|
---|
62 |
|
---|
63 | const parsedTargets = targets ? targetsParser(targets) : null;
|
---|
64 |
|
---|
65 | const result = {
|
---|
66 | list: [],
|
---|
67 | targets: {},
|
---|
68 | };
|
---|
69 |
|
---|
70 | exclude = normalizeModules(exclude);
|
---|
71 |
|
---|
72 | modules = modules ? [...normalizeModules(modules)] : allModules;
|
---|
73 |
|
---|
74 | if (exclude.size) modules = modules.filter(it => !exclude.has(it));
|
---|
75 |
|
---|
76 | modules = intersection(modules, version ? getModulesListForTargetVersion(version) : allModules);
|
---|
77 |
|
---|
78 | if (!inverse) modules = filterOutStabilizedProposals(modules);
|
---|
79 |
|
---|
80 | for (const key of modules) {
|
---|
81 | const check = checkModule(key, parsedTargets);
|
---|
82 |
|
---|
83 | if (check.required ^ inverse) {
|
---|
84 | result.list.push(key);
|
---|
85 | result.targets[key] = check.targets;
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | return result;
|
---|
90 | };
|
---|