source: frontend/node_modules/postcss-discard-overridden/src/index.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.1 KB
Line 
1'use strict';
2const OVERRIDABLE_RULES = new Set(['keyframes', 'counter-style']);
3const SCOPE_RULES = new Set(['media', 'supports']);
4
5/**
6 * @param {string} prop
7 * @return {string}
8 */
9function vendorUnprefixed(prop) {
10 return prop.replace(/^-\w+-/, '');
11}
12
13/**
14 * @param {string} name
15 * @return {boolean}
16 */
17function isOverridable(name) {
18 return OVERRIDABLE_RULES.has(vendorUnprefixed(name.toLowerCase()));
19}
20
21/**
22 * @param {string} name
23 * @return {boolean}
24 */
25function isScope(name) {
26 return SCOPE_RULES.has(vendorUnprefixed(name.toLowerCase()));
27}
28
29/**
30 * @param {import('postcss').AtRule} node
31 * @return {string}
32 */
33function getScope(node) {
34 /** @type {import('postcss').Container<import('postcss').ChildNode> | import('postcss').Document | undefined} */
35 let current = node.parent;
36
37 const chain = [node.name.toLowerCase(), node.params];
38
39 while (current) {
40 if (
41 current.type === 'atrule' &&
42 isScope(/** @type import('postcss').AtRule */ (current).name)
43 ) {
44 chain.unshift(
45 /** @type import('postcss').AtRule */ (current).name +
46 ' ' +
47 /** @type import('postcss').AtRule */ (current).params
48 );
49 }
50 current = current.parent;
51 }
52
53 return chain.join('|');
54}
55
56/**
57 * @type {import('postcss').PluginCreator<void>}
58 * @return {import('postcss').Plugin}
59 */
60function pluginCreator() {
61 return {
62 postcssPlugin: 'postcss-discard-overridden',
63 prepare() {
64 const cache = new Map();
65 /** @type {{node: import('postcss').AtRule, scope: string}[]} */
66 const rules = [];
67
68 return {
69 OnceExit(css) {
70 css.walkAtRules((node) => {
71 if (isOverridable(node.name)) {
72 const scope = getScope(node);
73
74 cache.set(scope, node);
75 rules.push({
76 node,
77 scope,
78 });
79 }
80 });
81
82 rules.forEach((rule) => {
83 if (cache.get(rule.scope) !== rule.node) {
84 rule.node.remove();
85 }
86 });
87 },
88 };
89 },
90 };
91}
92
93pluginCreator.postcss = true;
94module.exports = pluginCreator;
Note: See TracBrowser for help on using the repository browser.