forked from unocss/unocss
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(attributify-jsx): new transformer
transformer-attributify-jsx
(u…
…nocss#1334) Co-authored-by: Anthony Fu <[email protected]>
- Loading branch information
1 parent
719cdab
commit 3a931b8
Showing
17 changed files
with
559 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
# @unocss/transformer-attributify-jsx | ||
|
||
<!-- @unocss-ignore --> | ||
|
||
Support [valueless attributify](https://github.com/unocss/unocss/tree/main/packages/preset-attributify#valueless-attributify) in JSX/TSX. | ||
|
||
```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 @unocss/transformer-attributify-jsx | ||
``` | ||
|
||
```ts | ||
// uno.config.js | ||
import { defineConfig, presetAttributify } from 'unocss' | ||
import transformerAttributifyJsx from '@unocss/transformer-attributify-jsx' | ||
|
||
export default defineConfig({ | ||
// ... | ||
presets: [ | ||
// ... | ||
presetAttributify() | ||
], | ||
transformers: [ | ||
transformerAttributifyJsx(), // <-- | ||
], | ||
}) | ||
``` | ||
|
||
## Caveats | ||
|
||
> ⚠️ 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 hover:text-2xl /> <!-- cannot contain `:` --> | ||
|
||
<div translate-x-[100px] /> <!-- cannot contain `[` or `]` --> | ||
``` | ||
|
||
Instead, you may want to use valued attributes instead: | ||
|
||
```html | ||
<div translate="x-100%" /> | ||
|
||
<div hover="text-2xl" /> | ||
|
||
<div translate="x-[100px]" /> | ||
``` | ||
|
||
## Blocklist | ||
|
||
This transformer will only transform attrubutes 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 © 2022-PRESENT [Anthony Fu](https://github.com/antfu) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { defineBuildConfig } from 'unbuild' | ||
|
||
export default defineBuildConfig({ | ||
entries: [ | ||
'src/index', | ||
], | ||
clean: true, | ||
declaration: true, | ||
externals: [ | ||
'magic-string', | ||
], | ||
rollup: { | ||
emitCJS: true, | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"name": "@unocss/transformer-attributify-jsx", | ||
"version": "0.0.1", | ||
"description": "Support valueless attributify in JSX/TSX.", | ||
"author": "Anthony Fu <[email protected]>", | ||
"license": "MIT", | ||
"funding": "https://github.com/sponsors/antfu", | ||
"homepage": "https://github.com/unocss/unocss/tree/main/packages/transformer-attributify-jsx#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/unocss/unocss.git", | ||
"directory": "packages/transformer-attributify-jsx" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/unocss/unocss/issues" | ||
}, | ||
"keywords": [ | ||
"unocss", | ||
"unocss-transformer" | ||
], | ||
"sideEffects": false, | ||
"exports": { | ||
".": { | ||
"require": "./dist/index.cjs", | ||
"import": "./dist/index.mjs" | ||
} | ||
}, | ||
"main": "./dist/index.cjs", | ||
"module": "./dist/index.mjs", | ||
"types": "./dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "unbuild", | ||
"stub": "unbuild --stub" | ||
}, | ||
"dependencies": { | ||
"@unocss/core": "workspace:*" | ||
}, | ||
"devDependencies": { | ||
"@rollup/pluginutils": "^4.2.1", | ||
"magic-string": "^0.26.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import type { SourceCodeTransformer } from '@unocss/core' | ||
import { createFilter } from '@rollup/pluginutils' | ||
|
||
export type FilterPattern = ReadonlyArray<string | RegExp> | string | RegExp | null | ||
|
||
export interface TransformerAttributifyJsxOptions { | ||
/** | ||
* the list of attributes to ignore | ||
* @default [] | ||
*/ | ||
blocklist?: (string | RegExp)[] | ||
|
||
/** | ||
* Regex of modules to be included from processing | ||
* @default [/\.[jt]sx$/, /\.mdx$/] | ||
*/ | ||
include?: FilterPattern | ||
|
||
/** | ||
* Regex of modules to exclude from processing | ||
* | ||
* @default [] | ||
*/ | ||
exclude?: FilterPattern | ||
} | ||
|
||
const elementRE = /<!--[\s\S]*?-->|<(\/?)([a-zA-Z][-.:0-9_a-zA-Z]*)((?:\s+[^>]*?(?:(?:'[^']*')|(?:"[^"]*"))?)*)\s*(\/?)>/gs | ||
const attributeRE = /([a-zA-Z()#][\[?a-zA-Z0-9-_:()#%\]?]*)(?:\s*=\s*((?:'[^']*')|(?:"[^"]*")|\S+))?/g | ||
|
||
export default function transformerAttributifyJsx(options: TransformerAttributifyJsxOptions = {}): SourceCodeTransformer { | ||
const { | ||
blocklist = [], | ||
} = options | ||
|
||
const isBlocked = (matchedRule: string) => { | ||
for (const blockedRule of blocklist) { | ||
if (blockedRule instanceof RegExp) { | ||
if (blockedRule.test(matchedRule)) | ||
return true | ||
} | ||
else if (matchedRule === blockedRule) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
const idFilter = createFilter( | ||
options.include || [/\.[jt]sx$/, /\.mdx$/], | ||
options.exclude || [], | ||
) | ||
|
||
return { | ||
name: 'transformer-jsx', | ||
enforce: 'pre', | ||
idFilter, | ||
async transform(code, _, { uno }) { | ||
const tasks: Promise<void>[] = [] | ||
|
||
for (const item of Array.from(code.original.matchAll(elementRE))) { | ||
for (const attr of item[3].matchAll(attributeRE)) { | ||
const matchedRule = attr[0] | ||
if (matchedRule.includes('=') || isBlocked(matchedRule)) | ||
continue | ||
|
||
tasks.push(uno.parseToken(matchedRule).then((matched) => { | ||
if (matched) { | ||
const tag = item[2] | ||
const startIdx = (item.index || 0) + (attr.index || 0) + tag.length + 1 | ||
const endIdx = startIdx + matchedRule.length | ||
code.overwrite(startIdx, endIdx, `${matchedRule}=""`) | ||
} | ||
})) | ||
} | ||
} | ||
|
||
await Promise.all(tasks) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.