@unocss/transformer-directives 0.62.4 → 0.63.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.
Files changed (2) hide show
  1. package/dist/index.mjs +66 -5
  2. package/package.json +4 -4
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { expandVariantGroup, notNull, regexScopePlaceholder, toArray, cssIdRE } from '@unocss/core';
2
- import { transformThemeString, hasThemeFn } from '@unocss/rule-utils';
2
+ import { transformThemeString, hasThemeFn, hasIconFn } from '@unocss/rule-utils';
3
3
  import { generate, parse, clone, List, walk } from 'css-tree';
4
4
 
5
5
  async function handleApply(ctx, node) {
@@ -89,7 +89,58 @@ function removeComments(value) {
89
89
  return value.replace(/(\/\*(?:.|\n)*?\*\/)|(\/\/.*)/g, "");
90
90
  }
91
91
 
92
- function handleFunction({ code, uno, options }, node) {
92
+ async function transformIconString(uno, icon, color) {
93
+ const presetIcons = uno.userConfig.presets?.flat()?.findLast((i) => i.name === "@unocss/preset-icons");
94
+ if (!presetIcons) {
95
+ console.warn("@unocss/preset-icons not found, icon() directive will be keep as-is");
96
+ return;
97
+ }
98
+ const {
99
+ scale = 1,
100
+ prefix = "i-",
101
+ collections: customCollections,
102
+ extraProperties = {},
103
+ customizations = {},
104
+ autoInstall = false,
105
+ collectionsNodeResolvePath,
106
+ unit
107
+ } = presetIcons.options;
108
+ const api = presetIcons.api;
109
+ const loaderOptions = {
110
+ addXmlNs: true,
111
+ scale,
112
+ customCollections,
113
+ autoInstall,
114
+ cwd: collectionsNodeResolvePath,
115
+ // avoid warn from @iconify/loader: we'll warn below if not found
116
+ warn: void 0,
117
+ customizations: {
118
+ ...customizations,
119
+ additionalProps: { ...extraProperties },
120
+ trimCustomSvg: true,
121
+ async iconCustomizer(collection, icon2, props) {
122
+ await customizations.iconCustomizer?.(collection, icon2, props);
123
+ if (unit) {
124
+ if (!props.width)
125
+ props.width = `${scale}${unit}`;
126
+ if (!props.height)
127
+ props.height = `${scale}${unit}`;
128
+ }
129
+ }
130
+ }
131
+ };
132
+ const loader = await api.createNodeLoader?.() || (async () => void 0);
133
+ for (const p of toArray(prefix)) {
134
+ if (icon.startsWith(p)) {
135
+ icon = icon.slice(p.length);
136
+ const parsed = await api.parseIconWithLoader(icon, loader, loaderOptions);
137
+ if (parsed)
138
+ return `url("data:image/svg+xml;utf8,${color ? api.encodeSvgForCss(parsed.svg).replace(/currentcolor/gi, color) : api.encodeSvgForCss(parsed.svg)}")`;
139
+ }
140
+ }
141
+ }
142
+
143
+ async function handleFunction({ code, uno, options }, node) {
93
144
  const { throwOnMissing = true } = options;
94
145
  switch (node.name) {
95
146
  case "theme": {
@@ -99,6 +150,16 @@ function handleFunction({ code, uno, options }, node) {
99
150
  const value = transformThemeString(themeStr, uno.config.theme, throwOnMissing);
100
151
  if (value)
101
152
  code.overwrite(node.loc.start.offset, node.loc.end.offset, value);
153
+ break;
154
+ }
155
+ case "icon": {
156
+ const params = node.children.toArray().filter((i) => i.type === "String").map((i) => i.value);
157
+ if (params.length === 0)
158
+ throw new Error("icon() expects at least one argument");
159
+ const value = await transformIconString(uno, ...params);
160
+ if (value)
161
+ code.overwrite(node.loc.start.offset, node.loc.end.offset, value);
162
+ break;
102
163
  }
103
164
  }
104
165
  }
@@ -171,8 +232,8 @@ async function transformDirectives(code, uno, options, filename, originalCode, o
171
232
  const parseCode = originalCode || code.original;
172
233
  const hasApply = parseCode.includes("@apply") || applyVariable.some((s) => parseCode.includes(s));
173
234
  const hasScreen = parseCode.includes("@screen");
174
- const hasThemeFn$1 = hasThemeFn(parseCode);
175
- if (!hasApply && !hasThemeFn$1 && !hasScreen)
235
+ const hasFn = hasThemeFn(parseCode) || hasIconFn(parseCode);
236
+ if (!hasApply && !hasFn && !hasScreen)
176
237
  return;
177
238
  const ast = parse(parseCode, {
178
239
  parseCustomProperty: true,
@@ -196,7 +257,7 @@ async function transformDirectives(code, uno, options, filename, originalCode, o
196
257
  if (hasScreen && node.type === "Atrule")
197
258
  handleScreen(ctx, node);
198
259
  if (node.type === "Function")
199
- handleFunction(ctx, node);
260
+ await handleFunction(ctx, node);
200
261
  if (hasApply && node.type === "Rule")
201
262
  await handleApply(ctx, node);
202
263
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@unocss/transformer-directives",
3
3
  "type": "module",
4
- "version": "0.62.4",
4
+ "version": "0.63.1",
5
5
  "description": "UnoCSS transformer for `@apply` directive",
6
6
  "author": "hannoeru <me@hanlee.co>",
7
7
  "license": "MIT",
@@ -32,9 +32,9 @@
32
32
  "dist"
33
33
  ],
34
34
  "dependencies": {
35
- "css-tree": "^2.3.1",
36
- "@unocss/rule-utils": "0.62.4",
37
- "@unocss/core": "0.62.4"
35
+ "css-tree": "^3.0.0",
36
+ "@unocss/rule-utils": "0.63.1",
37
+ "@unocss/core": "0.63.1"
38
38
  },
39
39
  "devDependencies": {
40
40
  "magic-string": "^0.30.11"