@unocss/transformer-directives 0.56.5 → 0.57.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
@@ -6,45 +6,11 @@ const core = require('@unocss/core');
6
6
  const cssTree = require('css-tree');
7
7
  const ruleUtils = require('@unocss/rule-utils');
8
8
 
9
- const themeFnRE = /theme\((.*?)\)/g;
10
9
  function handleThemeFn({ code, uno, options }, node) {
11
10
  const { throwOnMissing = true } = options;
12
11
  const offset = node.value.loc.start.offset;
13
12
  const str = code.original.slice(offset, node.value.loc.end.offset);
14
- const matches = Array.from(str.matchAll(themeFnRE));
15
- if (!matches.length)
16
- return;
17
- for (const match of matches) {
18
- const rawArg = match[1].trim();
19
- if (!rawArg)
20
- throw new Error("theme() expect exact one argument, but got 0");
21
- const [rawKey, alpha] = rawArg.slice(1, -1).split("/");
22
- let value = uno.config.theme;
23
- const keys = rawKey.trim().split(".");
24
- keys.every((key) => {
25
- if (value[key] != null)
26
- value = value[key];
27
- else if (value[+key] != null)
28
- value = value[+key];
29
- else
30
- return false;
31
- return true;
32
- });
33
- if (typeof value === "string") {
34
- if (alpha) {
35
- const color = ruleUtils.parseCssColor(value);
36
- if (color)
37
- value = ruleUtils.colorToString(color, alpha);
38
- }
39
- code.overwrite(
40
- offset + match.index,
41
- offset + match.index + match[0].length,
42
- value
43
- );
44
- } else if (throwOnMissing) {
45
- throw new Error(`theme of "${rawArg.slice(1, -1)}" did not found`);
46
- }
47
- }
13
+ code.overwrite(offset, node.value.loc.end.offset, ruleUtils.transformThemeFn(str, uno.config.theme, throwOnMissing));
48
14
  }
49
15
 
50
16
  const screenRuleRE = /(@screen) (.+) /g;
@@ -66,9 +32,9 @@ function handleScreen({ code, uno }, node) {
66
32
  breakpoints = uno.userConfig.theme.breakpoints;
67
33
  if (!breakpoints)
68
34
  breakpoints = uno.config.theme.breakpoints;
69
- return breakpoints;
35
+ return breakpoints ? Object.entries(breakpoints).sort((a, b) => Number.parseInt(a[1].replace(/[a-z]+/gi, "")) - Number.parseInt(b[1].replace(/[a-z]+/gi, ""))).map(([point, size]) => ({ point, size })) : void 0;
70
36
  };
