npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

stylelint-plugin-prefer-utility

v1.0.0-1

Published

A Stylelint plugin to let you know when you might be better off using a utility class rather than creating a new one.

Downloads

52

Readme

stylelint-plugin-prefer-utility

A Stylelint plugin to let you know when you might be better off using a utility class rather than creating a new one.

License: MIT Travis CI status Appveryor status

The plugin provides a prefer-utility/prefer-utility rule that compares the number of declarations in each ruleset with a configurable threshold. It warns you if that number is equal or below that threshold, so that you can avoid introducing a new rule for a so few properties.

You'll probably want to avoid certain rules, too. Like your utility classes for example... they're already utility classes. For this, the rule can be configured to ignore some rulesets through String/RegExp matching on the selector or a custom Function.

As CSS projects file organisation and naming schemes vary greatly from one to another, the plugin does only the warning bit. If you chose to listen to the warning, it's up to you to:

  • write the utility class, however best suits your project,
  • clean up the class that triggered the warning to keep things tidy ;)

That said, there might be room for some help in the warning message. If you'd find this handy, please join the discussion on this issue

Getting started

The package is available on NPM and can be installed via:

npm install stylelint-plugin-prefer-utility

It provides a shareable config you can extend to quickly add the rule to you current .stylelintrc. With this configuration, it will:

  • warn when there's 1 declaration (as utility classes generally have only one declaration),
  • ignore any selector that's not a single class or id.
{
  "extends": [
    "stylelint-plugin-prefer-utility/config"
  ]
}

You can then override the options for the prefer-utility/prefer-utility rule as best suits your needs:

{
  "extends": [
    "stylelint-plugin-prefer-utility/config"
  ],
  "rules": [
    "prefer-utility/prefer-utility": [
      // If you prefer only creating new classes
      // for more than 2 declarations
      2, {
        // If your utility classes are prefixed with `u-`
        ignoreRules: /^.u-/
      }]
  ]
}

If you need it for creating your own ignoreRules, you can reuse the helper function used by the shareable config for testing if a selector is a single class or ID with:

 targetsSingleClassOrId = require('stylelint-plugin-prefer-utility/lib/targetsSingleClassOrId')

or

 import targetsSingleClassOrId from 'stylelint-plugin-prefer-utility/lib/targetsSingleClassOrId'