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

postcss-transform-tailwindcss

v1.1.4

Published

PostCSS plugin transform tailwincss

Downloads

9

Readme

PostCSS Transform Tailwindcss [postcss]

Static Badge Static Badge Static Badge

[PostCSS Transform Tailwindcss] can convert class names from less, sass, and less files to classMame in tsx and class in html

Quick start

[PostCSS Transform Tailwindcss] is a [PostCSS] plugin. If you want to transition from semantic class names to Tailwindcss, this is a good tool.

Usage

Add [PostCSS Transform Tailwindcss] to your project:

npm install postcss-transform-tailwindcss --save-dev

Use [PostCSS Transform Tailwindcss] as a [PostCSS] plugin:

Transform Css

npm install sass --save-dev
npm install postcss-nested --save-dev
const sass = require("sass");
const postcss = require("postcss");
const postcssNested = require("postcss-nested");
const postcssTransformTailwindcss = require("postcss-transform-tailwindcss");
const from = "project/xxx/xxx.css";
const { css } = sass.compile();
postcss([
  postcssNested(),
  postcssTransformTailwindcss(/* pluginOptions */)
]).process(css, {
  from,
  to: undefined
});

Transform Scss

npm install sass --save-dev
const sass = require("sass");
const postcss = require("postcss");
const postcssTransformTailwindcss = require("postcss-transform-tailwindcss");
const from = "project/xxx/xxx.scss";
const { css } = sass.compile();
postcss([postcssTransformTailwindcss(/* pluginOptions */)]).process(css, {
  from,
  to: undefined
});

Transform less

npm install sass --save-dev
npm install less --save-dev
const sass = require("sass");
const postcss = require("postcss");
const postcssTransformTailwindcss = require("postcss-transform-tailwindcss");
const from = "project/xxx/xxx.less";
const css = fs.readFileSync(from, "utf-8");
// https://lesscss.org/usage/
less.render(css, async (error, output) => {
  if (error) {
    console.warn("error", error);
    return;
  }
  const { css } = sass.compileString(output.css);
  const result = await postcss([postcssTransformTailwindcss(opts)]).process(
    css,
    {
      from,
      to: undefined
    }
  );
});

Transform files

npm install sass --save-dev
npm install less --save-dev
npm install globs --save-dev
const sass = require("sass");
const postcss = require("postcss");
const globs = require("globs");
const postcssTransformTailwindcss = require("postcss-transform-tailwindcss");

async function transformSass(from, opts = {}) {
  const { css } = sass.compile(from);
  const result = await postcss([postcssTransformTailwindcss(opts)]).process(
    css,
    {
      from,
      to: undefined
    }
  );
}
globs("xxx/xxx/**/*.scss", function(err, files) {
  if (err) {
    throw err;
  }
  files.forEach(path => {
    transformSass(path, {
      ratio: 2,
      fileExtensions: ["tsx"],
      removeEmptyClassName: true,
      getClassNames(classNames, merge) {
        classNames["align-items"] = merge(classNames["align-items"], {
          "align-items: top;": "items-top",
          "align-items: bottom;": "items-bottom"
        });
        return classNames;
      }
    });
  });
});

Options

ratio

The ratio option is similar to the ratio of the parameters in the Postcss-pxtransform plugin. For example, if the size is twice the size of the design draft, then ratio: 2.

postcssTransformTailwindcss({ ratio: 0 });

fileExtensions

The FileExtensions option is a collection of files that CSS needs to convert, which can be of file types such as '.jsx ','.tsx', or '.html '

postcssTransformTailwindcss({ fileExtensions: ["jsx"] });
postcssTransformTailwindcss({ fileExtensions: ["jsx", "tsx"] });
postcssTransformTailwindcss({ fileExtensions: ["html", "tsx", "jsx"] });

removeEmptyClassName

The removeEmptyClassName option is very useful during debugging to determine whether to remove the class names of the corresponding. jsx, tsx,. html files that convert CSS

postcssTransformTailwindcss({ removeEmptyClassName: true });

getClassNames

The getClassNames option is a function that takes two parameters. The first parameter is a collection of tailwincss classNames, and the second parameter is [deepmerge]( https://www.npmjs.com/package/deepmerge )Method, you need to return the merged classNames

postcssTransformTailwindcss({
  getClassNames(classNames, merge) {
    classNames["align-items"] = merge(classNames["align-items"], {
      "align-items: top;": "items-top",
      "align-items: bottom;": "items-bottom"
    });
    return classNames;
  }
});

Notes

  1. When converting HTML files, tag elements must be closed and cannot have comments. At the same time, the label 'DOCTYPE html' needs to be temporarily removed
  2. When dealing with. tsx or. jsx.html cross selectors, only a maximum of two will be processed, and nested descendants of cross selectors cannot be processed.
  3. Due to the higher priority of the media query selector in tailwindcss compared to regular atomic selectors, the effect is different.
  4. When dealing with situations where animations and tailwindcss do not match, you can add custom configurations in tailwind.diag.js and merge custom configurations in the getClassNames plugin
  5. Remember to check if the conversion is correct after completion