forked from parcel-bundler/lightningcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-prefixes.js
More file actions
151 lines (133 loc) · 3.96 KB
/
build-prefixes.js
File metadata and controls
151 lines (133 loc) · 3.96 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
147
148
149
150
151
const prefixes = require('autoprefixer/data/prefixes');
const browsers = require('caniuse-lite').agents;
const fs = require('fs');
const BROWSER_MAPPING = {
and_chr: 'chrome',
and_ff: 'firefox',
ie_mob: 'ie',
op_mob: 'opera',
and_qq: null,
and_uc: null,
baidu: null,
bb: null,
kaios: null,
op_mini: null,
};
let flexSpec = {};
let p = new Map();
for (let prop in prefixes) {
let browserMap = {};
for (let b of prefixes[prop].browsers) {
let [name, version, flex_spec] = b.split(' ');
if (BROWSER_MAPPING[name] === null) {
continue;
}
let prefix = browsers[name].prefix_exceptions?.[version] || browsers[name].prefix;
name = BROWSER_MAPPING[name] || name;
let v = parseVersion(version);
if (v == null) {
console.log('BAD VERSION', prop, name, version);
continue;
}
if (browserMap[name]?.[prefix] == null) {
browserMap[name] = browserMap[name] || {};
browserMap[name][prefix] = [v, v];
} else {
if (v < browserMap[name][prefix][0]) {
browserMap[name][prefix][0] = v;
}
if (v > browserMap[name][prefix][1]) {
browserMap[name][prefix][1] = v;
}
}
if (flex_spec === '2009') {
// console.log(prop, name, version, flex_spec);
if (flexSpec[name] == null) {
flexSpec[name] = [v, v];
} else {
if (v < flexSpec[name][0]) {
flexSpec[name][0] = v;
}
if (v > flexSpec[name][1]) {
flexSpec[name][1] = v;
}
}
}
}
// p[prop] = browserMap;
let s = JSON.stringify(browserMap);
let found = false;
for (let [key, val] of p) {
if (JSON.stringify(val) === s) {
key.push(prop);
found = true;
break;
}
}
if (!found) {
p.set([prop], browserMap);
}
}
let prefixMapping = {
webkit: 'WebKit',
moz: 'Moz',
ms: 'Ms',
o: 'O'
};
let enumify = (f) => f.replace(/^@([a-z])/, (_, x) => 'At' + x.toUpperCase()).replace(/^::([a-z])/, (_, x) => 'PseudoElement' + x.toUpperCase()).replace(/^:([a-z])/, (_, x) => 'PseudoClass' + x.toUpperCase()).replace(/(^|-)([a-z])/g, (_, a, x) => x.toUpperCase())
let s = `// This file is autogenerated by build-prefixes.js. DO NOT EDIT!
use serde::{Deserialize, Serialize};
use super::VendorPrefix;
#[derive(Serialize, Debug, Deserialize, Clone, Copy, Default)]
pub struct Browsers {
pub ${Object.keys(browsers).filter(b => !(b in BROWSER_MAPPING)).sort().join(': Option<u32>,\n pub ')}: Option<u32>
}
pub enum Feature {
${[...p.keys()].flat().map(enumify).sort().join(',\n ')}
}
impl Feature {
pub fn prefixes_for(&self, browsers: Browsers) -> VendorPrefix {
let mut prefixes = VendorPrefix::None;
match self {
${[...p].map(([features, versions]) => {
return `${features.map(name => `Feature::${enumify(name)}`).join(' |\n ')} => {
${Object.entries(versions).map(([name, prefixes]) => {
return `if let Some(version) = browsers.${name} {
${Object.entries(prefixes).map(([prefix, [min, max]]) => {
if (!prefixMapping[prefix]) {
throw new Error('Missing prefix ' + prefix);
}
return `if version >= ${min} && version <= ${max} {
prefixes |= VendorPrefix::${prefixMapping[prefix]};
}`
}).join('\n ')}
}`;
}).join('\n ')}
}`
}).join(',\n ')}
}
prefixes
}
}
pub fn is_flex_2009(browsers: Browsers) -> bool {
${Object.entries(flexSpec).map(([name, [min, max]]) => {
return `if let Some(version) = browsers.${name} {
if version >= ${min} && version <= ${max} {
return true;
}
}`;
}).join('\n ')}
false
}
`;
fs.writeFileSync('src/properties/prefixes.rs', s);
function parseVersion(version) {
let [major, minor = '0', patch = '0'] = version
.split('-')[0]
.split('.')
.map(v => parseInt(v, 10));
if (isNaN(major) || isNaN(minor) || isNaN(patch)) {
return null;
}
return major << 16 | minor << 8 | patch;
}