@unocss/transformer-attributify-jsx 0.45.1
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 +118 -0
- package/dist/index.cjs +61 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.mjs +59 -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,118 @@
|
|
|
1
|
+
# @unocss/transformer-attributify-jsx
|
|
2
|
+
|
|
3
|
+
<!-- @unocss-ignore -->
|
|
4
|
+
|
|
5
|
+
Support [valueless attributify](https://github.com/unocss/unocss/tree/main/packages/preset-attributify#valueless-attributify) in JSX/TSX.
|
|
6
|
+
|
|
7
|
+
```jsx
|
|
8
|
+
export function Component() {
|
|
9
|
+
return (
|
|
10
|
+
<div text-red text-center text-5xl animate-bounce>
|
|
11
|
+
unocss
|
|
12
|
+
</div>
|
|
13
|
+
)
|
|
14
|
+
}
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Will be transformed to:
|
|
18
|
+
|
|
19
|
+
```jsx
|
|
20
|
+
export function Component() {
|
|
21
|
+
return (
|
|
22
|
+
<div text-red="" text-center="" text-5xl="" animate-bounce="">
|
|
23
|
+
unocss
|
|
24
|
+
</div>
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
<details>
|
|
30
|
+
<summary>Without this transformer</summary>
|
|
31
|
+
|
|
32
|
+
JSX by default will treat valueless attributes as boolean attributes.
|
|
33
|
+
|
|
34
|
+
```jsx
|
|
35
|
+
export function Component() {
|
|
36
|
+
return (
|
|
37
|
+
<div text-red={true} text-center={true} text-5xl={true} animate-bounce={true}>
|
|
38
|
+
unocss
|
|
39
|
+
</div>
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
</details>
|
|
45
|
+
|
|
46
|
+
## Install
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
npm i -D @unocss/transformer-attributify-jsx
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
// uno.config.js
|
|
54
|
+
import { defineConfig, presetAttributify } from 'unocss'
|
|
55
|
+
import transformerAttributifyJsx from '@unocss/transformer-attributify-jsx'
|
|
56
|
+
|
|
57
|
+
export default defineConfig({
|
|
58
|
+
// ...
|
|
59
|
+
presets: [
|
|
60
|
+
// ...
|
|
61
|
+
presetAttributify()
|
|
62
|
+
],
|
|
63
|
+
transformers: [
|
|
64
|
+
transformerAttributifyJsx(), // <--
|
|
65
|
+
],
|
|
66
|
+
})
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Caveats
|
|
70
|
+
|
|
71
|
+
> ⚠️ The rules are almost the same as those of `preset-attributify`, but there are several precautions
|
|
72
|
+
|
|
73
|
+
```html
|
|
74
|
+
<div translate-x-100% /> <!-- cannot end with `%` -->
|
|
75
|
+
|
|
76
|
+
<div hover:text-2xl /> <!-- cannot contain `:` -->
|
|
77
|
+
|
|
78
|
+
<div translate-x-[100px] /> <!-- cannot contain `[` or `]` -->
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Instead, you may want to use valued attributes instead:
|
|
82
|
+
|
|
83
|
+
```html
|
|
84
|
+
<div translate="x-100%" />
|
|
85
|
+
|
|
86
|
+
<div hover="text-2xl" />
|
|
87
|
+
|
|
88
|
+
<div translate="x-[100px]" />
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Blocklist
|
|
92
|
+
|
|
93
|
+
This transformer will only transform attrubutes that are valid UnoCSS utilities.
|
|
94
|
+
You can also `blocklist` bypass some attributes from been transformed.
|
|
95
|
+
|
|
96
|
+
```js
|
|
97
|
+
transformerAttributifyJsx({
|
|
98
|
+
blocklist: [/text-[a-zA-Z]*/, 'text-5xl']
|
|
99
|
+
})
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
```jsx
|
|
103
|
+
<div text-red text-center text-5xl animate-bounce>
|
|
104
|
+
unocss
|
|
105
|
+
</div>
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Will be compiled to:
|
|
109
|
+
|
|
110
|
+
```html
|
|
111
|
+
<div text-red text-center text-5xl animate-bounce="">
|
|
112
|
+
unocss
|
|
113
|
+
</div>
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## License
|
|
117
|
+
|
|
118
|
+
MIT License © 2022-PRESENT [Anthony Fu](https://github.com/antfu)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const core = require('@unocss/core');
|
|
4
|
+
|
|
5
|
+
function createFilter(include, exclude) {
|
|
6
|
+
const includePattern = core.toArray(include || []);
|
|
7
|
+
const excludePattern = core.toArray(exclude || []);
|
|
8
|
+
return (id) => {
|
|
9
|
+
if (excludePattern.some((p) => id.match(p)))
|
|
10
|
+
return false;
|
|
11
|
+
return includePattern.some((p) => id.match(p));
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
const elementRE = /<!--[\s\S]*?-->|<(\/?)([a-zA-Z][-.:0-9_a-zA-Z]*)((?:\s+[^>]*?(?:(?:'[^']*')|(?:"[^"]*"))?)*)\s*(\/?)>/gs;
|
|
15
|
+
const attributeRE = /([a-zA-Z()#][\[?a-zA-Z0-9-_:()#%\]?]*)(?:\s*=\s*((?:'[^']*')|(?:"[^"]*")|\S+))?/g;
|
|
16
|
+
function transformerAttributifyJsx(options = {}) {
|
|
17
|
+
const {
|
|
18
|
+
blocklist = []
|
|
19
|
+
} = options;
|
|
20
|
+
const isBlocked = (matchedRule) => {
|
|
21
|
+
for (const blockedRule of blocklist) {
|
|
22
|
+
if (blockedRule instanceof RegExp) {
|
|
23
|
+
if (blockedRule.test(matchedRule))
|
|
24
|
+
return true;
|
|
25
|
+
} else if (matchedRule === blockedRule) {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return false;
|
|
30
|
+
};
|
|
31
|
+
const idFilter = createFilter(
|
|
32
|
+
options.include || [/\.[jt]sx$/, /\.mdx$/],
|
|
33
|
+
options.exclude || []
|
|
34
|
+
);
|
|
35
|
+
return {
|
|
36
|
+
name: "transformer-jsx",
|
|
37
|
+
enforce: "pre",
|
|
38
|
+
idFilter,
|
|
39
|
+
async transform(code, _, { uno }) {
|
|
40
|
+
const tasks = [];
|
|
41
|
+
for (const item of Array.from(code.original.matchAll(elementRE))) {
|
|
42
|
+
for (const attr of item[3].matchAll(attributeRE)) {
|
|
43
|
+
const matchedRule = attr[0];
|
|
44
|
+
if (matchedRule.includes("=") || isBlocked(matchedRule))
|
|
45
|
+
continue;
|
|
46
|
+
tasks.push(uno.parseToken(matchedRule).then((matched) => {
|
|
47
|
+
if (matched) {
|
|
48
|
+
const tag = item[2];
|
|
49
|
+
const startIdx = (item.index || 0) + (attr.index || 0) + tag.length + 1;
|
|
50
|
+
const endIdx = startIdx + matchedRule.length;
|
|
51
|
+
code.overwrite(startIdx, endIdx, `${matchedRule}=""`);
|
|
52
|
+
}
|
|
53
|
+
}));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
await Promise.all(tasks);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
module.exports = transformerAttributifyJsx;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { SourceCodeTransformer } from '@unocss/core';
|
|
2
|
+
|
|
3
|
+
declare type FilterPattern = ReadonlyArray<string | RegExp> | string | RegExp | null;
|
|
4
|
+
interface TransformerAttributifyJsxOptions {
|
|
5
|
+
/**
|
|
6
|
+
* the list of attributes to ignore
|
|
7
|
+
* @default []
|
|
8
|
+
*/
|
|
9
|
+
blocklist?: (string | RegExp)[];
|
|
10
|
+
/**
|
|
11
|
+
* Regex of modules to be included from processing
|
|
12
|
+
* @default [/\.[jt]sx$/, /\.mdx$/]
|
|
13
|
+
*/
|
|
14
|
+
include?: FilterPattern;
|
|
15
|
+
/**
|
|
16
|
+
* Regex of modules to exclude from processing
|
|
17
|
+
*
|
|
18
|
+
* @default []
|
|
19
|
+
*/
|
|
20
|
+
exclude?: FilterPattern;
|
|
21
|
+
}
|
|
22
|
+
declare function transformerAttributifyJsx(options?: TransformerAttributifyJsxOptions): SourceCodeTransformer;
|
|
23
|
+
|
|
24
|
+
export { FilterPattern, TransformerAttributifyJsxOptions, transformerAttributifyJsx as default };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { toArray } from '@unocss/core';
|
|
2
|
+
|
|
3
|
+
function createFilter(include, exclude) {
|
|
4
|
+
const includePattern = toArray(include || []);
|
|
5
|
+
const excludePattern = toArray(exclude || []);
|
|
6
|
+
return (id) => {
|
|
7
|
+
if (excludePattern.some((p) => id.match(p)))
|
|
8
|
+
return false;
|
|
9
|
+
return includePattern.some((p) => id.match(p));
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
const elementRE = /<!--[\s\S]*?-->|<(\/?)([a-zA-Z][-.:0-9_a-zA-Z]*)((?:\s+[^>]*?(?:(?:'[^']*')|(?:"[^"]*"))?)*)\s*(\/?)>/gs;
|
|
13
|
+
const attributeRE = /([a-zA-Z()#][\[?a-zA-Z0-9-_:()#%\]?]*)(?:\s*=\s*((?:'[^']*')|(?:"[^"]*")|\S+))?/g;
|
|
14
|
+
function transformerAttributifyJsx(options = {}) {
|
|
15
|
+
const {
|
|
16
|
+
blocklist = []
|
|
17
|
+
} = options;
|
|
18
|
+
const isBlocked = (matchedRule) => {
|
|
19
|
+
for (const blockedRule of blocklist) {
|
|
20
|
+
if (blockedRule instanceof RegExp) {
|
|
21
|
+
if (blockedRule.test(matchedRule))
|
|
22
|
+
return true;
|
|
23
|
+
} else if (matchedRule === blockedRule) {
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return false;
|
|
28
|
+
};
|
|
29
|
+
const idFilter = createFilter(
|
|
30
|
+
options.include || [/\.[jt]sx$/, /\.mdx$/],
|
|
31
|
+
options.exclude || []
|
|
32
|
+
);
|
|
33
|
+
return {
|
|
34
|
+
name: "transformer-jsx",
|
|
35
|
+
enforce: "pre",
|
|
36
|
+
idFilter,
|
|
37
|
+
async transform(code, _, { uno }) {
|
|
38
|
+
const tasks = [];
|
|
39
|
+
for (const item of Array.from(code.original.matchAll(elementRE))) {
|
|
40
|
+
for (const attr of item[3].matchAll(attributeRE)) {
|
|
41
|
+
const matchedRule = attr[0];
|
|
42
|
+
if (matchedRule.includes("=") || isBlocked(matchedRule))
|
|
43
|
+
continue;
|
|
44
|
+
tasks.push(uno.parseToken(matchedRule).then((matched) => {
|
|
45
|
+
if (matched) {
|
|
46
|
+
const tag = item[2];
|
|
47
|
+
const startIdx = (item.index || 0) + (attr.index || 0) + tag.length + 1;
|
|
48
|
+
const endIdx = startIdx + matchedRule.length;
|
|
49
|
+
code.overwrite(startIdx, endIdx, `${matchedRule}=""`);
|
|
50
|
+
}
|
|
51
|
+
}));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
await Promise.all(tasks);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export { transformerAttributifyJsx as default };
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@unocss/transformer-attributify-jsx",
|
|
3
|
+
"version": "0.45.1",
|
|
4
|
+
"description": "Support valueless attributify in JSX/TSX.",
|
|
5
|
+
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"funding": "https://github.com/sponsors/antfu",
|
|
8
|
+
"homepage": "https://github.com/unocss/unocss/tree/main/packages/transformer-attributify-jsx#readme",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/unocss/unocss.git",
|
|
12
|
+
"directory": "packages/transformer-attributify-jsx"
|
|
13
|
+
},
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/unocss/unocss/issues"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"unocss",
|
|
19
|
+
"unocss-transformer"
|
|
20
|
+
],
|
|
21
|
+
"sideEffects": false,
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"require": "./dist/index.cjs",
|
|
25
|
+
"import": "./dist/index.mjs"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"main": "./dist/index.cjs",
|
|
29
|
+
"module": "./dist/index.mjs",
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"files": [
|
|
32
|
+
"dist"
|
|
33
|
+
],
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@unocss/core": "0.45.1"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"magic-string": "^0.26.2"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "unbuild",
|
|
42
|
+
"stub": "unbuild --stub"
|
|
43
|
+
}
|
|
44
|
+
}
|