@unocss/transformer-directives 0.35.3 → 0.37.0

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/dist/index.cjs CHANGED
@@ -15,13 +15,14 @@ function transformerDirectives(options = {}) {
15
15
  }
16
16
  };
17
17
  }
18
+ const themeFnRE = /theme\((.*?)\)/g;
18
19
  async function transformDirectives(code, uno, options, filename, originalCode, offset) {
19
20
  const {
20
21
  varStyle = "--at-",
21
22
  throwOnMissing = true
22
23
  } = options;
23
24
  const isApply = code.original.includes("@apply") || varStyle !== false && code.original.includes(varStyle);
24
- const hasThemeFn = /theme\([^)]*?\)/.test(code.original);
25
+ const hasThemeFn = code.original.match(themeFnRE);
25
26
  if (!isApply && !hasThemeFn)
26
27
  return;
27
28
  const ast = cssTree.parse(originalCode || code.original, {
@@ -85,39 +86,39 @@ async function transformDirectives(code, uno, options, filename, originalCode, o
85
86
  code.remove(calcOffset(childNode.loc.start.offset), calcOffset(childNode.loc.end.offset));
86
87
  };
87
88
  const handleThemeFn = (node) => {
88
- if (node.type === "Function" && node.name === "theme" && node.children) {
89
- const children = node.children.toArray().filter((n) => n.type === "String");
90
- if (children.length !== 1)
91
- throw new Error(`theme() expect exact one argument, but got ${children.length}`);
92
- const matchedThemes = children.map((childNode) => {
93
- if (childNode.type !== "String")
94
- return null;
95
- const keys = childNode.value.split(".");
96
- let value = uno.config.theme;
97
- keys.every((key) => {
98
- if (!Reflect.has(value, key)) {
99
- value = null;
100
- return false;
101
- }
102
- value = value[key];
103
- return true;
104
- });
105
- if (typeof value === "string")
106
- return value;
107
- if (throwOnMissing)
108
- throw new Error(`theme of "${childNode.value}" did not found`);
109
- return null;
89
+ const value = node.value;
90
+ const offset2 = value.loc.start.offset;
91
+ const str = code.original.slice(offset2, value.loc.end.offset);
92
+ const matches = Array.from(str.matchAll(themeFnRE));
93
+ if (!matches.length)
94
+ return;
95
+ for (const match of matches) {
96
+ const rawArg = match[1].trim();
97
+ if (!rawArg)
98
+ throw new Error("theme() expect exact one argument, but got 0");
99
+ let value2 = uno.config.theme;
100
+ const keys = rawArg.slice(1, -1).split(".");
101
+ keys.every((key) => {
102
+ if (value2[key] != null)
103
+ value2 = value2[key];
104
+ else if (value2[+key] != null)
105
+ value2 = value2[+key];
106
+ else
107
+ return false;
108
+ return true;
110
109
  });
111
- if (matchedThemes.length !== children.length)
112
- return;
113
- code.overwrite(calcOffset(node.loc.start.offset), calcOffset(node.loc.end.offset), matchedThemes.join(" "));
110
+ if (typeof value2 === "string") {
111
+ code.overwrite(offset2 + match.index, offset2 + match.index + match[0].length, value2);
112
+ } else if (throwOnMissing) {
113
+ throw new Error(`theme of "${rawArg.slice(1, -1)}" did not found`);
114
+ }
114
115
  }
115
116
  };
116
117
  const stack = [];
117
118
  const processNode = async (node, _item, _list) => {
118
- if (hasThemeFn) {
119
+ if (hasThemeFn && node.type === "Declaration")
119
120
  handleThemeFn(node);
120
- } else if (isApply && node.type === "Rule") {
121
+ if (isApply && node.type === "Rule") {
121
122
  await Promise.all(node.block.children.map(async (childNode, _childItem) => {
122
123
  if (childNode.type === "Raw")
123
124
  return transformDirectives(code, uno, options, filename, childNode.value, calcOffset(childNode.loc.start.offset));
package/dist/index.mjs CHANGED
@@ -11,13 +11,14 @@ function transformerDirectives(options = {}) {
11
11
  }
12
12
  };
13
13
  }
14
+ const themeFnRE = /theme\((.*?)\)/g;
14
15
  async function transformDirectives(code, uno, options, filename, originalCode, offset) {
15
16
  const {
16
17
  varStyle = "--at-",
17
18
  throwOnMissing = true
18
19
  } = options;
19
20
  const isApply = code.original.includes("@apply") || varStyle !== false && code.original.includes(varStyle);
20
- const hasThemeFn = /theme\([^)]*?\)/.test(code.original);
21
+ const hasThemeFn = code.original.match(themeFnRE);
21
22
  if (!isApply && !hasThemeFn)
22
23
  return;
23
24
  const ast = parse(originalCode || code.original, {
@@ -81,39 +82,39 @@ async function transformDirectives(code, uno, options, filename, originalCode, o
81
82
  code.remove(calcOffset(childNode.loc.start.offset), calcOffset(childNode.loc.end.offset));
82
83
  };
83
84
  const handleThemeFn = (node) => {
84
- if (node.type === "Function" && node.name === "theme" && node.children) {
85
- const children = node.children.toArray().filter((n) => n.type === "String");
86
- if (children.length !== 1)
87
- throw new Error(`theme() expect exact one argument, but got ${children.length}`);
88
- const matchedThemes = children.map((childNode) => {
89
- if (childNode.type !== "String")
90
- return null;
91
- const keys = childNode.value.split(".");
92
- let value = uno.config.theme;
93
- keys.every((key) => {
94
- if (!Reflect.has(value, key)) {
95
- value = null;
96
- return false;
97
- }
98
- value = value[key];
99
- return true;
100
- });
101
- if (typeof value === "string")
102
- return value;
103
- if (throwOnMissing)
104
- throw new Error(`theme of "${childNode.value}" did not found`);
105
- return null;
85
+ const value = node.value;
86
+ const offset2 = value.loc.start.offset;
87
+ const str = code.original.slice(offset2, value.loc.end.offset);
88
+ const matches = Array.from(str.matchAll(themeFnRE));
89
+ if (!matches.length)
90
+ return;
91
+ for (const match of matches) {
92
+ const rawArg = match[1].trim();
93
+ if (!rawArg)
94
+ throw new Error("theme() expect exact one argument, but got 0");
95
+ let value2 = uno.config.theme;
96
+ const keys = rawArg.slice(1, -1).split(".");
97
+ keys.every((key) => {
98
+ if (value2[key] != null)
99
+ value2 = value2[key];
100
+ else if (value2[+key] != null)
101
+ value2 = value2[+key];
102
+ else
103
+ return false;
104
+ return true;
106
105
  });
107
- if (matchedThemes.length !== children.length)
108
- return;
109
- code.overwrite(calcOffset(node.loc.start.offset), calcOffset(node.loc.end.offset), matchedThemes.join(" "));
106
+ if (typeof value2 === "string") {
107
+ code.overwrite(offset2 + match.index, offset2 + match.index + match[0].length, value2);
108
+ } else if (throwOnMissing) {
109
+ throw new Error(`theme of "${rawArg.slice(1, -1)}" did not found`);
110
+ }
110
111
  }
111
112
  };
112
113
  const stack = [];
113
114
  const processNode = async (node, _item, _list) => {
114
- if (hasThemeFn) {
115
+ if (hasThemeFn && node.type === "Declaration")
115
116
  handleThemeFn(node);
116
- } else if (isApply && node.type === "Rule") {
117
+ if (isApply && node.type === "Rule") {
117
118
  await Promise.all(node.block.children.map(async (childNode, _childItem) => {
118
119
  if (childNode.type === "Raw")
119
120
  return transformDirectives(code, uno, options, filename, childNode.value, calcOffset(childNode.loc.start.offset));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/transformer-directives",
3
- "version": "0.35.3",
3
+ "version": "0.37.0",
4
4
  "description": "UnoCSS transformer for `@apply` directive",
5
5
  "author": "hannoeru <me@hanlee.co>",
6
6
  "license": "MIT",
@@ -31,7 +31,7 @@
31
31
  "dist"
32
32
  ],
33
33
  "dependencies": {
34
- "@unocss/core": "0.35.3",
34
+ "@unocss/core": "0.37.0",
35
35
  "css-tree": "^2.1.0"
36
36
  },
37
37
  "devDependencies": {