-
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathutils-build-data.js
More file actions
146 lines (120 loc) · 4.03 KB
/
utils-build-data.js
File metadata and controls
146 lines (120 loc) · 4.03 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// coped from https://github.com/babel/babel/blob/main/packages/babel-compat-data/scripts/utils-build-data.js
"use strict";
const fs = require("fs");
const flatMap = require("es-toolkit/compat/flatMap");
const mapValues = require("es-toolkit/compat/mapValues");
const findLastIndex = require("es-toolkit/compat/findLastIndex");
const electronToChromiumVersions = require("electron-to-chromium").versions;
const envs = require("../../build/compat-table/environments");
const parseEnvsVersions = require("../../build/compat-table/build-utils/parse-envs-versions");
const interpolateAllResults = require("../../build/compat-table/build-utils/interpolate-all-results");
const compareVersions = require("../../build/compat-table/build-utils/compare-versions");
// Add Electron to the list of environments
Object.keys(electronToChromiumVersions).forEach(electron => {
const chrome = electronToChromiumVersions[electron];
const electronId = `electron${electron.replace(".", "_")}`;
let chromeId = `chrome${chrome}`;
// This is missing
if (chromeId === "chrome82") chromeId = "chrome81";
if (!envs[chromeId]) {
console.error(
`Electron ${electron} inherits from Chrome ${chrome}, which is not defined.`
);
return;
}
envs[electronId] = { equals: chromeId };
});
const envsVersions = parseEnvsVersions(envs);
const compatSources = ["es5", "es6", "es2016plus", "esnext"].map(source => {
const data = require(`../../build/compat-table/data-${source}`);
interpolateAllResults(data.tests, envs);
return data;
});
// End of compat-table code adaptation
exports.environments = [
"chrome",
"opera",
"edge",
"firefox",
"safari",
"node",
"ie",
"android",
"ios",
"phantom",
"samsung",
"electron",
];
const compatibilityTests = flatMap(compatSources, data =>
flatMap(data.tests, test => {
if (!test.subtests) return test;
return test.subtests.map(subtest =>
Object.assign({}, subtest, {
name: test.name + " / " + subtest.name,
group: test.name,
})
);
})
);
exports.getLowestImplementedVersion = (
{ features },
env,
exclude = () => false
) => {
const tests = compatibilityTests.filter(test => {
// TODO (Babel 9): Use ||=, &&=
let ok = features.includes(test.name);
ok = ok || (test.group && features.includes(test.group));
ok = ok || (features.length === 1 && test.name.startsWith(features[0]));
ok = ok && !exclude(test.name);
return ok;
});
const envTests = tests.map(({ res }) => {
const lastNotImplemented = findLastIndex(
envsVersions[env],
// Babel assumes strict mode
({ id }) => !(res[id] === true || res[id] === "strict")
);
return envsVersions[env][lastNotImplemented + 1];
});
if (envTests.length === 0 || envTests.some(t => !t)) return null;
const result = envTests.reduce((a, b) => {
return compareVersions(a, b) > 0 ? a : b;
});
// NOTE(bng): A number of environments in compat-table changed to
// include a trailing zero (node10 -> node10_0), so for now stripping
// it to be consistent
return result.version.join(".").replace(/\.0$/, "");
};
exports.generateData = (environments, features) => {
return mapValues(features, options => {
if (!options.features) {
options = {
features: [options],
};
}
const plugin = {};
environments.forEach(env => {
const version = exports.getLowestImplementedVersion(options, env);
if (version) plugin[env] = version;
});
return plugin;
});
};
exports.writeFile = function (data, dataPath, name) {
const stringified = JSON.stringify(data, null, 2) + "\n";
if (process.env.CHECK_COMPAT_DATA) {
const currentData = fs.readFileSync(dataPath, "utf8");
// Compare as JSON strings to also check keys ordering
if (currentData !== stringified) {
console.error(
`The newly generated ${name} data does not match the current ` +
"files. Re-run `yarn build-es-shims-data`."
);
return false;
}
} else {
fs.writeFileSync(dataPath, stringified);
}
return true;
};