New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

postcss-bs-tailwind

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

postcss-bs-tailwind

A PostCSS plugin for generating Tailwind CSS types for BuckleScript/ReasonML

  • 0.2.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-83.33%
Maintainers
1
Weekly downloads
 
Created
Source

postcss-bs-tailwind

A PostCSS plugin for generating ReasonML variables that correspond to Tailwind CSS classnames.

What does this plugin do?

This plugin generates a Tailwind.re module for you that contains a binding for each and every Tailwind class in your CSS. So, instead of relying on strings in your code (which can be prone to typos), you get the safety (and the auto-complete) of a variable! And, thanks to BuckleScript's [@bs.inline] directive, the bindings have zero runtime cost! They complile to plain strings!

Installation and Configuration

This codegen tool works best when you're using re-classnames, which is a ReasonML implementation of the popular classnames library for composing classes into a string you can pass to the className prop on a DOM node. The examples bellow assume you've followed re-classname's installation instructions.

npm install postcss-bs-tailwind re-classnames

# or

yarn add postcss-bs-tailwind re-classnames

Then, update your PostCSS config to look something like this:

const tailwindcss = require("tailwindcss");
const reason = require("postcss-bs-tailwind");

module.exports = {
  plugins: [
    tailwindcss("./tailwind.config.js"),
    require("autoprefixer"),
    reason({ modulePath: "./bindings/Tailwind.re" })
  ]
};

Important: Make sure this plugin appears after your tailwindcss plugin, preferably last in the list.

The modulePath config value is required, and denotes the name/location where you'd like the generated module. Now, when you run PostCSS, you'll end up with a Reason module that looks like this:

// ...
[@bs.inline] let bg_cyan_050 = "bg-cyan-050";
[@bs.inline] let bg_cyan_100 = "bg-cyan-100";
[@bs.inline] let bg_cyan_200 = "bg-cyan-200";
[@bs.inline] let bg_cyan_300 = "bg-cyan-300";
// ...

So now, in your ReasonReact code, you can do:

open Tailwind;

[@react.component]
let make = () => {
  <button className=Cn.make([bg_cyan_300])> "Cyan Button"->React.string </button>
};

Name Mapping

Tailwind's default naming scheme isn't compatible with Reason's syntax, so each name gets changed a little:

Tailwind ClassReason VariableNaming Rule
bg-blue-100bg_blue_100- gets changed to _
focus:bg-blue-100focus__bg_blue_050: gets changed to __
-m-1neg_m_1- as a prefix gets changed to neg_
w-1/2w_one_half1/2 gets changed to one_half (other fractions get replaced with their text representation)

Don't worry - the Reason varaible is only a placeholder for the actual string value! When your Reason code is compliled to JS, the original Tailwind string will be right where you expect it!

Custom Tailwind Stuff

No worries! This plugin works by parsing the CSS after Tailwind CSS is generated. It walks through each class definition in your CSS code and transforms the class' name into something that's Reason-friendly. So, any special classes you've got (from Tailwind plugins or custom config) should get picked up just fine. That said, there could be some naming edge cases that aren't handled as of yet. Feel free to open an issue if you bump into one!

FAQs

Package last updated on 28 Apr 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc