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

css-modules-to-tailwind

v0.3.0

Published

tailwind css convert tool

Downloads

103

Readme

CSS Modules to Tailwind CSS

This is a tool to convert css-modules to tailwind-css

Support list

  • [x] tsconfig.json alias support, like alias/component/index.module.css
  • [x] css file circular reference
  • [x] project level support, just run this command: npx css-modules-to-tailwind src/**/*.tsx
  • [ ] pseudo-classes support

About

Install

Global install:

npm install css-modules-to-tailwind -g

Or use npx:

npx css-modules-to-tailwind src/index.tsx
// npx css-modules-to-tailwind src/**/*.tsx

It will check your git directory is clean, you can use '--force' to skip the check.

How it works

It uses jscodeshift and postcss.

Try it yourself:

  1. First, Create a new jsx/tsx file(index.tsx/jsx):

    import React from 'react';
    import style from './index.module.css';
    
    export const User = () => (
      <div className={style.header}>
        <div className={style.user}>
          <img className={style.avatar} alt="avatar" />
          <span className={style.username}>username</span>
        </div>
        <div className={style.channelName}>name</div>
      </div>
    );
  2. Create a new css modules file:

    .header {
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
     
    .user {
      display: flex;
      align-items: center;
      font-weight: bold;
    }
    
    .avatar {
      width: 0.625rem;
      height: 0.625rem;
    }
    
    .username {
      font-size: 0.75rem;
      line-height: 1rem;
      color: #7DD3FC;
      margin-left: 0.25rem;
    }
  3. Use this tool now:

    npx css-modules-to-tailwind index.tsx
  4. You will get:

    // index.tsx
    import React from 'react';
    
    export const User = () => (
      <div className='items-center flex justify-between w-full'>
        <div className='items-center flex font-bold'>
          <img className='h-2.5 w-2.5' alt="avatar" />
          <span className='text-sky-300 text-xs ml-1'>username</span>
        </div>
        <div className={` `}>name</div>
      </div>
    );

    If the css file content is empty, import specifiers and css files will be removed, unused class will be replaced with ` `, You should search globally for ` `, then delete them.

🙋‍♂️ Flat and single structure design makes this tool work better.

Only css-modules?

Of course not. It can also be used for less/scss modules, but it doesn't work very well, like:

.selector1 {
  selector2();
}

.selector2 {
  font-size: 0.75rem;
  line-height: 1rem;
}

It just becomes:

.selector1 {
  selector2();
}

I think you should use composes.

Inappropriate scenes

Unreasonable nesting

import style form 'index.module.css';

const User = () => (
  <>
    <div className={style.parentA}>
      <div className={style.childrenA}>childrenA</div>
    </div>
    <div className={style.parentB}>
      <div className={style.childrenA}>childrenA</div>
    </div>
  </>
);
.parentA {
  .childrenA {
    // some decl
  }
}

You shouldn't use nesting as namespace.

You should not write multiple/conflicting declarations in a selector

import clsx from 'clsx';
import style form 'index.module.css';

const User = () => (
  <>
    <div className={clsx(style.cls1, style.cls2)}></div>
  </>
);
.cls1 {
  margin-left: 0.5rem;
  display: none;
}

.cls2 {
  margin-left: 0.375rem;
  display: block
}

Always, it will become like this:

const User = () => (
  <>
    <div className={clsx('hidden ml-2', 'block ml-1.5')}></div>
  </>
);

I mean, in tailwind, "ml-2 ml-1.5" === "ml-2", but in your code, is the latter declaration overriding the former.

Support detail

Composes

  1. Quote itself

    .class1 {
      display: flex;
    }
    
    .class2 {
      compose: class1
    }

    it just becomes:

    .class1 {
      @apply flex;
    }
    
    .class2 {
      composes: class1
    }
  2. Other CSS file:

    /** index1.module.css */
    .test1 {
      display: flex;
    }
    
    /** index2.module.css */
    .test2 {
      composes: test1 from './index1.module.css'
    }

    index1.module.css will be removed, and index2.module.css:

    .test2 {
      @apply flex;
    }

Multiple states

For example:

.button {
  width: 1.25rem; /* 20px */
}

.box .button {
  width: 2rem; /* 32px */
}

It just becomes:

.button {
  @apply w-5; /* 20px */
}

.box .button {
  @apply w-8; /* 32px */
}

Classes with multiple states will not do too much processing, because I don't know if there is a conflict between the states.

Permutations

Multiple style declarations can form a Tailwind CSS class. For example:

.truncate {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
const Com = () => <div className={style.truncate}>text</div>

It will become:

const Com = () => <div className='truncate'>text</div>

Of course, it supports more complex permutations and combinations, you can try it.

Do i have to use tailwind-css?

I think it's very useful, you can try it