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.
+70
View File
@@ -0,0 +1,70 @@
# @unocss/transformer-compile-class
<!-- @unocss-ignore -->
Compile group of classes into one class. Inspired by [WindiCSS's compilation mode](https://windicss.org/posts/modes.html#compilation-mode) and [#948](https://github.com/unocss/unocss/issues/948) by [@UltraCakeBakery](https://github.com/UltraCakeBakery).
## Install
```bash
npm i -D @unocss/transformer-compile-class
```
```ts
import transformerCompileClass from '@unocss/transformer-compile-class'
// uno.config.ts
import { defineConfig } from 'unocss'
export default defineConfig({
// ...
transformers: [
transformerCompileClass(),
],
})
```
## Usage
At the begin of your class strings, **add `:uno:` at the begin of the strings** to mark them for compilation. For example:
```html
<div class=":uno: text-center sm:text-left">
<div class=":uno: text-sm font-bold hover:text-red"/>
</div>
```
Will be compiled to:
```html
<div class="uno-qlmcrp">
<div class="uno-0qw2gr"/>
</div>
```
```css
.uno-qlmcrp {
text-align: center;
}
.uno-0qw2gr {
font-size: 0.875rem;
line-height: 1.25rem;
font-weight: 700;
}
.uno-0qw2gr:hover {
--un-text-opacity: 1;
color: rgb(248 113 113 / var(--un-text-opacity));
}
@media (min-width: 640px) {
.uno-qlmcrp {
text-align: left;
}
}
```
## Options
You can config the trigger string and prefix for compile class with the options. Refers to [the types](https://github.com/unocss/unocss/blob/main/packages/transformer-compile-class/src/index.ts#L4) for details.
## License
MIT License &copy; 2021-PRESENT [Anthony Fu](https://github.com/antfu)
@@ -0,0 +1,62 @@
import { SourceCodeTransformer } from '@unocss/core';
interface CompileClassOptions {
/**
* Trigger regex literal. The default trigger regex literal matches `:uno:`,
* for example: `<div class=":uno: font-bold text-white">`.
*
* @example
* The trigger additionally allows defining a capture group named `name`, which
* allows custom class names. One possible regex would be:
*
* ```
* export default defineConfig({
* transformers: [
* transformerCompileClass({
* trigger: /(["'`]):uno(?:-)?(?<name>[^\s\1]+)?:\s([^\1]*?)\1/g
* }),
* ],
* })
* ```
*
* This regular expression matches `:uno-MYNAME:` and uses `MYNAME` in
* combination with the class prefix as the final class name, for example:
* `.uno-MYNAME`. It should be noted that the regex literal needs to include
* the global flag `/g`.
*
* @note
* This parameter is backwards compatible. It accepts string only trigger
* words, like `:uno:` or a regex literal.
*
* @default `/(["'`]):uno(?:-)?(?<name>[^\s\1]+)?:\s([^\1]*?)\1/g`
*/
trigger?: string | RegExp;
/**
* Prefix for compile class name
* @default 'uno-'
*/
classPrefix?: string;
/**
* Hash function
*/
hashFn?: (str: string) => string;
/**
* Allow add hash to class name even if the class name is explicitly defined
*
* @default false
*/
alwaysHash?: boolean;
/**
* Left unknown classes inside the string
*
* @default true
*/
keepUnknown?: boolean;
/**
* The layer name of generated rules
*/
layer?: string;
}
declare function transformerCompileClass(options?: CompileClassOptions): SourceCodeTransformer;
export { type CompileClassOptions, transformerCompileClass as default };
@@ -0,0 +1,62 @@
import { SourceCodeTransformer } from '@unocss/core';
interface CompileClassOptions {
/**
* Trigger regex literal. The default trigger regex literal matches `:uno:`,
* for example: `<div class=":uno: font-bold text-white">`.
*
* @example
* The trigger additionally allows defining a capture group named `name`, which
* allows custom class names. One possible regex would be:
*
* ```
* export default defineConfig({
* transformers: [
* transformerCompileClass({
* trigger: /(["'`]):uno(?:-)?(?<name>[^\s\1]+)?:\s([^\1]*?)\1/g
* }),
* ],
* })
* ```
*
* This regular expression matches `:uno-MYNAME:` and uses `MYNAME` in
* combination with the class prefix as the final class name, for example:
* `.uno-MYNAME`. It should be noted that the regex literal needs to include
* the global flag `/g`.
*
* @note
* This parameter is backwards compatible. It accepts string only trigger
* words, like `:uno:` or a regex literal.
*
* @default `/(["'`]):uno(?:-)?(?<name>[^\s\1]+)?:\s([^\1]*?)\1/g`
*/
trigger?: string | RegExp;
/**
* Prefix for compile class name
* @default 'uno-'
*/
classPrefix?: string;
/**
* Hash function
*/
hashFn?: (str: string) => string;
/**
* Allow add hash to class name even if the class name is explicitly defined
*
* @default false
*/
alwaysHash?: boolean;
/**
* Left unknown classes inside the string
*
* @default true
*/
keepUnknown?: boolean;
/**
* The layer name of generated rules
*/
layer?: string;
}
declare function transformerCompileClass(options?: CompileClassOptions): SourceCodeTransformer;
export { type CompileClassOptions, transformerCompileClass as default };
@@ -0,0 +1,77 @@
import { escapeRegExp, expandVariantGroup } from '@unocss/core';
function transformerCompileClass(options = {}) {
const {
trigger = /(["'`]):uno-?(?<name>\S+)?:\s(.*?)\1/g,
classPrefix = "uno-",
hashFn = hash,
keepUnknown = true,
alwaysHash = false
} = options;
const compiledClass = /* @__PURE__ */ new Set();
const regexp = typeof trigger === "string" ? new RegExp(`(["'\`])${escapeRegExp(trigger)}\\s([^\\1]*?)\\1`, "g") : trigger;
return {
name: "@unocss/transformer-compile-class",
enforce: "pre",
async transform(s, _, { uno, tokens, invalidate }) {
const matches = [...s.original.matchAll(regexp)];
if (!matches.length)
return;
const size = compiledClass.size;
for (const match of matches) {
let body = match.length === 4 && match.groups ? expandVariantGroup(match[3].trim()) : expandVariantGroup(match[2].trim());
const start = match.index;
const replacements = [];
if (keepUnknown) {
const result = await Promise.all(body.split(/\s+/).filter(Boolean).map(async (i) => [i, !!await uno.parseToken(i)]));
const known = result.filter(([, matched]) => matched).map(([i]) => i);
const unknown = result.filter(([, matched]) => !matched).map(([i]) => i);
replacements.push(...unknown);
body = known.join(" ");
}
if (body) {
body = body.split(/\s+/).sort().join(" ");
let hash2;
let explicitName = false;
if (match.groups && match.groups.name) {
hash2 = match.groups.name;
if (alwaysHash)
hash2 += `-${hashFn(body)}`;
explicitName = true;
} else {
hash2 = hashFn(body);
}
const className = `${classPrefix}${hash2}`;
if (tokens && tokens.has(className) && explicitName) {
const existing = uno.config.shortcuts.find((i) => i[0] === className);
if (existing && existing[1] !== body)
throw new Error(`Duplicated compile class name "${className}". One is "${body}" and the other is "${existing[1]}". Please choose different class name or set 'alwaysHash' to 'true'.`);
}
compiledClass.add(className);
replacements.unshift(className);
if (options.layer)
uno.config.shortcuts.push([className, body, { layer: options.layer }]);
else
uno.config.shortcuts.push([className, body]);
if (tokens)
tokens.add(className);
}
s.overwrite(start + 1, start + match[0].length - 1, replacements.join(" "));
}
if (compiledClass.size > size)
invalidate();
}
};
}
function hash(str) {
let i;
let l;
let hval = 2166136261;
for (i = 0, l = str.length; i < l; i++) {
hval ^= str.charCodeAt(i);
hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
}
return `00000${(hval >>> 0).toString(36)}`.slice(-6);
}
export { transformerCompileClass as default };
@@ -0,0 +1,46 @@
{
"name": "@unocss/transformer-compile-class",
"type": "module",
"version": "0.65.4",
"description": "Compile group of classes into one class",
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
"license": "MIT",
"funding": "https://github.com/sponsors/antfu",
"homepage": "https://github.com/unocss/unocss/tree/main/packages/transformer-compile-class#readme",
"repository": {
"type": "git",
"url": "https://github.com/unocss/unocss",
"directory": "packages/transformer-compile-class"
},
"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"
}
}