unocss-transformer-alias 0.0.3 → 0.0.5

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
@@ -4,25 +4,27 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  const core = require('@unocss/core');
6
6
 
7
- function transformerAlias(options = {}) {
7
+ 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
- async function transformAlias(code, uno, options = {}) {
17
- const prefix = options.prefix ?? "*";
18
- const extraRE = new RegExp(`${escapeRegExp(prefix)}([\\w-]+)`, "g");
16
+ async function transformAlias(code, uno, {
17
+ prefix = "*",
18
+ keep = "+"
19
+ } = {}) {
20
+ const extraRE = new RegExp(`(${escapeRegExp(prefix)}|${escapeRegExp(keep)})([\\w-]+)`, "g");
19
21
  const map = /* @__PURE__ */ new Map();
20
22
  for (const item of Array.from(code.original.matchAll(extraRE))) {
21
23
  let result = map.get(item[0]);
22
24
  if (result === false) {
23
25
  continue;
24
26
  } else if (!result) {
25
- const r = await expandShortcut(item[1], uno);
27
+ const r = await expandShortcut(item[2], uno);
26
28
  if (r) {
27
29
  result = r[0].join(" ");
28
30
  map.set(item[0], result);
@@ -31,6 +33,8 @@ async function transformAlias(code, uno, options = {}) {
31
33
  continue;
32
34
  }
33
35
  }
36
+ if (item[1] === keep)
37
+ result = `${item[2]} ${result}`;
34
38
  code.overwrite(item.index, item.index + item[0].length, result);
35
39
  }
36
40
  }
package/dist/index.d.ts CHANGED
@@ -8,9 +8,15 @@ interface TransformerAliasOptions {
8
8
  * @default "*"
9
9
  */
10
10
  prefix?: string;
11
+ /**
12
+ * Prefix for your alias and keep the original class.
13
+ *
14
+ * @default '+'
15
+ */
16
+ keep?: string;
11
17
  }
12
18
  declare function transformerAlias(options?: TransformerAliasOptions): SourceCodeTransformer;
13
- declare function transformAlias(code: MagicString, uno: UnoGenerator, options?: TransformerAliasOptions): Promise<void>;
19
+ declare function transformAlias(code: MagicString, uno: UnoGenerator, { prefix, keep, }?: TransformerAliasOptions): Promise<void>;
14
20
  declare function expandShortcut(input: string, uno: UnoGenerator, depth?: number): Promise<[ShortcutValue[]] | undefined>;
15
21
 
16
22
  export { TransformerAliasOptions, transformerAlias as default, expandShortcut, transformAlias };
package/dist/index.mjs CHANGED
@@ -1,24 +1,26 @@
1
1
  import { isStaticShortcut, isString, expandVariantGroup } from '@unocss/core';
2
2
 
3
- function transformerAlias(options = {}) {
3
+ 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
- async function transformAlias(code, uno, options = {}) {
13
- const prefix = options.prefix ?? "*";
14
- const extraRE = new RegExp(`${escapeRegExp(prefix)}([\\w-]+)`, "g");
12
+ async function transformAlias(code, uno, {
13
+ prefix = "*",
14
+ keep = "+"
15
+ } = {}) {
16
+ const extraRE = new RegExp(`(${escapeRegExp(prefix)}|${escapeRegExp(keep)})([\\w-]+)`, "g");
15
17
  const map = /* @__PURE__ */ new Map();
16
18
  for (const item of Array.from(code.original.matchAll(extraRE))) {
17
19
  let result = map.get(item[0]);
18
20
  if (result === false) {
19
21
  continue;
20
22
  } else if (!result) {
21
- const r = await expandShortcut(item[1], uno);
23
+ const r = await expandShortcut(item[2], uno);
22
24
  if (r) {
23
25
  result = r[0].join(" ");
24
26
  map.set(item[0], result);
@@ -27,6 +29,8 @@ async function transformAlias(code, uno, options = {}) {
27
29
  continue;
28
30
  }
29
31
  }
32
+ if (item[1] === keep)
33
+ result = `${item[2]} ${result}`;
30
34
  code.overwrite(item.index, item.index + item[0].length, result);
31
35
  }
32
36
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "unocss-transformer-alias",
3
3
  "type": "module",
4
- "version": "0.0.3",
4
+ "version": "0.0.5",
5
5
  "description": "Transform alias for UnoCSS shortcuts",
6
6
  "author": "Chris <https://github.com/zyyv>",
7
7
  "license": "MIT",