|
1 | | -# transformer-attributify-jsx-sg |
| 1 | +# vue-macros-sg |
2 | 2 |
|
3 | | -<!-- @unocss-ignore --> |
| 3 | +vue-macros-sg is a cli for rewriting at vue macros powered by ast-grep. |
4 | 4 |
|
5 | | -Support [valueless attributify](https://github.com/unocss/unocss/tree/main/packages/preset-attributify#valueless-attributify) in JSX/TSX. \ |
6 | | -Using [ast-grep](https://github.com/ast-grep/ast-grep) to match attributes, Thanks for [HerringtonDarkholme](https://github.com/HerringtonDarkholme). |
| 5 | +## Support vue macros |
7 | 6 |
|
8 | | -### Benchmark |
9 | | -`pnpm run bench` |
10 | | -``` |
11 | | -Running "unocss concurrent transform jsx" suite... |
12 | | -benchRegexTransform: |
13 | | - 16 ops/s, ±9.95% | fastest |
14 | | -
|
15 | | -benchAstGrepTransform: |
16 | | - 13 ops/s, ±0.34% | 18.75% slower |
17 | | -
|
18 | | -benchSwcTransform: |
19 | | - 6 ops/s, ±0.84% | 62.5% slower |
20 | | -
|
21 | | -benchBabelTransform: |
22 | | - 2 ops/s, ±1.44% | slowest, 87.5% slower |
23 | | -``` |
| 7 | +- [x] jsx-directive |
| 8 | +- [x] define-render |
| 9 | +- [x] export-render |
| 10 | +- [x] define-slots |
| 11 | +- [x] short-v-model |
| 12 | +- [x] setup-sfc |
24 | 13 |
|
25 | 14 | ## Usage |
26 | 15 |
|
27 | | -```jsx |
28 | | -export function Component() { |
29 | | - return ( |
30 | | - <div text-red text-center text-5xl animate-bounce> |
31 | | - unocss |
32 | | - </div> |
33 | | - ) |
34 | | -} |
35 | | -``` |
36 | | - |
37 | | -Will be transformed to: |
38 | | - |
39 | | -```jsx |
40 | | -export function Component() { |
41 | | - return ( |
42 | | - <div text-red="" text-center="" text-5xl="" animate-bounce=""> |
43 | | - unocss |
44 | | - </div> |
45 | | - ) |
46 | | -} |
47 | | -``` |
48 | | - |
49 | | -<details> |
50 | | -<summary>Without this transformer</summary> |
51 | | - |
52 | | -JSX by default will treat valueless attributes as boolean attributes. |
53 | | - |
54 | | -```jsx |
55 | | -export function Component() { |
56 | | - return ( |
57 | | - <div text-red={true} text-center={true} text-5xl={true} animate-bounce={true}> |
58 | | - unocss |
59 | | - </div> |
60 | | - ) |
61 | | -} |
62 | | -``` |
63 | | - |
64 | | -</details> |
65 | | - |
66 | | -## Install |
67 | | - |
68 | | -```bash |
69 | | -npm i -D transformer-attributify-jsx-sg |
70 | | -``` |
71 | | - |
72 | | -```ts |
73 | | -// uno.config.ts |
74 | | -import { defineConfig, presetAttributify } from 'unocss' |
75 | | -import transformerAttributifyJsx from 'transformer-attributify-jsx-sg' |
76 | | - |
77 | | -export default defineConfig({ |
78 | | - // ... |
79 | | - presets: [ |
80 | | - // ... |
81 | | - presetAttributify() |
82 | | - ], |
83 | | - transformers: [ |
84 | | - transformerAttributifyJsx(), // <-- |
85 | | - ], |
86 | | -}) |
87 | | -``` |
88 | | - |
89 | | -## Caveats |
90 | | - |
91 | | -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. |
92 | | - |
93 | | -> ⚠️ The rules are almost the same as those of `preset-attributify`, but there are several precautions |
94 | | -
|
95 | | -```html |
96 | | -<div translate-x-100% /> <!-- cannot end with `%` --> |
97 | | - |
98 | | -<div translate-x-[100px] /> <!-- cannot contain `[` or `]` --> |
99 | | -``` |
100 | | - |
101 | | -Instead, you may want to use valued attributes instead: |
102 | | - |
103 | | -```html |
104 | | -<div translate="x-100%" /> |
105 | | - |
106 | | -<div translate="x-[100px]" /> |
107 | | -``` |
108 | | - |
109 | | -## Blocklist |
110 | | - |
111 | | -This transformer will only transform attributes that are valid UnoCSS utilities. |
112 | | -You can also `blocklist` bypass some attributes from been transformed. |
113 | | - |
114 | | -```js |
115 | | -transformerAttributifyJsx({ |
116 | | - blocklist: [/text-[a-zA-Z]*/, 'text-5xl'] |
117 | | -}) |
118 | | -``` |
119 | | - |
120 | | -```jsx |
121 | | -<div text-red text-center text-5xl animate-bounce> |
122 | | - unocss |
123 | | -</div> |
124 | | -``` |
125 | | - |
126 | | -Will be compiled to: |
127 | | - |
128 | | -```html |
129 | | -<div text-red text-center text-5xl animate-bounce=""> |
130 | | - unocss |
131 | | -</div> |
| 16 | +```shell |
| 17 | +npx vue-macros-sg ./src |
132 | 18 | ``` |
133 | 19 |
|
134 | 20 | ## License |
|
0 commit comments