-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
53 lines (45 loc) · 1.45 KB
/
index.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
const valueParser = require('postcss-value-parser');
/**
* @type {import('postcss').PluginCreator}
*/
module.exports = (opts = {}) => {
const { format } = Object(opts);
return {
postcssPlugin: 'postcss-remove-font-face-format',
AtRule: {
"font-face"(rule) {
rule.walkDecls("src", (node) => {
// If no format is defined we don't need to do anything
if (!node.value.includes("format(")) {
return;
}
// Split individual font-face sources
const sources = node.value.split(",");
// Filter to sources we want to keep
const filteredSources = sources.filter(source => {
const values = valueParser(source);
let remove = false;
values.walk((value) => {
// We are only interested in the format value
if (value.type !== "function" || value.value !== "format")
{
return;
}
value.nodes.forEach((child) => {
// Check if format should be removed
if (child.type == "string" && child.value == format)
{
remove = true;
}
});
});
return !remove;
});
// Write filtered sources back to node
node.value = filteredSources.join(', ');
});
}
}
}
}
module.exports.postcss = true