Skip to content

Commit

Permalink
feat: add exclusion functionality in handleAtRule
Browse files Browse the repository at this point in the history
  • Loading branch information
Ezlie Nguyen committed Nov 7, 2022
1 parent 8cc05c3 commit 68c5ddb
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 16 deletions.
8 changes: 6 additions & 2 deletions packages/server/__tests__/__snapshots__/collect.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,19 @@ exports[`include atrule once critical 1`] = `
h1 {
font-size: 20px;
}
}
"
`;

exports[`include atrule once other 1`] = `
"@media screen {
.class {
font-size: 15px;
}
}
"
`;

exports[`include atrule once other 1`] = `""`;

exports[`simple class name critical 1`] = `
".linaria {
}
Expand Down
36 changes: 36 additions & 0 deletions packages/server/__tests__/collect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,39 @@ test('does not match selectors in excludedClasses list', () => {
expect(critical).toMatchInlineSnapshot(`".linaria.ltr {}.lily {}"`);
expect(other).toMatchInlineSnapshot(`".linaria.rtl {}"`);
});

test('does not match child selectors in excludedClasses list for media queries', () => {
const code = dedent`
<div class="lily linaria ltr dir"></div>
`;

const css = dedent`
@media only screen and (max-width:561.5px) {
.dir-ltr.left_6_3_l9xil3s {
padding-left: 24px !important;
}
.dir-rtl.left_6_3_l9xil3s {
padding-right: 24px !important;
}
}
`;

const { critical, other } = collect(code, css, [], ['dir-rtl']);

expect(critical).toMatchInlineSnapshot(`
"@media only screen and (max-width:561.5px) {
.dir-ltr.left_6_3_l9xil3s {
padding-left: 24px !important;
}
}"
`);
expect(other).toMatchInlineSnapshot(`
"@media only screen and (max-width:561.5px) {
.dir-rtl.left_6_3_l9xil3s {
padding-right: 24px !important;
}
}"
`);
});
35 changes: 21 additions & 14 deletions packages/server/src/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,32 +62,39 @@ export default function collect(
if ('selector' in rule && rule.selector.startsWith('.')) {
const isExcluded =
excludedClasses.length > 0 && excludedClassesRegExp.test(rule.selector);
if (isExcluded) return false;

const isMatch = Boolean(rule.selector.match(htmlClassesRegExp));
return isMatch && !isExcluded;
return Boolean(rule.selector.match(htmlClassesRegExp));
}

return true;
};

const handleAtRule = (rule: AtRule) => {
let addedToCritical = false;
if (rule.name === 'keyframes') {
return;
}

rule.each((childRule) => {
if (isCritical(childRule) && !addedToCritical) {
critical.append(rule.clone());
addedToCritical = true;
const criticalRule = rule.clone();
const otherRule = rule.clone();

let removedNodesFromOther = 0;
criticalRule.each((childRule: ChildNode, index: number) => {
if (isCritical(childRule)) {
otherRule.nodes[index - removedNodesFromOther]?.remove();
removedNodesFromOther += 1;
} else {
childRule.remove();
}
});

if (rule.name === 'keyframes') {
return;
}
rule.remove();

if (addedToCritical) {
rule.remove();
} else {
other.append(rule);
if (criticalRule.nodes.length > 0) {
critical.append(criticalRule);
}
if (otherRule.nodes.length > 0) {
other.append(otherRule);
}
};

Expand Down

0 comments on commit 68c5ddb

Please sign in to comment.