Skip to content

Add @silent at-rule #169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions __tests__/fixtures/tailwind-input.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
@responsive {
.example {
@apply .font-bold;
@apply .delete-me;
color: config('colors.red');
}
}

@silent {
.delete-me {
background-color: blue;
}
}
5 changes: 5 additions & 0 deletions __tests__/fixtures/tailwind-output.css
Original file line number Diff line number Diff line change
Expand Up @@ -3547,6 +3547,7 @@ button,

.example {
font-weight: 700;
background-color: blue;
color: #e3342f;
}

Expand Down Expand Up @@ -6511,6 +6512,7 @@ button,

.sm\:example {
font-weight: 700;
background-color: blue;
color: #e3342f;
}
}
Expand Down Expand Up @@ -9476,6 +9478,7 @@ button,

.md\:example {
font-weight: 700;
background-color: blue;
color: #e3342f;
}
}
Expand Down Expand Up @@ -12441,6 +12444,7 @@ button,

.lg\:example {
font-weight: 700;
background-color: blue;
color: #e3342f;
}
}
Expand Down Expand Up @@ -15406,6 +15410,7 @@ button,

.xl\:example {
font-weight: 700;
background-color: blue;
color: #e3342f;
}
}
55 changes: 55 additions & 0 deletions __tests__/silentAtRule.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import _ from 'lodash'
import postcss from 'postcss'
import tailwind from '../src/index'

it('removes silenced rules while still making them available to @apply', () => {
const input = _.trim(`
@silent {
@responsive {
.foo {
color: red;
}
}
@tailwind screens;
}


.bar {
@apply .foo;
}
`)

const expected = _.trim(`
.bar {
color: red;
}
`)

return postcss([tailwind()])
.process(input)
.then(result => {
expect(result.css).toBe(expected)
})
})

it('throws an error if @silent is used anywhere but the root of the tree', () => {
const input = _.trim(`
@media (min-width: 100px) {
@silent {
.foo {
color: red;
}
}
.bar {
@apply .foo;
}
}
`)

expect.assertions(1)
return postcss([tailwind()])
.process(input)
.catch(e => {
expect(e).toMatchObject({ name: 'CssSyntaxError' })
})
})
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import substituteFocusableAtRules from './lib/substituteFocusableAtRules'
import substituteResponsiveAtRules from './lib/substituteResponsiveAtRules'
import substituteScreenAtRules from './lib/substituteScreenAtRules'
import substituteClassApplyAtRules from './lib/substituteClassApplyAtRules'
import substituteSilentAtRules from './lib/substituteSilentAtRules'

const plugin = postcss.plugin('tailwind', config => {
const plugins = []
Expand Down Expand Up @@ -41,6 +42,7 @@ const plugin = postcss.plugin('tailwind', config => {
substituteResponsiveAtRules(lazyConfig),
substituteScreenAtRules(lazyConfig),
substituteClassApplyAtRules(lazyConfig),
substituteSilentAtRules(lazyConfig),
stylefmt,
]
)
Expand Down
11 changes: 11 additions & 0 deletions src/lib/substituteSilentAtRules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default function() {
return function(css) {
css.walkAtRules('silent', atRule => {
if (atRule.parent.type !== 'root') {
throw atRule.error(`@silent at-rules cannot be nested.`)
}

atRule.remove()
})
}
}
10 changes: 9 additions & 1 deletion src/util/findMixin.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import _ from 'lodash'

function ruleIsValidMixin(rule) {
if (rule.parent.type === 'root') {
return true
}

return rule.parent.type === 'atrule' && ['silent'].includes(rule.parent.name)
}

export default function findMixin(css, mixin, onError) {
const matches = []

css.walkRules(rule => {
if (rule.selectors.includes(mixin) && rule.parent.type === 'root') {
if (rule.selectors.includes(mixin) && ruleIsValidMixin(rule)) {
matches.push(rule)
}
})
Expand Down