Skip to content

Commit

Permalink
fix: add !important only to modifiers
Browse files Browse the repository at this point in the history
fixes a specificity issue where you couldn't override an existing class, like when passing `text-base` to a component that already has `text-sm`
  • Loading branch information
cossssmin committed Mar 21, 2023
1 parent 0641846 commit d4e54da
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/generators/tailwindcss.js
Expand Up @@ -8,6 +8,22 @@ const {requireUncached} = require('../utils/helpers')
const mergeLonghand = require('postcss-merge-longhand')
const {get, isObject, isEmpty, merge} = require('lodash')

const addImportantPlugin = () => {
return {
postcssPlugin: 'add-important',
Rule(rule) {
const shouldAddImportant = get(rule, 'raws.tailwind.layer') === 'variants'
|| get(rule, 'parent.type') === 'atrule'

if (shouldAddImportant) {
rule.walkDecls(decl => {
decl.important = true
})
}
}
}
}

module.exports = {
compile: async ({css = '', html = '', config = {}}) => {
// Compute the Tailwind config to use
Expand Down Expand Up @@ -43,7 +59,6 @@ module.exports = {
'./src/components/**/*.html'

const tailwindConfig = merge({
important: true,
content: {
files: [
layoutsPath,
Expand Down Expand Up @@ -105,6 +120,7 @@ module.exports = {
const toProcess = [
postcssNested(),
tailwindcss(tailwindConfig),
get(tailwindConfig, 'important') === false ? () => {} : addImportantPlugin(),
get(config, 'shorthandCSS', get(config, 'shorthandInlineCSS')) === true ?
mergeLonghand() :
() => {},
Expand Down
6 changes: 6 additions & 0 deletions test/stubs/tailwind/add-important.html
@@ -0,0 +1,6 @@
sm:block
hover:sm:flex
hover:opacity-50
[&_p]:mt-4
[@media(any-hover:hover){&:hover}]:uppercase
[border:0]
43 changes: 41 additions & 2 deletions test/test-tailwindcss.js
Expand Up @@ -114,12 +114,12 @@ test('uses custom postcss plugins from the maizzle config', async t => {

const css = await Tailwind.compile({
css: '.test {transform: scale(0.5)}',
html: '<div class="test inline">Test</div>',
html: '<div class="test">Test</div>',
config
})

t.not(css, undefined)
t.is(css.trim(), '.inline {display: inline !important} .test {-webkit-transform: scale(0.5);transform: scale(0.5)}')
t.is(css.trim(), '.test {-webkit-transform: scale(0.5);transform: scale(0.5)}')
})

test('respects `shorthandCSS` in maizzle config', async t => {
Expand Down Expand Up @@ -184,3 +184,42 @@ test('works with custom `components.root`', async t => {

t.true(css.includes('.flex'))
})

test('adds `!important` to selectors that will not be inlined', async t => {
const css = await Tailwind.compile({
config: {
build: {
tailwind: {
config: {
content: ['./test/stubs/tailwind/*.html']
}
}
}
}
})

t.true(css.includes('display: block !important'))
t.true(css.includes('display: flex !important'))
t.true(css.includes('opacity: 0.5 !important'))
t.true(css.includes('margin-top: 1rem !important'))
t.true(css.includes('text-transform: uppercase !important'))

t.false(css.includes('border: 0 !important'))
})

test('respects `important: false` from tailwind config', async t => {
const css = await Tailwind.compile({
config: {
build: {
tailwind: {
config: {
important: false,
content: ['./test/stubs/tailwind/*.html']
}
}
}
}
})

t.false(css.includes('display: block !important'))
})

0 comments on commit d4e54da

Please sign in to comment.