@@ -3,6 +3,7 @@ const parseObjectStyles = require('tailwindcss/lib/util/parseObjectStyles').defa
33const { isPlainObject, bigSign } = require ( './utils' )
44const selectorParser = require ( 'postcss-selector-parser' )
55const prefixSelector = require ( 'tailwindcss/lib/util/prefixSelector' ) . default
6+ const { updateAllClasses } = require ( '../pluginUtils' )
67
78let classNameParser = selectorParser ( ( selectors ) => {
89 return selectors . first . filter ( ( { type } ) => type === 'class' ) . pop ( ) . value
@@ -64,6 +65,26 @@ function applyPrefix(matches, context) {
6465 return matches
6566}
6667
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+
6788// Takes a list of rule tuples and applies a variant like `hover`, sm`,
6889// whatever to it. We used to do some extra caching here to avoid generating
6990// a variant of the same rule more than once, but this was never hit because
@@ -179,6 +200,12 @@ function sortAgainst(toSort, against) {
179200function * resolveMatches ( candidate , context ) {
180201 let separator = context . tailwindConfig . separator
181202 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+ }
182209
183210 // Strip prefix
184211 // md:hover:tw-bg-black
@@ -220,6 +247,10 @@ function* resolveMatches(candidate, context) {
220247
221248 matches = applyPrefix ( matches , context )
222249
250+ if ( important ) {
251+ matches = applyImportant ( matches , context )
252+ }
253+
223254 for ( let variant of variants ) {
224255 matches = applyVariant ( variant , matches , context )
225256 }
0 commit comments