-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.mjs
53 lines (47 loc) · 1.52 KB
/
index.mjs
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
// tooling
import getSystemUiFamily from "./lib/get-system-ui-family.mjs";
import browserslist from "browserslist";
// plugin
const creator = (opts) => {
const { family, preserve = true, browsers } = Object(opts);
let systemUiFamily = family;
if (typeof family === "string") {
systemUiFamily = family.trim().split(/\s*,\s*/);
} else if (!family) {
// browsers supported by the configuration
const supportedBrowsers = browserslist(browsers, {
ignoreUnknownVersions: true,
});
systemUiFamily = getSystemUiFamily(supportedBrowsers, preserve);
}
// system-ui and fallbacks match
const whitespace = "[\\f\\n\\r\\x09\\x20]";
const systemUiMatch = new RegExp(
`(^|,|${whitespace}+)(?:system-ui${whitespace}*)(?:,${whitespace}*(?:${systemUiFamily.join(
"|"
)})${whitespace}*)?(,|$)`,
"i"
);
// system-ui fallback replacement
const systemUiReplace = `$1${systemUiFamily.join(", ")}$2`;
const visited = new WeakSet();
return {
postcssPlugin: "postcss-font-family-system-ui",
// update font declarations to polyfill system-ui usage
Declaration(decl) {
if (decl.prop.match(fontPropertyMatch) && !visited.has(decl)) {
decl.value = decl.value.replace(systemUiMatch, systemUiReplace);
visited.add(decl);
}
},
};
};
// Required by PostCSS 8
creator.postcss = true;
export default creator;
/* match the following properties:
* - font
* - font-family
* - custom properties (see https://www.w3.org/TR/css-variables-1/#custom-property)
*/
const fontPropertyMatch = /(?:^(?:-|\\002d){2})|(?:^font(?:-family)?$)/i;