Skip to content
This repository was archived by the owner on Apr 6, 2021. It is now read-only.

Fix multiple @apply involving responsive rules #151

Merged
merged 2 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions src/lib/expandApplyAtRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,15 @@ function expandApplyAtRules(context) {
.join(', ')
}

/** @type {Map<import('postcss').Node, [string, boolean, import('postcss').Node[]][]>} */
let perParentApplies = new Map()

// Collect all apply candidates and their rules
for (let apply of applies) {
let siblings = []
let candidates = perParentApplies.get(apply.parent) || []

perParentApplies.set(apply.parent, candidates)

let [applyCandidates, important] = extractApplyCandidates(apply.params)

for (let applyCandidate of applyCandidates) {
Expand All @@ -115,13 +122,22 @@ function expandApplyAtRules(context) {

let rules = applyClassCache.get(applyCandidate)

candidates.push([applyCandidate, important, rules])
}
}

for (const [parent, candidates] of perParentApplies) {
let siblings = []

for (let [applyCandidate, important, rules] of candidates) {
for (let [meta, node] of rules) {
let root = postcss.root({ nodes: [node.clone()] })
let canRewriteSelector = node.type !== 'atrule' || (node.type === 'atrule' && node.name !== 'keyframes');
let canRewriteSelector =
node.type !== 'atrule' || (node.type === 'atrule' && node.name !== 'keyframes')

if (canRewriteSelector) {
root.walkRules((rule) => {
rule.selector = replaceSelector(apply.parent.selector, rule.selector, applyCandidate)
rule.selector = replaceSelector(parent.selector, rule.selector, applyCandidate)

rule.walkDecls((d) => {
d.important = important
Expand All @@ -134,11 +150,13 @@ function expandApplyAtRules(context) {
}

// Inject the rules, sorted, correctly
const nodes = siblings.sort(([a], [z]) => bigSign(a.sort - z.sort)).map(s => s[1])
const nodes = siblings.sort(([a], [z]) => bigSign(a.sort - z.sort)).map((s) => s[1])

// `apply.parent` is referring to the node at `.abc` in: .abc { @apply mt-2 }
apply.parent.after(nodes)
// `parent` refers to the node at `.abc` in: .abc { @apply mt-2 }
parent.after(nodes)
}

for (let apply of applies) {
// If there are left-over declarations, just remove the @apply
if (apply.parent.nodes.length > 1) {
apply.remove()
Expand Down
32 changes: 32 additions & 0 deletions tests/10-apply.test.css
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,38 @@
color: green;
font-weight: 700;
}
h1 {
font-size: 1.5rem;
line-height: 2rem;
}
@media (min-width: 640px) {
h1 {
font-size: 1.875rem;
line-height: 2.25rem;
}
}
@media (min-width: 1024px) {
h1 {
font-size: 1.5rem;
line-height: 2rem;
}
}
h2 {
font-size: 1.5rem;
line-height: 2rem;
}
@media (min-width: 640px) {
h2 {
font-size: 1.5rem;
line-height: 2rem;
}
}
@media (min-width: 1024px) {
h2 {
font-size: 1.5rem;
line-height: 2rem;
}
}
@keyframes spin {
to {
transform: rotate(360deg);
Expand Down
9 changes: 9 additions & 0 deletions tests/10-apply.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ test('@apply', () => {
.use-with-other-properties-component {
@apply use-with-other-properties-base;
}

h1 {
@apply text-2xl lg:text-2xl sm:text-3xl;
}
h2 {
@apply text-2xl;
@apply lg:text-2xl;
@apply sm:text-2xl;
}
}

@layer utilities {
Expand Down