@mhsdesign/jit-browser-tailwindcss

0.4.1 • Public • Published

@mhsdesign/jit-browser-tailwindcss

Still in Development

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

image

(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

(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 ;)

To be continued ...

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

Readme

Keywords

Package Sidebar

Install

npm i @mhsdesign/jit-browser-tailwindcss

Weekly Downloads

1,258

Version

0.4.1

License

MIT

Unpacked Size

1.23 MB

Total Files

6

Last publish

Collaborators

  • marchenry