@unocss/transformer-compile-class 0.33.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +70 -0
- package/dist/index.cjs +53 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.mjs +51 -0
- package/package.json +44 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021-PRESENT Anthony Fu <https://github.com/antfu>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# @unocss/transformer-compile-class
|
|
2
|
+
|
|
3
|
+
<!-- @unocss-ignore -->
|
|
4
|
+
|
|
5
|
+
Compile group of classes into one class. Inspried 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).
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm i -D @unocss/transformer-compile-class
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
// uno.config.js
|
|
15
|
+
import { defineConfig } from 'unocss'
|
|
16
|
+
import transformerCompileClass from '@unocss/transformer-compile-class'
|
|
17
|
+
|
|
18
|
+
export default defineConfig({
|
|
19
|
+
// ...
|
|
20
|
+
transformers: [
|
|
21
|
+
transformerCompileClass(),
|
|
22
|
+
],
|
|
23
|
+
})
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
At the begin of your class strings, **add `:uno:` at the begin of the strings** to mark them for compilation. For example:
|
|
29
|
+
|
|
30
|
+
```html
|
|
31
|
+
<div class=":uno: text-center sm:text-left">
|
|
32
|
+
<div class=":uno: text-sm font-bold hover:text-red"/>
|
|
33
|
+
</div>
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Will be compiled to:
|
|
37
|
+
|
|
38
|
+
```html
|
|
39
|
+
<div class="uno-qlmcrp">
|
|
40
|
+
<div class="uno-0qw2gr"/>
|
|
41
|
+
</div>
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
```css
|
|
45
|
+
.uno-qlmcrp {
|
|
46
|
+
text-align: center;
|
|
47
|
+
}
|
|
48
|
+
.uno-0qw2gr {
|
|
49
|
+
font-size: 0.875rem;
|
|
50
|
+
line-height: 1.25rem;
|
|
51
|
+
font-weight: 700;
|
|
52
|
+
}
|
|
53
|
+
.uno-0qw2gr:hover {
|
|
54
|
+
--un-text-opacity: 1;
|
|
55
|
+
color: rgba(248, 113, 113, var(--un-text-opacity));
|
|
56
|
+
}
|
|
57
|
+
@media (min-width: 640px) {
|
|
58
|
+
.uno-qlmcrp {
|
|
59
|
+
text-align: left;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Options
|
|
65
|
+
|
|
66
|
+
You can config the trigger string and prefix for compile class with the options. Refers to [the types](https://github.com/antfu/unocss/blob/main/packages/transformer-compile-class/src/index.ts#L4) for details.
|
|
67
|
+
|
|
68
|
+
## License
|
|
69
|
+
|
|
70
|
+
MIT License © 2021-PRESENT [Anthony Fu](https://github.com/antfu)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const core = require('@unocss/core');
|
|
4
|
+
|
|
5
|
+
function transformerCompileClass(options = {}) {
|
|
6
|
+
const {
|
|
7
|
+
trigger = ":uno:",
|
|
8
|
+
classPrefix = "uno-",
|
|
9
|
+
hashFn = hash,
|
|
10
|
+
keepUnknown = true
|
|
11
|
+
} = options;
|
|
12
|
+
const regex = new RegExp(`(["'\`])${core.escapeRegExp(trigger)}\\s([^\\1]*?)\\1`, "g");
|
|
13
|
+
return {
|
|
14
|
+
name: "compile-class",
|
|
15
|
+
enforce: "pre",
|
|
16
|
+
async transform(s, _, { uno }) {
|
|
17
|
+
const matches = [...s.original.matchAll(regex)];
|
|
18
|
+
if (!matches.length)
|
|
19
|
+
return;
|
|
20
|
+
for (const match of matches) {
|
|
21
|
+
let body = match[2].trim();
|
|
22
|
+
const start = match.index;
|
|
23
|
+
const replacements = [];
|
|
24
|
+
if (keepUnknown) {
|
|
25
|
+
const result = await Promise.all(body.split(/\s+/).filter(Boolean).map(async (i) => [i, !!await uno.parseToken(i)]));
|
|
26
|
+
const known = result.filter(([, matched]) => matched).map(([i]) => i);
|
|
27
|
+
const unknown = result.filter(([, matched]) => !matched).map(([i]) => i);
|
|
28
|
+
replacements.push(...unknown);
|
|
29
|
+
body = known.join(" ");
|
|
30
|
+
}
|
|
31
|
+
if (body) {
|
|
32
|
+
const hash2 = hashFn(body);
|
|
33
|
+
const className = `${classPrefix}${hash2}`;
|
|
34
|
+
replacements.unshift(className);
|
|
35
|
+
uno.config.shortcuts.push([className, body]);
|
|
36
|
+
}
|
|
37
|
+
s.overwrite(start + 1, start + match[0].length - 1, replacements.join(" "));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function hash(str) {
|
|
43
|
+
let i;
|
|
44
|
+
let l;
|
|
45
|
+
let hval = 2166136261;
|
|
46
|
+
for (i = 0, l = str.length; i < l; i++) {
|
|
47
|
+
hval ^= str.charCodeAt(i);
|
|
48
|
+
hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
|
|
49
|
+
}
|
|
50
|
+
return `00000${(hval >>> 0).toString(36)}`.slice(-6);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
module.exports = transformerCompileClass;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { SourceCodeTransformer } from '@unocss/core';
|
|
2
|
+
|
|
3
|
+
interface CompileClassOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Trigger string
|
|
6
|
+
* @default ':uno:'
|
|
7
|
+
*/
|
|
8
|
+
trigger?: string;
|
|
9
|
+
/**
|
|
10
|
+
* Prefix for compile class name
|
|
11
|
+
* @default 'uno-'
|
|
12
|
+
*/
|
|
13
|
+
classPrefix?: string;
|
|
14
|
+
/**
|
|
15
|
+
* Hash function
|
|
16
|
+
*/
|
|
17
|
+
hashFn?: (str: string) => string;
|
|
18
|
+
/**
|
|
19
|
+
* Left unknown classes inside the string
|
|
20
|
+
*
|
|
21
|
+
* @default true
|
|
22
|
+
*/
|
|
23
|
+
keepUnknown?: boolean;
|
|
24
|
+
}
|
|
25
|
+
declare function transformerCompileClass(options?: CompileClassOptions): SourceCodeTransformer;
|
|
26
|
+
|
|
27
|
+
export { CompileClassOptions, transformerCompileClass as default };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { escapeRegExp } from '@unocss/core';
|
|
2
|
+
|
|
3
|
+
function transformerCompileClass(options = {}) {
|
|
4
|
+
const {
|
|
5
|
+
trigger = ":uno:",
|
|
6
|
+
classPrefix = "uno-",
|
|
7
|
+
hashFn = hash,
|
|
8
|
+
keepUnknown = true
|
|
9
|
+
} = options;
|
|
10
|
+
const regex = new RegExp(`(["'\`])${escapeRegExp(trigger)}\\s([^\\1]*?)\\1`, "g");
|
|
11
|
+
return {
|
|
12
|
+
name: "compile-class",
|
|
13
|
+
enforce: "pre",
|
|
14
|
+
async transform(s, _, { uno }) {
|
|
15
|
+
const matches = [...s.original.matchAll(regex)];
|
|
16
|
+
if (!matches.length)
|
|
17
|
+
return;
|
|
18
|
+
for (const match of matches) {
|
|
19
|
+
let body = match[2].trim();
|
|
20
|
+
const start = match.index;
|
|
21
|
+
const replacements = [];
|
|
22
|
+
if (keepUnknown) {
|
|
23
|
+
const result = await Promise.all(body.split(/\s+/).filter(Boolean).map(async (i) => [i, !!await uno.parseToken(i)]));
|
|
24
|
+
const known = result.filter(([, matched]) => matched).map(([i]) => i);
|
|
25
|
+
const unknown = result.filter(([, matched]) => !matched).map(([i]) => i);
|
|
26
|
+
replacements.push(...unknown);
|
|
27
|
+
body = known.join(" ");
|
|
28
|
+
}
|
|
29
|
+
if (body) {
|
|
30
|
+
const hash2 = hashFn(body);
|
|
31
|
+
const className = `${classPrefix}${hash2}`;
|
|
32
|
+
replacements.unshift(className);
|
|
33
|
+
uno.config.shortcuts.push([className, body]);
|
|
34
|
+
}
|
|
35
|
+
s.overwrite(start + 1, start + match[0].length - 1, replacements.join(" "));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function hash(str) {
|
|
41
|
+
let i;
|
|
42
|
+
let l;
|
|
43
|
+
let hval = 2166136261;
|
|
44
|
+
for (i = 0, l = str.length; i < l; i++) {
|
|
45
|
+
hval ^= str.charCodeAt(i);
|
|
46
|
+
hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
|
|
47
|
+
}
|
|
48
|
+
return `00000${(hval >>> 0).toString(36)}`.slice(-6);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export { transformerCompileClass as default };
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@unocss/transformer-compile-class",
|
|
3
|
+
"version": "0.33.2",
|
|
4
|
+
"description": "Compile group of classes into one class",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"unocss",
|
|
7
|
+
"unocss-transformer"
|
|
8
|
+
],
|
|
9
|
+
"homepage": "https://github.com/unocss/unocss/tree/main/packages/transformer-compile-class#readme",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/unocss/unocss/issues"
|
|
12
|
+
},
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/unocss/unocss.git",
|
|
18
|
+
"directory": "packages/transformer-compile-class"
|
|
19
|
+
},
|
|
20
|
+
"funding": "https://github.com/sponsors/antfu",
|
|
21
|
+
"main": "./dist/index.cjs",
|
|
22
|
+
"module": "./dist/index.mjs",
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"require": "./dist/index.cjs",
|
|
27
|
+
"import": "./dist/index.mjs"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist"
|
|
32
|
+
],
|
|
33
|
+
"sideEffects": false,
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@unocss/core": "0.33.2"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"magic-string": "^0.26.1"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "unbuild",
|
|
42
|
+
"stub": "unbuild --stub"
|
|
43
|
+
}
|
|
44
|
+
}
|