transformer-attributify-jsx-swc
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

transformer-attributify-jsx-swc

Support valueless attributify in JSX/TSX.
Using SWC to match attributes.

Benchmark

pnpm run bench

benchRegexTransform:
    898 ops/s, ±0.24%   | fastest

benchAstGrepTransform:
  370 ops/s, ±0.25%   | 58.8% slower

benchSwcTransform:
  232 ops/s, ±1.01%   | 74.16% slower

benchBabelTransform:
  72 ops/s, ±3.85%    | slowest, 91.98% slower

Usage

export function Component() {
  return (
    <div text-red text-center text-5xl animate-bounce>
      unocss
    </div>
  )
}

Will be transformed to:

export function Component() {
  return (
    <div text-red="" text-center="" text-5xl="" animate-bounce="">
      unocss
    </div>
  )
}
Without this transformer

JSX by default will treat valueless attributes as boolean attributes.

export function Component() {
  return (
    <div text-red={true} text-center={true} text-5xl={true} animate-bounce={true}>
      unocss
    </div>
  )
}

Install

npm i -D transformer-attributify-jsx-swc
// uno.config.ts
import { defineConfig, presetAttributify } from 'unocss'
import transformerAttributifyJsx from 'transformer-attributify-jsx-swc'

export default defineConfig({
  // ...
  presets: [
    // ...
    presetAttributify()
  ],
  transformers: [
    transformerAttributifyJsx(), // <--
  ],
})

Caveats

If you encounter any issues with this package, there is @unocss/transformer-attributify-jsx-babel package that uses Babel to transform JSX.

⚠️ The rules are almost the same as those of preset-attributify, but there are several precautions

<div translate-x-100% /> <!-- cannot end with `%` -->

<div translate-x-[100px] /> <!-- cannot contain `[` or `]` -->

Instead, you may want to use valued attributes instead:

<div translate="x-100%" />

<div translate="x-[100px]" />

Blocklist

This transformer will only transform attributes that are valid UnoCSS utilities. You can also blocklist bypass some attributes from been transformed.

transformerAttributifyJsx({
  blocklist: [/text-[a-zA-Z]*/, 'text-5xl']
})
<div text-red text-center text-5xl animate-bounce>
  unocss
</div>

Will be compiled to:

<div text-red text-center text-5xl animate-bounce="">
  unocss
</div>

License

MIT License © 2022-PRESENT zhiyuanzmj

Package Sidebar

Install

npm i transformer-attributify-jsx-swc

Weekly Downloads

0

Version

0.0.1

License

MIT

Unpacked Size

12.3 kB

Total Files

8

Last publish

Collaborators

  • zmjs