@unocss/transformer-attributify-jsx 66.3.3 → 66.4.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/README.md CHANGED
@@ -68,8 +68,6 @@ export default defineConfig({
68
68
 
69
69
  ## Caveats
70
70
 
71
- If you encounter any issues with this package, there is [@unocss/transformer-attributify-jsx-babel](https://github.com/unocss/unocss/tree/main/packages-presets/transformer-attributify-jsx-babel) package that uses Babel to transform JSX.
72
-
73
71
  > ⚠️ The rules are almost the same as those of `preset-attributify`, but there are several precautions
74
72
 
75
73
  ```html
package/dist/index.d.mts CHANGED
@@ -21,4 +21,5 @@ interface TransformerAttributifyJsxOptions {
21
21
  }
22
22
  declare function transformerAttributifyJsx(options?: TransformerAttributifyJsxOptions): SourceCodeTransformer;
23
23
 
24
- export { type FilterPattern, type TransformerAttributifyJsxOptions, transformerAttributifyJsx as default };
24
+ export { transformerAttributifyJsx as default };
25
+ export type { FilterPattern, TransformerAttributifyJsxOptions };
package/dist/index.d.ts CHANGED
@@ -21,4 +21,5 @@ interface TransformerAttributifyJsxOptions {
21
21
  }
22
22
  declare function transformerAttributifyJsx(options?: TransformerAttributifyJsxOptions): SourceCodeTransformer;
23
23
 
24
- export { type FilterPattern, type TransformerAttributifyJsxOptions, transformerAttributifyJsx as default };
24
+ export { transformerAttributifyJsx as default };
25
+ export type { FilterPattern, TransformerAttributifyJsxOptions };
package/dist/index.mjs CHANGED
@@ -1,3 +1,6 @@
1
+ import process from 'node:process';
2
+ import { parse } from '@babel/parser';
3
+ import traverse from '@babel/traverse';
1
4
  import { toArray } from '@unocss/core';
2
5
 
3
6
  function createFilter(include, exclude) {
@@ -9,9 +12,6 @@ function createFilter(include, exclude) {
9
12
  return includePattern.some((p) => id.match(p));
10
13
  };
11
14
  }
12
- const elementRE = /<([^/?<>0-9$_!][^\s>]*)\s+((?:"[^"]*"|'[^"]*'|(\{[^}]*\})|[^{>])+)>/g;
13
- const attributeRE = /(?<![~`!$%^&*()_+\-=[{;':"|,.<>/?])([a-z()#][[?\w\-:()#%\]]*)(?:\s*=\s*('[^']*'|"[^"]*"|\S+))?|\{[^}]*\}/gi;
14
- const valuedAttributeRE = /((?!\d|-{2}|-\d)[\w\u00A0-\uFFFF:!%.~<-]+)=(?:"[^"]*"|'[^']*'|(\{)((?:[`(][^`)]*[`)]|[^}])+)(\}))/g;
15
15
  function transformerAttributifyJsx(options = {}) {
16
16
  const {
17
17
  blocklist = []
@@ -36,44 +36,29 @@ function transformerAttributifyJsx(options = {}) {
36
36
  enforce: "pre",
37
37
  idFilter,
38
38
  async transform(code, _, { uno }) {
39
+ if (process.env.VSCODE_CWD)
40
+ return;
39
41
  const tasks = [];
40
- const attributify = uno.config.presets.find((i) => i.name === "@unocss/preset-attributify");
41
- const attributifyPrefix = attributify?.options?.prefix ?? "un-";
42
- for (const item of Array.from(code.original.matchAll(elementRE))) {
43
- let attributifyPart = item[2];
44
- if (valuedAttributeRE.test(attributifyPart)) {
45
- attributifyPart = attributifyPart.replace(valuedAttributeRE, (match, _2, dynamicFlagStart) => {
46
- if (!dynamicFlagStart)
47
- return " ".repeat(match.length);
48
- let preLastModifierIndex = 0;
49
- let temp = match;
50
- for (const _item of match.matchAll(elementRE)) {
51
- const attrAttributePart = _item[2];
52
- if (valuedAttributeRE.test(attrAttributePart))
53
- attrAttributePart.replace(valuedAttributeRE, (m) => " ".repeat(m.length));
54
- const pre = temp.slice(0, preLastModifierIndex) + " ".repeat(_item.index + _item[0].indexOf(_item[2]) - preLastModifierIndex) + attrAttributePart;
55
- temp = pre + " ".repeat(_item.input.length - pre.length);
56
- preLastModifierIndex = pre.length;
57
- }
58
- if (preLastModifierIndex !== 0)
59
- return temp;
60
- return " ".repeat(match.length);
61
- });
42
+ const ast = parse(code.toString(), {
43
+ sourceType: "module",
44
+ plugins: ["jsx", "typescript"]
45
+ });
46
+ traverse(ast, {
47
+ JSXAttribute(path) {
48
+ if (path.node.value === null) {
49
+ const attr = path.node.name.type === "JSXNamespacedName" ? `${path.node.name.namespace.name}:${path.node.name.name.name}` : path.node.name.name;
50
+ if (isBlocked(attr))
51
+ return;
52
+ tasks.push(
53
+ uno.parseToken(attr).then((matched) => {
54
+ if (matched) {
55
+ code.appendRight(path.node.end, '=""');
56
+ }
57
+ })
58
+ );
59
+ }
62
60
  }
63
- for (const attr of attributifyPart.matchAll(attributeRE)) {
64
- const matchedRule = attr[0].replace(/:/, "-");
65
- if (matchedRule.includes("=") || isBlocked(matchedRule))
66
- continue;
67
- const updatedMatchedRule = matchedRule.startsWith(attributifyPrefix) ? matchedRule.slice(attributifyPrefix.length) : matchedRule;
68
- tasks.push(uno.parseToken(updatedMatchedRule).then((matched) => {
69
- if (matched) {
70
- const startIdx = (item.index || 0) + (attr.index || 0) + item[0].indexOf(item[2]);
71
- const endIdx = startIdx + matchedRule.length;
72
- code.overwrite(startIdx, endIdx, `${matchedRule}=""`);
73
- }
74
- }));
75
- }
76
- }
61
+ });
77
62
  await Promise.all(tasks);
78
63
  }
79
64
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@unocss/transformer-attributify-jsx",
3
3
  "type": "module",
4
- "version": "66.3.3",
4
+ "version": "66.4.1",
5
5
  "description": "Support valueless attributify in JSX/TSX.",
6
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",
@@ -33,7 +33,9 @@
33
33
  "dist"
34
34
  ],
35
35
  "dependencies": {
36
- "@unocss/core": "66.3.3"
36
+ "@babel/parser": "^7.28.0",
37
+ "@babel/traverse": "^7.28.0",
38
+ "@unocss/core": "66.4.1"
37
39
  },
38
40
  "devDependencies": {
39
41
  "magic-string": "^0.30.17"