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

@mhsdesign/jit-browser-tailwindcss

v0.4.2

Published

Jit Browser Tailwindcss

Downloads

6,062

Readme

@mhsdesign/jit-browser-tailwindcss

Still in Development

Client side api to generate css via tailwind jit in the browser - Dynamic tailwindcss!

image

Tailwinds CDN: https://cdn.tailwindcss.com

(Keep in mind its not only pure tailwind but also dom observing etc) with tailwind Version 3.1.8

  • 372kB
  • 98.9kB brotli
  • 99.4kB gzip

This library: https://unpkg.com/@mhsdesign/jit-browser-tailwindcss

(pure Tailwind API) with tailwind Version 3.1.8

  • 246kB
  • 74.3kB gzip

Implementation

Current implementation

This library is inspired/extracted from the library monaco-tailwindcss see core code link

As you might see in the code core code example we uses tailwind internals to create a postcss plugin via createTailwindcssPlugin. This is not api but works for now ;) And produces highly optimized bundle sizes.

Previous / Other implementations

Previously i implemented this based on various other projects by mocking fs.readFileSync and having some kind of virtual file store.

This turned out to be not so efficient in terms of bundle size ;)

Also mocking fs.readFileSync had to be done in some postcss source files and this required the whole postcss package to be prebundled. If the developer wants to use post css too it would result postcss being in the bundle twice.

See packages which are implemented like this:

The advantage here being that it uses the official API and doesnt rely much on internals.

Supported Features of Tailwind Css

everything ;) - plugins, custom Tailwind config, custom Tailwind base css, variants ...

Usage:

npm

npm install @mhsdesign/jit-browser-tailwindcss

cdn

https://unpkg.com/@mhsdesign/jit-browser-tailwindcss

api to generate styles from content

/**
 * The entry point to retrieve 'tailwindcss'
 *
 * @param options {@link TailwindcssOptions}
 * @example
 * const tailwindConfig: TailwindConfig = {
 *   theme: {
 *     extend: {
 *       colors: {
 *         marcherry: 'red',
 *       },
 *     },
 *   },
 * };
 * const tailwindCss = tailwindcssFactory({ tailwindConfig });
 */
export function createTailwindcss(
  options?: TailwindcssOptions,
): Tailwindcss;

export interface TailwindcssOptions {
  /**
   * The tailwind configuration to use.
   */
  tailwindConfig?: TailwindConfig;
}

export interface Tailwindcss {
  /**
   * Update the current Tailwind configuration.
   *
   * @param tailwindConfig The new Tailwind configuration.
   */
  setTailwindConfig: (tailwindConfig: TailwindConfig) => void;

  /**
   * Generate styles using Tailwindcss.
   *
   * This generates CSS using the Tailwind JIT compiler. It uses the Tailwind configuration that has
   * previously been passed to {@link createTailwindcss}.
   *
   * @param css The CSS to process. Only one CSS file can be processed at a time.
   * @param content All content that contains CSS classes to extract.
   * @returns The CSS generated by the Tailwind JIT compiler. It has been optimized for the given
   * content.
   * @example
   * tailwindcss.generateStylesFromContent(
   *   css,
   *   [myHtmlCode]
   * )
   */
  generateStylesFromContent: (css: string, content: (Content | string)[]) => Promise<string>;
}

/**
 * Contains the content of CSS classes to extract.
 * With optional "extension" key, which might be relevant
 * to properly extract css classed based on the content language.
 */
export interface Content {
  content: string;
  extension?: string;
}

lower level api to create tailwind post css plugin

/**
 * Lower level API to create a PostCSS Tailwindcss Plugin
 * @internal might change in the future
 * @example
 * const processor = postcss([createTailwindcssPlugin({ tailwindConfig, content })]);
 * const { css } = await processor.process(css, { from: undefined });
 */
export function createTailwindcssPlugin(
  options: TailwindCssPluginOptions
): AcceptedPlugin;

export interface TailwindCssPluginOptions {
  /**
   * The tailwind configuration to use.
   */
  tailwindConfig?: TailwindConfig;
  /**
   * All content that contains CSS classes to extract.
   */
   content: (Content | string)[];
}

Examples:

see examples/*

  • esbuild-demo
  • esbuild-demo-worker
  • script-tag

Use cases

this plugin was developed to make dynamic html content elements from a CMS usable with tailwind classes. In that case one should already have a tailwind build and css file at hand - any further css can then be generated via this package. To have the least amount of css duplication, one should disable the normalize css and also use it without the @base include:

const tailwind = createTailwindcss({
    tailwindConfig: {
        // disable normalize css
        corePlugins: { preflight: false }
    }
})

const css = await tailwind.generateStylesFromContent(`
    /* without the "@tailwind base;" */
    @tailwind components;
    @tailwind utilities;
`, [htmlContent])

Development

build the package

npm run build

test each example (will be served with esbuild)

npm run example1
npm run example2
npm run example3