This repository was archived by the owner on May 11, 2018. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathtargets-parser.js
124 lines (100 loc) · 3.14 KB
/
targets-parser.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import browserslist from "browserslist";
import semver from "semver";
import { semverify } from "./utils";
const browserNameMap = {
android: "android",
chrome: "chrome",
and_chr: "chrome",
edge: "edge",
firefox: "firefox",
ie: "ie",
ios_saf: "ios",
safari: "safari",
};
const isBrowsersQueryValid = (browsers) =>
typeof browsers === "string" || Array.isArray(browsers);
const semverMin = (first, second) => {
return first && semver.lt(first, second) ? first : second;
};
const getLowestVersions = (browsers) => {
return browsers.reduce(
(all, browser) => {
const [browserName, browserVersion] = browser.split(" ");
const normalizedBrowserName = browserNameMap[browserName];
if (!normalizedBrowserName) {
return all;
}
try {
// Browser version can return as "10.0-10.2"
const splitVersion = browserVersion.split("-")[0];
const parsedBrowserVersion = semverify(splitVersion);
all[normalizedBrowserName] = semverMin(
all[normalizedBrowserName],
parsedBrowserVersion,
);
} catch (e) {}
return all;
},
{},
);
};
const outputDecimalWarning = (decimalTargets) => {
if (!decimalTargets || !decimalTargets.length) {
return;
}
console.log("Warning, the following targets are using a decimal version:");
console.log("");
decimalTargets.forEach(({ target, value }) =>
console.log(` ${target}: ${value}`));
console.log("");
console.log(
"We recommend using a string for minor/patch versions to avoid numbers like 6.10",
);
console.log("getting parsed as 6.1, which can lead to unexpected behavior.");
console.log("");
};
const targetParserMap = {
__default: (target, value) => [target, semverify(value)],
// Parse `node: true` and `node: "current"` to version
node: (target, value) => {
const parsed = value === true || value === "current"
? process.versions.node
: semverify(value);
return [target, parsed];
},
// Only valid value for Uglify is `true`
uglify: (target, value) => [target, value === true],
};
const getTargets = (targets = {}) => {
let targetOpts = {};
// Parse browsers target via browserslist
if (isBrowsersQueryValid(targets.browsers)) {
targetOpts = getLowestVersions(browserslist(targets.browsers));
}
// Parse remaining targets
const parsed = Object.keys(targets).reduce(
(results, target) => {
if (target !== "browsers") {
const value = targets[target];
// Warn when specifying minor/patch as a decimal
if (typeof value === "number" && value % 1 !== 0) {
results.decimalWarnings.push({ target, value });
}
// Check if we have a target parser?
const parser = targetParserMap[target] || targetParserMap.__default;
const [parsedTarget, parsedValue] = parser(target, value);
if (parsedValue) {
results.targets[parsedTarget] = parsedValue;
}
}
return results;
},
{
targets: targetOpts,
decimalWarnings: [],
},
);
outputDecimalWarning(parsed.decimalWarnings);
return parsed.targets;
};
export default getTargets;