MCPcopy Index your code
hub / github.com/callstack/linaria / collect

Function collect

packages/server/src/collect.ts:53–163  ·  view source on GitHub ↗
(
  html: string,
  css: string,
  classnameModifiers?: ClassnameModifiers
)

Source from the content-addressed store, hash-verified

51 * @returns {CollectResult} object containing the critical and other CSS styles
52 */
53export default function collect(
54 html: string,
55 css: string,
56 classnameModifiers?: ClassnameModifiers
57): CollectResult {
58 const animations = new Set();
59 const other = postcss.root();
60 const critical = postcss.root();
61 const stylesheet = postcss.parse(css);
62 const ignoredClasses = classnameModifiers?.ignoredClasses ?? [];
63 const blockedClasses = classnameModifiers?.blockedClasses ?? [];
64
65 const htmlClassesRegExp = extractClassesFromHtml(html, ignoredClasses);
66 const blockedClassesSanitized = blockedClasses.map(escapeRegex);
67 const blockedClassesRegExp = new RegExp(
68 blockedClassesSanitized.join('|'),
69 'gm'
70 );
71
72 const isCritical = (rule: ChildNode) => {
73 // Only check class names selectors
74 if ('selector' in rule && rule.selector.startsWith('.')) {
75 const isExcluded =
76 blockedClasses.length > 0 && blockedClassesRegExp.test(rule.selector);
77 if (isExcluded) return false;
78
79 return Boolean(rule.selector.match(htmlClassesRegExp));
80 }
81
82 return true;
83 };
84
85 const handleAtRule = (rule: AtRule) => {
86 if (rule.name === 'keyframes') {
87 return;
88 }
89
90 const criticalRule = rule.clone();
91 const otherRule = rule.clone();
92
93 let removedNodesFromOther = 0;
94 criticalRule.each((childRule: ChildNode, index: number) => {
95 if (isCritical(childRule)) {
96 otherRule.nodes[index - removedNodesFromOther]?.remove();
97 removedNodesFromOther += 1;
98 } else {
99 childRule.remove();
100 }
101 });
102
103 rule.remove();
104
105 if (criticalRule.nodes.length > 0) {
106 critical.append(criticalRule);
107 }
108 if (otherRule.nodes.length > 0) {
109 other.append(otherRule);
110 }

Callers 3

server.jsxFile · 0.90
testCollectFunction · 0.85
collect.test.tsFile · 0.85

Calls 5

extractClassesFromHtmlFunction · 0.85
handleAtRuleFunction · 0.85
isCriticalFunction · 0.85
rootMethod · 0.80
toStringMethod · 0.80

Tested by 1

testCollectFunction · 0.68