This video on Deno Stylelint looks at how you can set up Stylelint in your Deno or Deno Fresh project. Stylelint is useful tooling for working with CSS, especially in teams. You define custom rules or take a set of provided recommendations. The rules stipulate how you and your team should use CSS in your project. Examples are limiting the use of duplicate selectors, avoiding named colours (accepting hex, hsl etc. only) or forbidding descending specificity. As well as keeping the styles consistent across the team, the rules can help spot errors in your CSS.
In the video, we see how you can create your own Deno script to run Stylelint on your project. We see Deno file APIs on the way, so you will probably find the video useful if you are trying Deno for the first time. I hope it is interesting either way. You can drop a comment below or reach out for a chat on Element as well as Twitter @mention if you have suggestions for improvements or questions.
stylelint.ts — click to expand code.
- Deno Fresh PostCSS article
- Stylelint docs
- GitHub repo with full code
- Element chat: #Rodney matrix chat
- Twitter handle: @askRodney
- There is not currently a specific Deno Stylelint module, though you can use the Stylelint APIs in a Deno script. We saw you can import the Node Stylelint package in Deno using the Deno NPM import syntax (`import stylelint from "npm:stylelint"`). Then the stylelint object exposes a lint method. You can specify your regular Stylelint configuration as an argument to this lint method call. You can even use predefined rule sets like `stylelint-config-recommended`.
- Sometimes, you might want to disable a Stylelint rule locally. This is possible using a comment. Add a disable comment ahead of the block which you don’t want the rule to apply to. Then add an enable comment just afterwards. As an example, let’s say we want to disable the `font-weight-notation` rule. In this case, our enable and disable comments will take the shape: `/* stylelint-disable font-weight-notation */` and `/* stylelint-enable font-weight-notation */`.
- You might want to issue an exit(1) signal to let another process know something wasn’t right! We saw this in a Stylelint setting. When one or more of our rules was broken, we issued exit(1). Then, for example, if we were running Stylelint to check styles automatically before a git commit, the hook could abort when there is an error flagged by exit(1). When the script exits normally, it automatically issues exit(0) so we do not have to do anything in that case. Anyway, to emit the exit(1) just call `Deno.exit(1)` from your Deno script.
Have you found the post useful? Would you prefer to see posts on another topic instead? Get in touch with ideas for new posts. Also, if you like my writing style, get in touch if I can write some posts for your company site on a consultancy basis. Read on to find ways to get in touch, further below. If you want to support posts similar to this one and can spare a few dollars, euros or pounds, please consider supporting me through Buy me a Coffee.
Finally, feel free to share the post on your social media accounts for all your followers who will find it useful. As well as leaving a comment below, you can get in touch via @askRodney on Twitter, @[email protected] on Mastodon and also the #rodney Element Matrix room. Also, see further ways to get in touch with Rodney Lab. I post regularly on Astro as well as Deno. Also, subscribe to the newsletter to keep up-to-date with our latest projects.