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

Commit e8b9e5b

Browse files
committed
Add basic @apply !important support
1 parent 59131fc commit e8b9e5b

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

src/lib/expandApplyAtRules.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ function extractApplyCandidates(params) {
3232
let candidates = params.split(/[\s\t\n]+/g)
3333

3434
if (candidates[candidates.length - 1] === '!important') {
35-
candidates = candidates.slice(0, -1)
35+
return [candidates.slice(0, -1), true]
3636
}
3737

38-
return candidates
38+
return [candidates, false]
3939
}
4040

4141
function expandApplyAtRules(context) {
@@ -45,7 +45,7 @@ function expandApplyAtRules(context) {
4545
// Collect all @apply rules and candidates
4646
let applies = []
4747
root.walkAtRules('apply', (rule) => {
48-
let candidates = extractApplyCandidates(rule.params)
48+
let [candidates, important] = extractApplyCandidates(rule.params)
4949

5050
for (let util of candidates) {
5151
applyCandidates.add(util)
@@ -98,7 +98,7 @@ function expandApplyAtRules(context) {
9898

9999
for (let apply of applies) {
100100
let siblings = []
101-
let applyCandidates = extractApplyCandidates(apply.params)
101+
let [applyCandidates, important] = extractApplyCandidates(apply.params)
102102

103103
for (let applyCandidate of applyCandidates) {
104104
if (!applyClassCache.has(applyCandidate)) {
@@ -110,10 +110,13 @@ function expandApplyAtRules(context) {
110110
let rules = applyClassCache.get(applyCandidate)
111111

112112
for (let [meta, node] of rules) {
113-
let root = postcss.root({ nodes: [node] })
113+
let root = postcss.root({ nodes: [node.clone()] })
114114

115115
root.walkRules((rule) => {
116116
rule.selector = replaceSelector(apply.parent.selector, rule.selector, applyCandidate)
117+
rule.walkDecls((d) => {
118+
d.important = important
119+
})
117120
})
118121

119122
siblings.push([meta, root.nodes[0]])

tests/03-important-boolean.test.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
.btn {
3939
button: yes !important;
4040
}
41+
.custom-component {
42+
font-weight: 700;
43+
}
44+
.custom-important-component {
45+
text-align: center !important;
46+
}
4147
.font-bold {
4248
font-weight: 700 !important;
4349
}

tests/03-important-boolean.test.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<div class="container"></div>
22
<div class="btn"></div>
33
<div class="custom-util"></div>
4+
<div class="custom-component"></div>
5+
<div class="custom-important-component"></div>
46
<div class="font-bold"></div>
57
<div class="md:hover:text-right"></div>
68
<div class="motion-safe:hover:text-center"></div>

tests/03-important-boolean.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ test('important boolean', () => {
3939
let css = `
4040
@tailwind base;
4141
@tailwind components;
42+
@layer components {
43+
.custom-component {
44+
@apply font-bold;
45+
}
46+
.custom-important-component {
47+
@apply text-center !important;
48+
}
49+
}
4250
@tailwind utilities;
4351
`
4452

0 commit comments

Comments
 (0)