@@ -43,18 +43,24 @@ function expandApplyAtRules(context) {
4343 let applyCandidates = new Set ( )
4444
4545 // Collect all @apply rules and candidates
46- let applies = [ ]
46+ let ruleApplies = new Map ( )
4747 root . walkAtRules ( 'apply' , ( rule ) => {
4848 let [ candidates , important ] = extractApplyCandidates ( rule . params )
4949
50+ if ( ! ruleApplies . has ( rule . parent ) ) {
51+ ruleApplies . set ( rule . parent , new Set ( ) )
52+ }
53+
54+ let applies = ruleApplies . get ( rule . parent )
5055 for ( let util of candidates ) {
56+ applies . add ( [ util , important ] )
5157 applyCandidates . add ( util )
5258 }
53- applies . push ( rule )
59+ rule . remove ( )
5460 } )
5561
5662 // Start the @apply process if we have rules with @apply in them
57- if ( applies . length > 0 ) {
63+ if ( ruleApplies . size > 0 ) {
5864 // Fill up some caches!
5965 let applyClassCache = buildApplyCache ( applyCandidates , context )
6066
@@ -96,11 +102,9 @@ function expandApplyAtRules(context) {
96102 . join ( ', ' )
97103 }
98104
99- for ( let apply of applies ) {
105+ for ( let [ applyParentRule , applyCandidates ] of ruleApplies . entries ( ) ) {
100106 let siblings = [ ]
101- let [ applyCandidates , important ] = extractApplyCandidates ( apply . params )
102-
103- for ( let applyCandidate of applyCandidates ) {
107+ for ( let [ applyCandidate , important ] of applyCandidates ) {
104108 if ( ! applyClassCache . has ( applyCandidate ) ) {
105109 throw apply . error (
106110 `The \`${ applyCandidate } \` class does not exist. If \`${ applyCandidate } \` is a custom class, make sure it is defined within a \`@layer\` directive.`
@@ -113,7 +117,11 @@ function expandApplyAtRules(context) {
113117 let root = postcss . root ( { nodes : [ node . clone ( ) ] } )
114118
115119 root . walkRules ( ( rule ) => {
116- rule . selector = replaceSelector ( apply . parent . selector , rule . selector , applyCandidate )
120+ rule . selector = replaceSelector (
121+ applyParentRule . selector ,
122+ rule . selector ,
123+ applyCandidate
124+ )
117125 rule . walkDecls ( ( d ) => {
118126 d . important = important
119127 } )
@@ -125,16 +133,13 @@ function expandApplyAtRules(context) {
125133
126134 // Inject the rules, sorted, correctly
127135 for ( let [ , sibling ] of siblings . sort ( ( [ a ] , [ z ] ) => bigSign ( z . sort - a . sort ) ) ) {
128- // `apply.parent ` is referring to the node at `.abc` in: .abc { @apply mt-2 }
129- apply . parent . after ( sibling )
136+ // `applyParentRule ` is referring to the node at `.abc` in: .abc { @apply mt-2 }
137+ applyParentRule . after ( sibling )
130138 }
131139
132- // If there are left-over declarations, just remove the @apply
133- if ( apply . parent . nodes . length > 1 ) {
134- apply . remove ( )
135- } else {
136- // The node is empty, drop the full node
137- apply . parent . remove ( )
140+ // The node is empty, drop the full node
141+ if ( applyParentRule . nodes . length === 0 ) {
142+ applyParentRule . remove ( )
138143 }
139144 }
140145 }
0 commit comments