Skip to content

Commit 59131fc

Browse files
committed
Basic important support
1 parent 762564e commit 59131fc

File tree

5 files changed

+152
-7
lines changed

5 files changed

+152
-7
lines changed

src/lib/generateRules.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,14 @@ function* resolveMatches(candidate, context) {
168168
for (let ruleSet of [].concat(plugin(modifier, pluginHelpers))) {
169169
let [rules, options] = parseRules(ruleSet, context.postCssNodeCache)
170170
for (let rule of rules) {
171-
matches.push([{ ...sort, options }, rule])
171+
matches.push([{ ...sort, options: { ...sort.options, ...options } }, rule])
172172
}
173173
}
174174
} else {
175175
let ruleSet = plugin
176176
let [rules, options] = parseRules(ruleSet, context.postCssNodeCache)
177177
for (let rule of rules) {
178-
matches.push([{ ...sort, options }, rule])
178+
matches.push([{ ...sort, options: { ...sort.options, ...options } }, rule])
179179
}
180180
}
181181
}
@@ -216,7 +216,14 @@ function generateRules(candidates, context) {
216216
allRules.push(matches)
217217
}
218218

219-
return allRules.flat(1).map(([{ sort, layer }, rule]) => [sort | context.layerOrder[layer], rule])
219+
return allRules.flat(1).map(([{ sort, layer, options }, rule]) => {
220+
if (context.tailwindConfig.important === true && options.respectImportant) {
221+
rule.walkDecls((d) => {
222+
d.important = true
223+
})
224+
}
225+
return [sort | context.layerOrder[layer], rule]
226+
})
220227
}
221228

222229
module.exports = {

src/lib/setupContext.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,9 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
409409
context.candidateRuleMap.set(identifier, [])
410410
}
411411

412-
context.candidateRuleMap.get(identifier).push([{ sort: offset, layer: 'components' }, rule])
412+
context.candidateRuleMap
413+
.get(identifier)
414+
.push([{ sort: offset, layer: 'components', options }, rule])
413415
}
414416
},
415417
addUtilities(utilities, options) {
@@ -433,7 +435,9 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
433435
context.candidateRuleMap.set(identifier, [])
434436
}
435437

436-
context.candidateRuleMap.get(identifier).push([{ sort: offset, layer: 'utilities' }, rule])
438+
context.candidateRuleMap
439+
.get(identifier)
440+
.push([{ sort: offset, layer: 'utilities', options }, rule])
437441
}
438442
},
439443
matchBase: function (base) {
@@ -451,13 +455,22 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
451455
context.candidateRuleMap.get(identifier).push(...withOffsets)
452456
}
453457
},
454-
matchUtilities: function (utilities) {
458+
matchUtilities: function (utilities, options) {
459+
let defaultOptions = {
460+
variants: [],
461+
respectPrefix: true,
462+
respectImportant: true,
463+
respectVariants: true,
464+
}
465+
466+
options = { ...defaultOptions, ...options }
467+
455468
let offset = offsets.utilities++
456469

457470
for (let identifier in utilities) {
458471
let value = [].concat(utilities[identifier])
459472

460-
let withOffsets = value.map((rule) => [{ sort: offset, layer: 'utilities' }, rule])
473+
let withOffsets = value.map((rule) => [{ sort: offset, layer: 'utilities', options }, rule])
461474

462475
if (!context.candidateRuleMap.has(identifier)) {
463476
context.candidateRuleMap.set(identifier, [])
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
* {
2+
--tw-shadow: 0 0 #0000;
3+
--tw-ring-inset: var(--tw-empty, /*!*/ /*!*/);
4+
--tw-ring-offset-width: 0px;
5+
--tw-ring-offset-color: #fff;
6+
--tw-ring-color: rgba(59, 130, 246, 0.5);
7+
--tw-ring-offset-shadow: 0 0 #0000;
8+
--tw-ring-shadow: 0 0 #0000;
9+
}
10+
.container {
11+
width: 100%;
12+
}
13+
@media (min-width: 640px) {
14+
.container {
15+
max-width: 640px;
16+
}
17+
}
18+
@media (min-width: 768px) {
19+
.container {
20+
max-width: 768px;
21+
}
22+
}
23+
@media (min-width: 1024px) {
24+
.container {
25+
max-width: 1024px;
26+
}
27+
}
28+
@media (min-width: 1280px) {
29+
.container {
30+
max-width: 1280px;
31+
}
32+
}
33+
@media (min-width: 1536px) {
34+
.container {
35+
max-width: 1536px;
36+
}
37+
}
38+
.btn {
39+
button: yes !important;
40+
}
41+
.font-bold {
42+
font-weight: 700 !important;
43+
}
44+
.custom-util {
45+
button: no;
46+
}
47+
.group:hover .group-hover\:focus-within\:text-left:focus-within {
48+
text-align: left !important;
49+
}
50+
[dir='rtl'] .rtl\:active\:text-center:active {
51+
text-align: center !important;
52+
}
53+
@media (prefers-reduced-motion: no-preference) {
54+
.motion-safe\:hover\:text-center:hover {
55+
text-align: center !important;
56+
}
57+
}
58+
.dark .dark\:focus\:text-left:focus {
59+
text-align: left !important;
60+
}
61+
@media (min-width: 768px) {
62+
.md\:hover\:text-right:hover {
63+
text-align: right !important;
64+
}
65+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<div class="container"></div>
2+
<div class="btn"></div>
3+
<div class="custom-util"></div>
4+
<div class="font-bold"></div>
5+
<div class="md:hover:text-right"></div>
6+
<div class="motion-safe:hover:text-center"></div>
7+
<div class="dark:focus:text-left"></div>
8+
<div class="group-hover:focus-within:text-left"></div>
9+
<div class="rtl:active:text-center"></div>

tests/03-important-boolean.test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const postcss = require('postcss')
2+
const tailwind = require('../src/index.js')
3+
const fs = require('fs')
4+
const path = require('path')
5+
6+
function run(input, config = {}) {
7+
return postcss([tailwind(config)]).process(input, { from: path.resolve(__filename) })
8+
}
9+
10+
test('important boolean', () => {
11+
let config = {
12+
important: true,
13+
darkMode: 'class',
14+
purge: [path.resolve(__dirname, './03-important-boolean.test.html')],
15+
corePlugins: { preflight: false },
16+
theme: {},
17+
plugins: [
18+
function ({ addComponents, addUtilities }) {
19+
addComponents(
20+
{
21+
'.btn': {
22+
button: 'yes',
23+
},
24+
},
25+
{ respectImportant: true }
26+
)
27+
addUtilities(
28+
{
29+
'.custom-util': {
30+
button: 'no',
31+
},
32+
},
33+
{ respectImportant: false }
34+
)
35+
},
36+
],
37+
}
38+
39+
let css = `
40+
@tailwind base;
41+
@tailwind components;
42+
@tailwind utilities;
43+
`
44+
45+
return run(css, config).then((result) => {
46+
let expectedPath = path.resolve(__dirname, './03-important-boolean.test.css')
47+
let expected = fs.readFileSync(expectedPath, 'utf8')
48+
49+
expect(result.css).toMatchCss(expected)
50+
})
51+
})

0 commit comments

Comments
 (0)