71
- const variantEntries = Object.entries(resolveBreakpoints() ?? {}).map(([point, size], idx) => [point, size, idx]);
37
+ const variantEntries = (resolveBreakpoints() ?? []).map(({ point, size }, idx) => [point, size, idx]);
72
38
  const generateMediaQuery = (breakpointName2, prefix2) => {
73
39
  const [, size, idx] = variantEntries.find((i) => i[0] === breakpointName2);
74
40
  if (prefix2) {
@@ -194,7 +160,7 @@ async function transformDirectives(code, uno, options, filename, originalCode, o
194
160
  applyVariable = core.toArray(applyVariable || []);
195
161
  const hasApply = code.original.includes("@apply") || applyVariable.some((s) => code.original.includes(s));
196
162
  const hasScreen = code.original.includes("@screen");
197
- const hasThemeFn = code.original.match(themeFnRE);
163
+ const hasThemeFn = ruleUtils.hasThemeFn(code.original);
198
164
  if (!hasApply && !hasThemeFn && !hasScreen)
199
165
  return;
200
166
  const ast = cssTree.parse(originalCode || code.original, {
package/dist/index.mjs CHANGED
@@ -1,46 +1,12 @@
1
1
  import { expandVariantGroup, notNull, regexScopePlaceholder, cssIdRE, toArray } from '@unocss/core';
2
2
  import { generate, parse, clone, walk } from 'css-tree';
3
- import { parseCssColor, colorToString } from '@unocss/rule-utils';
3
+ import { transformThemeFn, hasThemeFn } from '@unocss/rule-utils';
4
4
 
5
- const themeFnRE = /theme\((.*?)\)/g;
6
5
  function handleThemeFn({ code, uno, options }, node) {
7
6
  const { throwOnMissing = true } = options;
8
7
  const offset = node.value.loc.start.offset;
9
8
  const str = code.original.slice(offset, node.value.loc.end.offset);
10
- const matches = Array.from(str.matchAll(themeFnRE));
11
- if (!matches.length)
12
- return;
13
- for (const match of matches) {
14
- const rawArg = match[1].trim();
15
- if (!rawArg)
16
- throw new Error("theme() expect exact one argument, but got 0");
17
- const [rawKey, alpha] = rawArg.slice(1, -1).split("/");
18
- let value = uno.config.theme;
19
- const keys = rawKey.trim().split(".");
20
- keys.every((key) => {
21
- if (value[key] != null)
22
- value = value[key];
23
- else if (value[+key] != null)
24
- value = value[+key];
25
- else
26
- return false;
27
- return true;
28
- });
29
- if (typeof value === "string") {
30
- if (alpha) {
31
- const color = parseCssColor(value);
32
- if (color)
33
- value = colorToString(color, alpha);
34
- }
35
- code.overwrite(
36
- offset + match.index,
37
- offset + match.index + match[0].length,
38
- value
39
- );
40
- } else if (throwOnMissing) {
41
- throw new Error(`theme of "${rawArg.slice(1, -1)}" did not found`);
42
- }
43
- }
9
+ code.overwrite(offset, node.value.loc.end.offset, transformThemeFn(str, uno.config.theme, throwOnMissing));
44
10
  }
45
11
 
46
12
  const screenRuleRE = /(@screen) (.+) /g;
@@ -62,9 +28,9 @@ function handleScreen({ code, uno }, node) {
62
28
  breakpoints = uno.userConfig.theme.breakpoints;
63
29
  if (!breakpoints)
64
30
  breakpoints = uno.config.theme.breakpoints;
65
- return breakpoints;
31
+ return breakpoints ? Object.entries(breakpoints).sort((a, b) => Number.parseInt(a[1].replace(/[a-z]+/gi, "")) - Number.parseInt(b[1].replace(/[a-z]+/gi, ""))).map(([point, size]) => ({ point, size })) : void 0;
66
32
  };
67
- const variantEntries = Object.entries(resolveBreakpoints() ?? {}).map(([point, size], idx) => [point, size, idx]);
33
+ const variantEntries = (resolveBreakpoints() ?? []).map(({ point, size }, idx) => [point, size, idx]);
68
34
  const generateMediaQuery = (breakpointName2, prefix2) => {
69
35
  const [, size, idx] = variantEntries.find((i) => i[0] === breakpointName2);
70
36
  if (prefix2) {
@@ -190,8 +156,8 @@ async function transformDirectives(code, uno, options, filename, originalCode, o
190
156
  applyVariable = toArray(applyVariable || []);
191
157
  const hasApply = code.original.includes("@apply") || applyVariable.some((s) => code.original.includes(s));
192
158
  const hasScreen = code.original.includes("@screen");
193
- const hasThemeFn = code.original.match(themeFnRE);
194
- if (!hasApply && !hasThemeFn && !hasScreen)
159
+ const hasThemeFn$1 = hasThemeFn(code.original);
160
+ if (!hasApply && !hasThemeFn$1 && !hasScreen)
195
161
  return;
196
162
  const ast = parse(originalCode || code.original, {
197
163
  parseAtrulePrelude: false,
@@ -212,7 +178,7 @@ async function transformDirectives(code, uno, options, filename, originalCode, o
212
178
  const processNode = async (node, _item, _list) => {
213
179
  if (hasScreen && node.type === "Atrule")
214
180
  handleScreen(ctx, node);
215
- if (hasThemeFn && node.type === "Declaration")
181
+ if (hasThemeFn$1 && node.type === "Declaration")
216
182
  handleThemeFn(ctx, node);
217
183
  if (hasApply && node.type === "Rule")
218
184
  await handleApply(ctx, node);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/transformer-directives",
3
- "version": "0.56.5",
3
+ "version": "0.57.0",
4
4
  "description": "UnoCSS transformer for `@apply` directive",
5
5
  "author": "hannoeru <me@hanlee.co>",
6
6
  "license": "MIT",
@@ -33,11 +33,11 @@
33
33
  ],
34
34
  "dependencies": {
35
35
  "css-tree": "^2.3.1",
36
- "@unocss/core": "0.56.5",
37
- "@unocss/rule-utils": "0.56.5"
36
+ "@unocss/core": "0.57.0",
37
+ "@unocss/rule-utils": "0.57.0"
38
38
  },
39
39
  "devDependencies": {
40
- "magic-string": "^0.30.3"
40
+ "magic-string": "^0.30.5"
41
41
  },
42
42
  "scripts": {
43
43
  "build": "unbuild",