# transformer-attributify-jsx-swc

> Use SWC to support valueless attributify in JSX/TSX.

Latest version **0.0.1** (published 2023-11-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install transformer-attributify-jsx-swc
pnpm add transformer-attributify-jsx-swc
yarn add transformer-attributify-jsx-swc
bun add transformer-attributify-jsx-swc
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2023-11-20 |
| First published | 2023-11-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 12 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | zhiyuanzmj |
| Maintainers | zmjs |
| Keywords | unocss, unocss-transformer, ast-grep |

## Links

- npm: https://www.npmjs.com/package/transformer-attributify-jsx-swc
- Repository: https://github.com/zhiyuanzmj/transformer-attributify-jsx-swc
- Homepage: https://github.com/zhiyuanzmj/transformer-attributify-jsx-swc/tree/main#readme
- Issues: https://github.com/zhiyuanzmj/transformer-attributify-jsx-swc/issues
- npm.io page: https://npm.io/package/transformer-attributify-jsx-swc

## Dependencies (3)

- [ast-kit](https://npm.io/package/ast-kit.md) ^0.11.2
- [@swc/core](https://npm.io/package/@swc/core.md) ^1.3.96
- [@unocss/core](https://npm.io/package/@unocss/core.md) ^0.57.4

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.0.1 (latest) — 2023-11-20
- 0.0.0 — 2023-11-20

## README

# transformer-attributify-jsx-swc

<!-- @unocss-ignore -->

Support [valueless attributify](https://github.com/unocss/unocss/tree/main/packages/preset-attributify#valueless-attributify) in JSX/TSX. \
Using [SWC](https://github.com/swc-project/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

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

Will be transformed to:

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

<details>
<summary>Without this transformer</summary>

JSX by default will treat valueless attributes as boolean attributes.

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

</details>

## Install

```bash
npm i -D transformer-attributify-jsx-swc
```

```ts
// 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](https://github.com/unocss/unocss/tree/main/packages/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

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

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

Instead, you may want to use valued attributes instead:

```html
<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.

```js
transformerAttributifyJsx({
  blocklist: [/text-[a-zA-Z]*/, 'text-5xl']
})
```

```jsx
<div text-red text-center text-5xl animate-bounce>
  unocss
</div>
```

Will be compiled to:

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

## License

MIT License &copy; 2022-PRESENT [zhiyuanzmj](https://github.com/zhiyuanzmj)

---
_Source: https://npm.io/package/transformer-attributify-jsx-swc · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
