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

tailwindcss-in-browser

v0.1.3

Published

Building CSS directly in the browser using Tailwind CSS 4.

Downloads

188

Readme

A JavaScript library that enables you to build CSS directly in the browser using Tailwind CSS 4.

Installation

npm install tailwindcss-in-browser

Usage

import buildCss from "tailwindcss-in-browser";

const markup =
  '<div class="text-2xl text-teal-600 font-semibold">Hello, world!</div>';

// Tailwind CSS 4 configuration via CSS.
const configurationCss = `
  @theme {
    --font-size-2xl: 1.75rem;
    --font-size-2xl--line-height: 2.25rem;
  }
`;

buildCss(markup, configurationCss, {
  compileCssOptions: { addPreflight: false }, // Optional. Defaults to `true`.
  transformCssOptions: { minify: false }, // Optional. Defaults to `true`.
}).then((css) => {
  // `css` contains the generated Tailwind CSS styles.
});

This library is available as an ES module and works with both module bundlers and directly in browsers:

<script type="module">
  import buildCss from "https://unpkg.com/tailwindcss-in-browser";

  // ...
</script>

Note for Vite users

If you use Vite, add the following to your config:

{
  optimizeDeps: {
    exclude: ["tailwindcss-in-browser"],
  },
};

This workaround is needed until vitejs/vite#8427 is fixed.

How it works

  1. A function from Tailwind CSS 3 is used to extract class names from the markup. In Tailwind CSS 4, this is done by the Oxide engine, which is written in Rust, and requires a Node.js runtime.

  2. Compiling the CSS using the extracted class names happens with Tailwind CSS 4, supporting its CSS-first configuration.

  3. Lightning CSS is used to transform the compiled CSS for browser compatibility, matching the implementation of Tailwind CSS 4, but using lightningcss-wasm, so it can run in the browser.

API reference

Main function

buildCss()

The primary function for generating Tailwind CSS styles. It extracts class names from the markup, compiles them using Tailwind CSS 4, and transforms them with Lightning CSS for browser compatibility.

| Parameter | Type | Description | | ----------------------------- | --------------------------------------------- | --------------------------------------------------------------------------------------------------------- | | markup | string | The HTML markup containing Tailwind classes. | | configurationCss | string | CSS configuration for Tailwind CSS 4 (see Tailwind CSS 4 configuration). | | options | object | Optional configuration object. | | options.compileCssOptions | CompileCssOptions | Options for CSS compilation. | | options.transformCssOptions | TransformCssOptions | Options for CSS transformation. |

Returns: Promise<string> - The compiled and transformed CSS.

By default, Tailwind CSS Preflight styles are included, and the output CSS is minified. You can customize these behaviors via options. In case you need more granular control, you can use the utility functions below. Calling them separately in the order below (extractClassNameCandidates()compileCss()transformCss()) is equivalent to calling buildCss().

Tailwind CSS 4 configuration

Tailwind CSS 4 uses a CSS-based configuration format. Normally in this CSS file you would add @import "tailwindcss", which imports the following:

When working with this library, all of the above is taken care of, so all you need to do is add your theme customizations with a @theme directive. E.g.:

@theme {
  /* Colors */
  --color-primary: #3b82f6;
  --color-secondary: #64748b;

  /* Typography */
  --font-size-base: 1rem;
  --font-size-lg: 1.125rem;
  --font-size-xl: 1.25rem;

  /* Spacing */
  --spacing-4: 1rem;
  --spacing-8: 2rem;
}

Utility functions

extractClassNameCandidates()

Extracts Tailwind class names from markup.

| Parameter | Type | Description | | --------- | -------- | ----------------------- | | markup | string | HTML markup to analyze. |

Returns: string[] - Array of extracted class names.

compileCss()

Compiles CSS using Tailwind CSS 4.

| Parameter | Type | Description | | --------------------- | ----------------------------------------- | -------------------------------- | | classNameCandidates | string[] | Array of class names to process. | | configurationCss | string | Tailwind v4 configuration CSS. | | options | CompileCssOptions | Compilation options. |

Returns: Promise<string> - Compiled CSS

transformCss()

Transforms CSS for browser compatibility.

| Parameter | Type | Description | | --------- | --------------------------------------------- | ----------------------- | | css | string | CSS to transform. | | options | TransformCssOptions | Transformation options. |

Returns: Promise<string> - Transformed CSS

Configuration options

CompileCssOptions

| Option | Type | Default | Description | | -------------- | --------- | ------- | ----------------------------------------------------------------------------------------- | | addPreflight | boolean | true | Whether to include Tailwind's Preflight styles. |

TransformCssOptions

| Option | Type | Default | Description | | -------- | --------- | ------- | --------------------------------- | | minify | boolean | true | Whether to minify the output CSS. |

Credits

  • The technical foundation for running Tailwind CSS 4 in the browser was demonstrated by @dtinth in a blog post, which served as the basis for this implementation.
  • This package was created with sponsorship from Acquia through work on Drupal's Experience Builder.