feature: vor und nachteile

This commit is contained in:
Hikyu
2026-05-31 13:54:31 +02:00
parent 552a947a1c
commit e3dad17446
23270 changed files with 3024794 additions and 200 deletions
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2021-PRESENT Anthony Fu <https://github.com/antfu>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@@ -0,0 +1,116 @@
# @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
import transformerAttributifyJsx from '@unocss/transformer-attributify-jsx'
// uno.config.ts
import { defineConfig, presetAttributify } from 'unocss'
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 [Anthony Fu](https://github.com/antfu)
@@ -0,0 +1,24 @@
import { SourceCodeTransformer } from '@unocss/core';
type FilterPattern = Array<string | RegExp> | string | RegExp | null;
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;
}
declare function transformerAttributifyJsx(options?: TransformerAttributifyJsxOptions): SourceCodeTransformer;
export { type FilterPattern, type TransformerAttributifyJsxOptions, transformerAttributifyJsx as default };
@@ -0,0 +1,24 @@
import { SourceCodeTransformer } from '@unocss/core';
type FilterPattern = Array<string | RegExp> | string | RegExp | null;
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;
}
declare function transformerAttributifyJsx(options?: TransformerAttributifyJsxOptions): SourceCodeTransformer;
export { type FilterPattern, type TransformerAttributifyJsxOptions, transformerAttributifyJsx as default };
@@ -0,0 +1,82 @@
import { toArray } from '@unocss/core';
function createFilter(include, exclude) {
const includePattern = toArray(include);
const excludePattern = toArray(exclude);
return (id) => {
if (excludePattern.some((p) => id.match(p)))
return false;
return includePattern.some((p) => id.match(p));
};
}
const elementRE = /<([^/?<>0-9$_!][^\s>]*)\s+((?:"[^"]*"|'[^"]*'|(\{[^}]*\})|[^{>])+)>/g;
const attributeRE = /(?<![~`!$%^&*()_+\-=[{;':"|,.<>/?])([a-z()#][[?\w\-:()#%\]]*)(?:\s*=\s*('[^']*'|"[^"]*"|\S+))?|\{[^}]*\}/gi;
const valuedAttributeRE = /((?!\d|-{2}|-\d)[\w\u00A0-\uFFFF:!%.~<-]+)=(?:"[^"]*"|'[^']*'|(\{)((?:[`(][^`)]*[`)]|[^}])+)(\}))/g;
function transformerAttributifyJsx(options = {}) {
const {
blocklist = []
} = options;
const isBlocked = (matchedRule) => {
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: "@unocss/transformer-attributify-jsx",
enforce: "pre",
idFilter,
async transform(code, _, { uno }) {
const tasks = [];
const attributify = uno.config.presets.find((i) => i.name === "@unocss/preset-attributify");
const attributifyPrefix = attributify?.options?.prefix ?? "un-";
for (const item of Array.from(code.original.matchAll(elementRE))) {
let attributifyPart = item[2];
if (valuedAttributeRE.test(attributifyPart)) {
attributifyPart = attributifyPart.replace(valuedAttributeRE, (match, _2, dynamicFlagStart) => {
if (!dynamicFlagStart)
return " ".repeat(match.length);
let preLastModifierIndex = 0;
let temp = match;
for (const _item of match.matchAll(elementRE)) {
const attrAttributePart = _item[2];
if (valuedAttributeRE.test(attrAttributePart))
attrAttributePart.replace(valuedAttributeRE, (m) => " ".repeat(m.length));
const pre = temp.slice(0, preLastModifierIndex) + " ".repeat(_item.index + _item[0].indexOf(_item[2]) - preLastModifierIndex) + attrAttributePart;
temp = pre + " ".repeat(_item.input.length - pre.length);
preLastModifierIndex = pre.length;
}
if (preLastModifierIndex !== 0)
return temp;
return " ".repeat(match.length);
});
}
for (const attr of attributifyPart.matchAll(attributeRE)) {
const matchedRule = attr[0].replace(/:/, "-");
if (matchedRule.includes("=") || isBlocked(matchedRule))
continue;
const updatedMatchedRule = matchedRule.startsWith(attributifyPrefix) ? matchedRule.slice(attributifyPrefix.length) : matchedRule;
tasks.push(uno.parseToken(updatedMatchedRule).then((matched) => {
if (matched) {
const startIdx = (item.index || 0) + (attr.index || 0) + item[0].indexOf(item[2]);
const endIdx = startIdx + matchedRule.length;
code.overwrite(startIdx, endIdx, `${matchedRule}=""`);
}
}));
}
}
await Promise.all(tasks);
}
};
}
export { transformerAttributifyJsx as default };
@@ -0,0 +1,46 @@
{
"name": "@unocss/transformer-attributify-jsx",
"type": "module",
"version": "0.65.4",
"description": "Support valueless attributify in JSX/TSX.",
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
"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": "https://github.com/unocss/unocss",
"directory": "packages/transformer-attributify-jsx"
},
"bugs": {
"url": "https://github.com/unocss/unocss/issues"
},
"keywords": [
"unocss",
"unocss-transformer"
],
"sideEffects": false,
"exports": {
".": {
"types": "./dist/index.d.mts",
"default": "./dist/index.mjs"
}
},
"main": "./dist/index.mjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"dependencies": {
"@unocss/core": "0.65.4"
},
"devDependencies": {
"magic-string": "^0.30.17"
},
"scripts": {
"build": "unbuild",
"stub": "unbuild --stub",
"test:attw": "attw --pack --config-path ../../.attw-esm-only.json"
}
}