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

prettier-plugin-typewind

v0.0.8

Published

Convert tailwind classes to typewind objects

Downloads

5

Readme

Plugin for converting string classes to object classes

Attention! This is a test solution, not ready for production.

But works fine, but does not exclude bugs

Introduction

This plugin is designed to simplify development using typewind, which makes it possible to type component classes.

In the development process, you inevitably encounter the need for uniformity in the code, uniformity in approaches. But transferring strings into objects by hand is time consuming.

A plugin has been written to help with this.

For example:

So exists code with include class-variance-authority module code like this

const button = cva("button", {
  variants: {
    intent: {
      primary: [
        "bg-blue-500",
        "text-white",
        "border-transparent",
        "hover:bg-blue-600",
      ],
      secondary: [
        "bg-white",
        "text-gray-800",
        "border-gray-400",
        "hover:bg-gray-100",
      ],
    },
    size: {
      small: ["text-sm", "py-1", "px-2"],
      medium: ["text-base", "py-2", "px-4"],
    },
  },
  compoundVariants: [{ intent: "primary", size: "medium", class: "uppercase" }],
  defaultVariants: {
    intent: "primary",
    size: "medium",
  },
});

Will be converted to this:

const button = cva(tw.button, {
  variants: {
    intent: {
      primary: [
        tw.bg_blue_500,
        tw.text_white,
        tw.border_transparent,
        tw.hover(tw.bg_blue_600),
      ],
      secondary: [
        tw.bg_white,
        tw.text_gray_800,
        tw.border_gray_400,
        tw.hover(tw.bg_gray_100),
      ],
    },
    size: {
      small: [tw.text_sm, tw.py_1, tw.px_2],
      medium: [tw.text_base, tw.py_2, tw.px_4],
    },
  },
  compoundVariants: [{ intent: "primary", size: "medium", class: "uppercase" }],
  defaultVariants: {
    intent: "primary",
    size: "medium",
  },
});

Exists code writen in JSX style

return (
    <div class={"flex space-x-2 justify-end"}>
      <Switch>
        <Match when={!requested()}>
          <button
            class={"btn btn-error btn-outline btn-sm normal-case"}
            onClick={(event) => {
              event.preventDefault();
              setRequested(true);
            }}
          >
            {buttonTitle}
          </button>
        </Match>
        <Match when={requested()}>
          <button
            class={"btn btn-sm btn-outline hover:btn-ghost md:hover:btn-warning normal-case"}
            onClick={(event) => {
              event.preventDefault();
              setRequested(false);
              onAcceptClick && onAcceptClick();
            }}
          >
            yes
          </button>
          <button
            class={"btn btn-sm btn-outline normal-case"}
            onClick={(event) => {
              event.preventDefault();
              setRequested(false);
              onDeclineClick && onDeclineClick();
            }}
          >
            no
          </button>
        </Match>
      </Switch>
    </div>
  );

will be converted to:

return (
    <div class={tw.flex.justify_end.space_x_2}>
      <Switch>
        <Match when={!requested()}>
          <button
            class={tw.btn.btn_error.btn_outline.btn_sm.normal_case}
            onClick={(event) => {
              event.preventDefault();
              setRequested(true);
            }}
          >
            {buttonTitle}
          </button>
        </Match>
        <Match when={requested()}>
          <button
            class={tw.btn.btn_outline.btn_sm.normal_case
              .hover(tw.btn_ghost)
              .md(tw.hover(tw.btn_warning))}
            onClick={(event) => {
              event.preventDefault();
              setRequested(false);
              onAcceptClick && onAcceptClick();
            }}
          >
            yes
          </button>
          <button
            class={tw.btn.btn_outline.btn_sm.normal_case}
            onClick={(event) => {
              event.preventDefault();
              setRequested(false);
              onDeclineClick && onDeclineClick();
            }}
          >
            no
          </button>
        </Match>
      </Switch>
    </div>
  );

Installation

To get started, just install prettier-plugin-tailwindcss as a dev-dependency:

npm i -D prettier prettier-plugin-typewind
or
yarn add -D prettier prettier-plugin-typewind

This plugin follows Prettier’s autoloading convention, so as long as you’ve got Prettier set up in your project, it’ll start working automatically as soon as it’s installed.

Note that plugin autoloading is not supported when using certain package managers, such as pnpm or Yarn PnP. In this case you may need to add the plugin to your Prettier config explicitly:

// prettier.config.js
module.exports = {
  plugins: [require('prettier-plugin-typewind')],
}

Resolving your Tailwind configuration

To ensure that the class sorting is taking into consideration any of your project's Tailwind customizations, it needs access to your Tailwind configuration file (tailwind.config.js).

By default the plugin will look for this file in the same directory as your Prettier configuration file. However, if your Tailwind configuration is somewhere else, you can specify this using the tailwindConfig option in your Prettier configuration.

Note that paths are resolved relative to the Prettier configuration file.

// prettier.config.js
module.exports = {
  tailwindConfig: './styles/tailwind.config.js',
}

Use

You can use it for example like this:

  • in package.json
{
  "scripts":{
    "pret": "prettier --write ./src"
  }
}

What can be converted

  • [x] JSX classes from [*.jsx,*.tsx] - class, className, classList
  • [x] Class Variance Authority from [*.ts,*.tsx] - base class, intents, sizes
  • [x] Variants, Importants, Arbitrary groups
  • [x] Support sorting classes like in prettier-plugin-tailwindcss
  • [ ] Support for not only Tailwind classes, also take custom classes and put it in raw()
  • [ ] Modules like Classnames or clsx
  • [ ] TWIN.macro