unocss-transformer-alias 0.0.2 → 0.0.4

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
@@ -8,33 +8,60 @@ function transformerAlias(options = {}) {
8
8
  return {
9
9
  name: "unocss-transformer-alias",
10
10
  enforce: "pre",
11
- transform(code, _, { uno }) {
12
- transformAlias(code, uno, options);
11
+ async transform(code, _, { uno }) {
12
+ await transformAlias(code, uno, options);
13
13
  }
14
14
  };
15
15
  }
16
- function transformAlias(code, uno, options = {}) {
16
+ async function transformAlias(code, uno, options = {}) {
17
17
  const prefix = options.prefix ?? "*";
18
18
  const extraRE = new RegExp(`${escapeRegExp(prefix)}([\\w-]+)`, "g");
19
+ const map = /* @__PURE__ */ new Map();
19
20
  for (const item of Array.from(code.original.matchAll(extraRE))) {
20
- for (const shortcut of uno.config.shortcuts) {
21
- let result;
22
- if (core.isStaticShortcut(shortcut)) {
23
- if (shortcut[0] === item[1])
24
- result = shortcut[1];
21
+ let result = map.get(item[0]);
22
+ if (result === false) {
23
+ continue;
24
+ } else if (!result) {
25
+ const r = await expandShortcut(item[1], uno);
26
+ if (r) {
27
+ result = r[0].join(" ");
28
+ map.set(item[0], result);
25
29
  } else {
26
- const match = item[1].match(shortcut[0]);
27
- if (match !== null)
28
- result = shortcut[1](match, uno.config);
30
+ map.set(item[0], false);
31
+ continue;
29
32
  }
30
- if (core.isString(result))
31
- code.overwrite(item.index, item.index + item[0].length, core.expandVariantGroup(result.trim()));
32
33
  }
34
+ code.overwrite(item.index, item.index + item[0].length, result);
33
35
  }
34
36
  }
37
+ async function expandShortcut(input, uno, depth = 5) {
38
+ if (depth <= 0)
39
+ return;
40
+ let result;
41
+ for (const shortcut of uno.config.shortcuts) {
42
+ if (core.isStaticShortcut(shortcut)) {
43
+ if (shortcut[0] === input)
44
+ result = shortcut[1];
45
+ } else {
46
+ const match = input.match(shortcut[0]);
47
+ if (match != null)
48
+ result = shortcut[1](match, {});
49
+ }
50
+ }
51
+ if (core.isString(result))
52
+ result = core.expandVariantGroup(result.trim()).split(/\s+/g);
53
+ if (!result)
54
+ return;
55
+ return [
56
+ (await Promise.all(
57
+ result.filter((s) => s !== input).map(async (r) => (core.isString(r) ? (await expandShortcut(r, uno, depth - 1))?.[0] : void 0) || [r])
58
+ )).flat(1).filter(Boolean)
59
+ ];
60
+ }
35
61
  function escapeRegExp(str) {
36
62
  return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
37
63
  }
38
64
 
39
65
  exports.default = transformerAlias;
66
+ exports.expandShortcut = expandShortcut;
40
67
  exports.transformAlias = transformAlias;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { SourceCodeTransformer, UnoGenerator } from '@unocss/core';
1
+ import { SourceCodeTransformer, UnoGenerator, ShortcutValue } from '@unocss/core';
2
2
  import MagicString from 'magic-string';
3
3
 
4
4
  interface TransformerAliasOptions {
@@ -10,6 +10,7 @@ interface TransformerAliasOptions {
10
10
  prefix?: string;
11
11
  }
12
12
  declare function transformerAlias(options?: TransformerAliasOptions): SourceCodeTransformer;
13
- declare function transformAlias(code: MagicString, uno: UnoGenerator, options?: TransformerAliasOptions): void;
13
+ declare function transformAlias(code: MagicString, uno: UnoGenerator, options?: TransformerAliasOptions): Promise<void>;
14
+ declare function expandShortcut(input: string, uno: UnoGenerator, depth?: number): Promise<[ShortcutValue[]] | undefined>;
14
15
 
15
- export { TransformerAliasOptions, transformerAlias as default, transformAlias };
16
+ export { TransformerAliasOptions, transformerAlias as default, expandShortcut, transformAlias };
package/dist/index.mjs CHANGED
@@ -4,32 +4,58 @@ function transformerAlias(options = {}) {
4
4
  return {
5
5
  name: "unocss-transformer-alias",
6
6
  enforce: "pre",
7
- transform(code, _, { uno }) {
8
- transformAlias(code, uno, options);
7
+ async transform(code, _, { uno }) {
8
+ await transformAlias(code, uno, options);
9
9
  }
10
10
  };
11
11
  }
12
- function transformAlias(code, uno, options = {}) {
12
+ async function transformAlias(code, uno, options = {}) {
13
13
  const prefix = options.prefix ?? "*";
14
14
  const extraRE = new RegExp(`${escapeRegExp(prefix)}([\\w-]+)`, "g");
15
+ const map = /* @__PURE__ */ new Map();
15
16
  for (const item of Array.from(code.original.matchAll(extraRE))) {
16
- for (const shortcut of uno.config.shortcuts) {
17
- let result;
18
- if (isStaticShortcut(shortcut)) {
19
- if (shortcut[0] === item[1])
20
- result = shortcut[1];
17
+ let result = map.get(item[0]);
18
+ if (result === false) {
19
+ continue;
20
+ } else if (!result) {
21
+ const r = await expandShortcut(item[1], uno);
22
+ if (r) {
23
+ result = r[0].join(" ");
24
+ map.set(item[0], result);
21
25
  } else {
22
- const match = item[1].match(shortcut[0]);
23
- if (match !== null)
24
- result = shortcut[1](match, uno.config);
26
+ map.set(item[0], false);
27
+ continue;
25
28
  }
26
- if (isString(result))
27
- code.overwrite(item.index, item.index + item[0].length, expandVariantGroup(result.trim()));
28
29
  }
30
+ code.overwrite(item.index, item.index + item[0].length, result);
29
31
  }
30
32
  }
33
+ async function expandShortcut(input, uno, depth = 5) {
34
+ if (depth <= 0)
35
+ return;
36
+ let result;
37
+ for (const shortcut of uno.config.shortcuts) {
38
+ if (isStaticShortcut(shortcut)) {
39
+ if (shortcut[0] === input)
40
+ result = shortcut[1];
41
+ } else {
42
+ const match = input.match(shortcut[0]);
43
+ if (match != null)
44
+ result = shortcut[1](match, {});
45
+ }
46
+ }
47
+ if (isString(result))
48
+ result = expandVariantGroup(result.trim()).split(/\s+/g);
49
+ if (!result)
50
+ return;
51
+ return [
52
+ (await Promise.all(
53
+ result.filter((s) => s !== input).map(async (r) => (isString(r) ? (await expandShortcut(r, uno, depth - 1))?.[0] : void 0) || [r])
54
+ )).flat(1).filter(Boolean)
55
+ ];
56
+ }
31
57
  function escapeRegExp(str) {
32
58
  return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
33
59
  }
34
60
 
35
- export { transformerAlias as default, transformAlias };
61
+ export { transformerAlias as default, expandShortcut, transformAlias };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "unocss-transformer-alias",
3
3
  "type": "module",
4
- "version": "0.0.2",
4
+ "version": "0.0.4",
5
5
  "description": "Transform alias for UnoCSS shortcuts",
6
6
  "author": "Chris <https://github.com/zyyv>",
7
7
  "license": "MIT",
@@ -62,6 +62,7 @@
62
62
  "lint": "eslint .",
63
63
  "test": "vitest",
64
64
  "test:update": "vitest -u",
65
- "typecheck": "tcs --noEmit"
65
+ "typecheck": "tcs --noEmit",
66
+ "play": "npm -C playground run dev"
66
67
  }
67
68
  }