Unknown at rule @apply... k thx bye!

Published on

Chances are you are using VSCode, and chances are you're also using Tailwind in one of your projects. Chances are that at my first chances are... you went immediately nuh-uh and moved on (and if you're a NeoVim user I can't help you anyway, you're already doomed and lost in the recommended plugins someone chose for you and you're probably still figuring out half of them) but if I got your attention with the first two assumptions, then this post might be relevant for you!

One issue I've been having ever since using Tailwind with SCSS modules has been that VSCode goes absolutely ballistic marking every @apply as an unknown CSS rule, as the editor assumes it's one at-rule from CSS. (If you are wondering what at-rules are, @import and @media might sound a bit more familiar :D)

@apply is a Tailwind directive that mimics in semantics a native CSS at-rule, and this one in the specific takes care of enabling Tailwind utility classes in a .scss file ... which by now we already now — I know, we wouldn't be reading this article otherwise — so jumping to the main thing, let me present you my favourite solution from the solutions list that this article from byby.dev proposes.

Open the .vscode folder in your project, create a tailwind_directives.json file and add the following lines. I took the liberty of expanding from the article, which was talking specifically about the @tailwind directive, and take it as a chance to put all the Tailwind specific directives I could find from Tailwind's documentation:

1{ 2 "version": 1.0, 3 "atDirectives": [ 4 { 5 "name": "@tailwind", 6 "description": "Use the @tailwind directive to insert Tailwind's `base`, `components`, `utilities`, and `screens` styles into your CSS." 7 }, 8 { 9 "name": "@apply", 10 "description": "Use @apply to inline any existing utility classes into your own custom CSS." 11 }, 12 { 13 "name": "@screen", 14 "description": "The screen function allows you to create media queries that reference your breakpoints by name instead of duplicating their values in your own CSS. Apparently deprecated in favour of @media?!" 15 }, 16 { 17 "name": "@layer", 18 "description": "Use the @layer directive to tell Tailwind which “bucket” a set of custom styles belong to. Valid layers are base, components, and utilities." 19 }, 20 { 21 "name": "@config", 22 "description": "Use the @config directive to specify which config file Tailwind should use when compiling that CSS file. Do not put @config before your @import statements." 23 } 24 ] 25}

Then, head to settings.json and add these two lines:

1{ 2// ... other stuff 3 "css.customData": [".vscode/tailwind_fix.json"], 4 "scss.customData": [".vscode/tailwind_fix.json"] 5}

And voilá! Done!

Now now, you might be tempted to follow the first solution proposed by the article, which simply tells VSCode to ignore every at-rule it cannot recognise:

1{ 2// ... other stuff 3 "css.lint.unknownAtRules": "ignore" 4 "scss.lint.unknownAtRules": "ignore" 5}

But! What about typos? We all make them, and I wouldn't trust myself writing things correctly any day :'D Better be more specific and let pass only the things we are aware of!




More Snippets

declare-ai.org - ... so, who wrote that?!

Let everyone know how AI helped you... or didn't!

It starts as a joke

Colossus explaining to his children the dangers of discrimination.