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

elm-tailwind-postcss

v0.0.3

Published

Postcss plugin to generate Tailwindcss classes into Elm functions

Downloads

1

Readme

elm-tailwind-postcss

Elm + Tailwindcss + Parcel = :rocket:

See the demo-repo.

Instead of using tailwindcss classes directly as string, which might accidently causing typo, you can use this package to generate tailwindcss classes into Elm module and use it as normal Elm function with the help of Elm Compiler to check for error.

This package is inspired by postcss-elm-tailwind. Some code and idea got from that repo.

Below is what view function might look like using Tailwind function.

view : Model -> Html Msg
view model =
    Html.div 
        [ T.h_screen
        , T.w_screen
        , T.flex
        , T.justify_center
        , T.items_center
        , T.bg_gray_200 
        ]
        [ Html.div 
            []
            [ Html.button
                [ E.onClick Decrement
                , T.px_2
                , T.px_4
                , T.text_white
                , T.bg_blue_500
                , T.w_full
                ]
                [ Html.text "-" 
                ]
                , Html.div
                    [ T.text_2xl
                    , T.text_center
                    , T.my_4
                    ]
                    [ Html.text (String.fromInt model) 
                    ]
                , Html.button
                    [ E.onClick Increment
                    , T.px_2
                    , T.px_4
                    , T.text_white
                    , T.bg_blue_500
                    , T.w_full
                    ]
                    [ Html.text "+" 
                    ]
              ]
          ]

Installation

npm i -D elm-tailwind-postcss

Usage

postcss.config.js

// tailwind to elm config
const etcconfig = {
    elmFile: "src/Tailwind/Tailwind.elm",
    elmModuleName: "Tailwind.Tailwind",
    formats: {
        svg: {
            elmFile: "src/Tailwind/Svg.elm",
            elmModuleName: "Tailwind.Svg"
        },
        string: {
            elmFile: "src/Tailwind/String.elm",
            elmModuleName: "Tailwind.String"
        }
    }
};

// purge
const purgeconfig = {
    content: ['./tmp/**/*.js'],
};

const process = require("process");
const autoprefixer = require("autoprefixer");
const purgecss = require('@fullhuman/postcss-purgecss')(purgeconfig);
const tailwindcss = require("tailwindcss")("tailwind.config.js");
const etc = require("elm-tailwind-postcss")(etcconfig);

const development = {
    plugins: 
        [ tailwindcss
        , etc
        , autoprefixer
        ],
};

// first build will bundle Elm, and Tailwind but doesn't purge CSS yet.
const firstbuild = {
    plugins:
        [ tailwindcss
        , autoprefixer
        ],
};

// production build will use result from first build and produce minified, purged CSS.
const production = {
    plugins: 
        [ tailwindcss
        , purgecss
        , autoprefixer
        ],
};

if (process.env.NODE_ENV === "firstbuild") {
    module.exports = firstbuild;
}
else if (process.env.NODE_ENV === "production") {
    module.exports = production;
} else {
    module.exports = development;
}

tailwind.config.config

Purge feature in Tailwind config has been disabled to hide warning message as we have configured it in postcss config already.

module.exports = {
    purge: false,
    darkMode: false, // or 'media' or 'class'
    theme: {
        extend: {},
    },
    variants: {
        extend: {},
    },
    plugins: [],
}

Other output formats

SVG

If you want to use Tailwind classes to style SVG you can output an Svg module like this:

module.exports = {
  plugins: [
    require("tailwindcss"),
    require("elm-tailwind-postcss")({
      elmFile: "src/Tailwind/Tailwind.elm",
      elmModuleName: "Tailwind.Tailwind",
      formats: {
        svg: {
          elmFile: "src/Tailwind/Svg.elm",
          elmModuleName: "Tailwind.Svg"
        }
      }
    })
  ]
};

String

If you want access to the class names themselves, you can output a String module as an escape hatch:

module.exports = {
  plugins: [
    require("tailwindcss"),
    require("elm-tailwind-postcss")({
      elmFile: "src/Tailwind/Tailwind.elm",
      elmModuleName: "Tailwind.Tailwind",
      formats: {
        string: {
          elmFile: "src/Tailwind/String.elm",
          elmModuleName: "Tailwind.String"
        }
      }
    })
  ]
};

Production Build

We are using Parcel for building the project.

In order to get a small build, you'll neet to build Tailwind twice - once without purgecss to build Tailwind.elm with all the classes and once with purgecss so that all the unused classes are removed from your production CSS. See how this is implemented in the demo

First Build

set NODE_ENV=firstbuild&&npx parcel build --out-dir tmp --public-url ./ index.html

The first build will output the result into tmp directory which contains all classes without purgecss.

Last Build/Production Build

set NODE_ENV=production&&npx parcel build --out-dir dist --public-url ./ ./tmp/index.html

The last build will get result from the first build, purge all unused classes and output into dist directory.

Syntax Changes

As Elm has restriction for function name, some Tailwind classes will be converted into valid Elm function name.

-- starts with number becomes "i_": 2xl -> i_2xl
div [ T.i_2xl ][]

-- starts with minus sign becomes "neg_": -m-2 -> neg_m_2
div [ T.neg_m_2][]

-- hyphene becomes "_": m-2 -> m_2
div [ T.m_2 ][]

-- colon becomes "__": sm:m-2 -> sm__m_2
div [ T.sm__m_2 ][]

-- divider becomes "over": inset-1/2 -> inset_1over2
div [ T.inset_1over2 ][]

-- dot sign becomes "_dot_": -inset-x-2.5 -> neg_inset_x_2_dot_5
div [ T.neg_inset_x_2_dot_5 ][]