Giter Site home page Giter Site logo

green-labs / res_tailwindcss Goto Github PK

View Code? Open in Web Editor NEW
27.0 8.0 4.0 151 KB

ReScript PPX which validates the tailwindcss class names

License: MIT License

Shell 3.41% OCaml 81.85% Standard ML 1.70% JavaScript 12.28% Batchfile 0.75%
rescript ppx tailwindcss

res_tailwindcss's Introduction

ReScript Tailwindcss

A ReScript PPX, which validates the tailwindcss class names

Motivation

The tailwind-ppx is the only ppx to validate the tailwindcss class names in compile time. But, it was archived, and written by ocaml-migrate-parsetree. My team considered taking over the repository and maintaining it but decided to rewrite it from the scratch with ppxlib and menhir. Additionally, we improve the logic to find the invalid class name with Spelling Corrector algorithm.

Plus, the arbitrary values in the JIT mode of Tailwindcss are supported!

<!-- arbitrary value examples -->
<div className=%twc("p-[75px]")> ... </div>
<div className=%twc("p-[calc(75px)]")> ... </div>
<div className=%twc("p-[calc(100%-40px)]")> ... </div>
<div className=%twc("bg-[#1da1f1]")> ... </div>
<div className=%twc("grid-cols-[1fr,700px,2fr]")> ... </div>
<div className=%twc("translate-x-[calc(-50%+27px)]")> ... </div>
<div className=%twc("!pb-[270px]")> ... </div>
<div className=%twc("after:content-['Hello_World']")> ... </div>
<div className=%twc("peer-checked:[&>svg]:rotate-180")> ... </div>

Install

yarn add -D @greenlabs/res-tailwindcss

<path_to_tailwindcss> should be replaced with the relative location of your generated tailwindcss file from your project root in which the bsconfig.json file is located.

// bsconfig.json

"ppx-flags": [
  ...,
  ["@greenlabs/res-tailwindcss/ppx", "--path <path_to_tailwindcss>"]
],

Example

<input type_="checkbox" className=%twc("peer") />
<div className=%twc("flex justify-center items-center after:content-['Hello_World'] peer-checked:[&>svg]:rotate-180")>
  ...
  <svg />
</div>

Development

  1. Create a sandbox with opam
opam switch create tailwindcss 4.12.1
  1. Install dependencies
opam install . --deps-only --with-test
  1. Build
opam exec -- dune build
  1. Test
cd rescript

(install dependencies)
yarn

(build --watch)
yarn res:clean && yarn res:watch

(run test --watch)
yarn test:watch

res_tailwindcss's People

Contributors

gyeop avatar jeong-sik avatar kit-ty-kate avatar minuukang avatar mununki avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

res_tailwindcss's Issues

Using command line arg instead of config file

Having a command line arg to get a tailwind CSS file path instead of making the redundant config file.

// bsconfig.json
"ppx-flags": [
  ...,
  ["@greenlabs/ppx-tailwindcss/ppx", "--path <path_to_tailwindcss>"]
]

improve the parser

  • more complicated than I thought regarding pseudo-classes, pseudo-elements, attribute-selectors
  • also needs to distinguish to the tailwindcss arbitrary values against attribute-selectors
  • currently, manipulating the string after parsed to get rid of pseudo-*, attribute-selectors ๐Ÿ™…

What to do

  • parse the pseudo-*, attribute-selectors, arbitrary values instead of manipulating string after parsed.

Issue with Underscore Arbitrary Modifier in Tailwind CSS

I'm encountering an issue when trying to use the underscore arbitrary modifier to style the descending a of a div. Below is the code I'm using:

<div
  className={cx([
     %twc(
          "tw-overflow-hidden tw-transition-[padding-top] tw-ease-out tw-duration-[400ms] [&_a]:tw-mt-4"
      ),
      switch state {
      | true => %twc("tw-p-20")
      | false => %twc("tw-max-h-0")
      },
   ])}

However, it results in the following error:

 104 โ”† className={cx([
  105 โ”†   %twc(
  106 โ”†     "tw-overflow-hidden tw-transition-[padding-top] tw-ease-out tw-dur
      โ”† ation-[400ms] [&_a]:tw-mt-4"
  107 โ”†   ),

  Class name not found: [&_a]:tw-mt-4

FAILED: cannot make progress due to previous errors.
>>>> Finish compiling(exit: 1)

Steps to Reproduce:

  1. Add the above code to a res component. (remove the prefix tw- if not configured)
  2. Ensure Tailwind CSS watcher is running to build the new classes.
  3. Observe the error message during compilation.

Additional Information:

  • Tailwind CSS reference: Tailwind CSS Documentation
  • Other selectors like > are working as expected.
  • Manually modifying the .bs.js output file with the underscore modifier works as expected.
  • It appears the issue might be with the ppx not recognizing this modifier.

Any assistance or guidance on how to resolve this issue would be greatly appreciated. Thank you!

issue when continuous space input between class names.

ex

%twc("fixed  w-full")

If there are two spaces between the class names, an error occurs that the class cannot be found.
It's an issue caused by a typo, but if you look at the error message, the user will try to find the problem in the wrong build of the tailwind.

fix to find the project root correctly in OCaml project

Currently, find_project_root func is working to find the project root with the location of bsconfig.json. It doesn't make sense in the OCaml project. It should be fixed to find *.opam, dune-project, or dune-workspace instead.

Read stanza reference for usage in OCaml project

Regarding the usage in the OCaml project. It needs to know where the tailwind CSS file is. (In the ReScript project, the path of the tailwind CSS file is passed in command line args.)

In the OCaml project, the best way is getting the path from dune file as inline_tests ppx does such as below.

(library
 (res_tailwindcss
  (deps
   "src/styles/tailwind.css"))
 (name example)
 (public_name example)
 (libraries core str)
 (preprocess
  (pps res_tailwindcss)))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.