@@ -3,6 +3,7 @@ const parseObjectStyles = require('tailwindcss/lib/util/parseObjectStyles').defa
3
3
const { isPlainObject, bigSign } = require ( './utils' )
4
4
const selectorParser = require ( 'postcss-selector-parser' )
5
5
const prefixSelector = require ( 'tailwindcss/lib/util/prefixSelector' ) . default
6
+ const { updateAllClasses } = require ( '../pluginUtils' )
6
7
7
8
let classNameParser = selectorParser ( ( selectors ) => {
8
9
return selectors . first . filter ( ( { type } ) => type === 'class' ) . pop ( ) . value
@@ -64,6 +65,26 @@ function applyPrefix(matches, context) {
64
65
return matches
65
66
}
66
67
68
+ function applyImportant ( matches ) {
69
+ if ( matches . length === 0 ) {
70
+ return matches
71
+ }
72
+ let result = [ ]
73
+
74
+ for ( let [ meta , rule ] of matches ) {
75
+ let container = postcss . root ( { nodes : [ rule ] } )
76
+ container . walkRules ( ( r ) => {
77
+ r . selector = updateAllClasses ( r . selector , ( className ) => {
78
+ return `!${ className } `
79
+ } )
80
+ r . walkDecls ( ( d ) => ( d . important = true ) )
81
+ } )
82
+ result . push ( [ meta , container . nodes [ 0 ] ] )
83
+ }
84
+
85
+ return result
86
+ }
87
+
67
88
// Takes a list of rule tuples and applies a variant like `hover`, sm`,
68
89
// whatever to it. We used to do some extra caching here to avoid generating
69
90
// a variant of the same rule more than once, but this was never hit because
@@ -179,6 +200,12 @@ function sortAgainst(toSort, against) {
179
200
function * resolveMatches ( candidate , context ) {
180
201
let separator = context . tailwindConfig . separator
181
202
let [ classCandidate , ...variants ] = candidate . split ( separator ) . reverse ( )
203
+ let important = false
204
+
205
+ if ( classCandidate . startsWith ( '!' ) ) {
206
+ important = true
207
+ classCandidate = classCandidate . slice ( 1 )
208
+ }
182
209
183
210
// Strip prefix
184
211
// md:hover:tw-bg-black
@@ -220,6 +247,10 @@ function* resolveMatches(candidate, context) {
220
247
221
248
matches = applyPrefix ( matches , context )
222
249
250
+ if ( important ) {
251
+ matches = applyImportant ( matches , context )
252
+ }
253
+
223
254
for ( let variant of variants ) {
224
255
matches = applyVariant ( variant , matches , context )
225
256
}
0 commit comments