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

cnat

v0.0.7

Published

Systematically apply certain modifications to classes, class names, used in your frontend codebase.

Downloads

4

Readme

crates.io npm version

CNAT

Class Name Alteration Tool. Systematically change all the class names in your codebase.

Install

cargo install cnat
npm install -g cnat

Why?

Because you joined a project that has some awful old tailwind configs: weird bespoke spacing configuration, weird sizes, some current tailwind default colors (e.g. slate) not available because the config was for an older version of tailwind, etc...

So now you can't just copy/paste tailwind class names from the internet or use ones that come from a ui component library. Say you want to use shadcn/ui; Nope!

You want to do something about this, but it's way too risky and time consuming. You would want to do this incrementally, to protect your sanity.

The best solution would be to deprecate the old configs while keeping them around and working; so you slap a prefix legacy- in the old tailwind.config.js.

Deprecation Steps

In the root of your project. Run:

echo '@tailwind base; @tailwind components; @tailwind utilities;' > temp.css
npx tailwindcss -i temp.css -o legacy-tw.css
cnat prefix -i legacy-tw.css --prefix 'legacy-' ./src

By default, cnat prefix will crawl through all the class=*, className=* in jsx elements and className:* in a React.createElement calls, inside of ts|js|tsx|jsx files. It will match any class in the source code with classes found in legacy-tw.css (which contains every style that tailwind generates based on your config).

Rename the file for the old configs, tailwind.legacy.config.ts.

mv tailwind.config.ts tailwind.legacy.config.ts # or *.js if your tailwind config files aren't in typescript

Add the prefix in the legacy config file.

export default {
  prefix: "legacy-",
};

Run the tailwind cli again to rebuild the legacy css classes. Now they will be prefixed since the tailwind.legacy.config.ts defined the 'legacy-'

npx tailwindcss -i temp.css -o legacy-tw.css -c tailwind.legacy.config.ts

Then, import the legacy css into your global css file.

@import "./legacy-tw.css";

@tailwind base;
@tailwind components;
@tailwind utilities;

/* And whatever else you have in here */

Now you can re-init new configs.

# You'd have to delete the current `tailwind.config.ts` if you haven't already.
npx tailwindcss init --ts

And now you can breathe. A fresh new start; ignoring what's under the bed now.

Usage

Systematically apply certain modifications to classes, class names, used in your frontend codebase.

Usage: cnat <COMMAND>

Commands:
  prefix      Apply a prefix to all the tailwind classes in every js file in a project
  completion  Generate completions for a specified shell
  help        Print this message or the help of the given subcommand(s)

Options:
  -h, --help     Print help
  -V, --version  Print version
cnat prefix --help
Apply a prefix to all the tailwind classes in every js file in a project

Usage: cnat prefix [OPTIONS] -i <CSS_FILE> --prefix <PREFIX> <CONTEXT>

Arguments:
  <CONTEXT>  The root directory of the js/ts project

Options:
  -i <CSS_FILE>             The output css file generated by calling `npx tailwindcss -i input.css -o output.css`
  -p, --prefix <PREFIX>     The prefix to apply to all the tailwind class names found
  -s, --scopes <SCOPES>...  Define scope within which prefixing happens. Example: --scopes 'att:className,*ClassName prop:classes fn:cva' [default: "att:class,className fn:createElement"]
  -h, --help                Print help

Scopes

You may have tailwind classes in other places besides className="...", or even cva(...). For examples, the classes prop in mui components.

You can define places for cnat to look for classes with --scopes or -s option. The syntax for a scope is :<...values>

Variants are:

  • fn to target a function call (e.g 'fn:cva')
  • att to target a jsx attribute (e.g. 'att:className')
  • prop to target a jsx attribute (e.g. 'prop:className')

Values are strings, and you can use a wildcard * at the begining or the end. For example 'att:className att:*ClassName' will find classes all of these attributes

<Btn
  className="w-10 bg-red"
  iconClassName="text-black"
  textClassName="text-xl"
/>

By default cnat use --scopes 'att:class,className fn:createElement'

cnat prefix -i legacy-tw.css --prefix 'legacy-' ./src --scopes 'att:class,className fn:createElement'