@unocss/transformer-compile-class 0.52.2 → 0.52.3

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,21 +4,21 @@ const core = require('@unocss/core');
4
4
 
5
5
  function transformerCompileClass(options = {}) {
6
6
  const {
7
- trigger = ":uno:",
7
+ trigger = /(["'`]):uno:\s([^\1]*?)\1/g,
8
8
  classPrefix = "uno-",
9
9
  hashFn = hash,
10
10
  keepUnknown = true
11
11
  } = options;
12
- const regex = new RegExp(`(["'\`])${core.escapeRegExp(trigger)}\\s([^\\1]*?)\\1`, "g");
12
+ const regexp = typeof trigger === "string" ? RegExp(`(["'\`])${core.escapeRegExp(trigger)}\\s([^\\1]*?)\\1`, "g") : trigger;
13
13
  return {
14
14
  name: "@unocss/transformer-compile-class",
15
15
  enforce: "pre",
16
16
  async transform(s, _, { uno, tokens }) {
17
- const matches = [...s.original.matchAll(regex)];
17
+ const matches = [...s.original.matchAll(regexp)];
18
18
  if (!matches.length)
19
19
  return;
20
20
  for (const match of matches) {
21
- let body = core.expandVariantGroup(match[2].trim());
21
+ let body = match.length === 4 && match.groups ? core.expandVariantGroup(match[3].trim()) : core.expandVariantGroup(match[2].trim());
22
22
  const start = match.index;
23
23
  const replacements = [];
24
24
  if (keepUnknown) {
@@ -30,14 +30,16 @@ function transformerCompileClass(options = {}) {
30
30
  }
31
31
  if (body) {
32
32
  body = body.split(/\s+/).sort().join(" ");
33
- const hash2 = hashFn(body);
34
- const className = `${classPrefix}${hash2}`;
33
+ const className = match.groups && match.groups.name ? `${classPrefix}${match.groups.name}` : `${classPrefix}${hashFn(body)}`;
34
+ if (tokens && tokens.has(className))
35
+ throw new Error(`duplicate compile class name '${className}', please choose different class name`);
35
36
  replacements.unshift(className);
36
37
  if (options.layer)
37
38
  uno.config.shortcuts.push([className, body, { layer: options.layer }]);
38
39
  else
39
40
  uno.config.shortcuts.push([className, body]);
40
- tokens.add(className);
41
+ if (tokens)
42
+ tokens.add(className);
41
43
  }
42
44
  s.overwrite(start + 1, start + match[0].length - 1, replacements.join(" "));
43
45
  }
package/dist/index.d.ts CHANGED
@@ -2,10 +2,35 @@ import { SourceCodeTransformer } from '@unocss/core';
2
2
 
3
3
  interface CompileClassOptions {
4
4
  /**
5
- * Trigger string
6
- * @default ':uno:'
5
+ * Trigger regex literal. The default trigger regex literal matches `:uno:`,
6
+ * for example: `<div class=":uno: font-bold text-white">`.
7
+ *
8
+ * @example
9
+ * The trigger additionally allows defining a capture group named `name`, which
10
+ * allows custom class names. One possible regex would be:
11
+ *
12
+ * ```
13
+ * export default defineConfig({
14
+ * transformers: [
15
+ * transformerCompileClass({
16
+ * trigger: /(["'`]):uno(?:-)?(?<name>[^\s\1]+)?:\s([^\1]*?)\1/g
17
+ * }),
18
+ * ],
19
+ * })
20
+ * ```
21
+ *
22
+ * This regular expression matches `:uno-MYNAME:` and uses `MYNAME` in
23
+ * combination with the class prefix as the final class name, for example:
24
+ * `.uno-MYNAME`. It should be noted that the regex literal needs to include
25
+ * the global flag `/g`.
26
+ *
27
+ * @note
28
+ * This parameter is backwards compatible. It accepts string only trigger
29
+ * words, like `:uno:` or a regex literal.
30
+ *
31
+ * @default `/(["'`]):uno:\s([^\1]*?)\1/g`
7
32
  */
8
- trigger?: string;
33
+ trigger?: string | RegExp;
9
34
  /**
10
35
  * Prefix for compile class name
11
36
  * @default 'uno-'
package/dist/index.mjs CHANGED
@@ -2,21 +2,21 @@ import { escapeRegExp, expandVariantGroup } from '@unocss/core';
2
2
 
3
3
  function transformerCompileClass(options = {}) {
4
4
  const {
5
- trigger = ":uno:",
5
+ trigger = /(["'`]):uno:\s([^\1]*?)\1/g,
6
6
  classPrefix = "uno-",
7
7
  hashFn = hash,
8
8
  keepUnknown = true
9
9
  } = options;
10
- const regex = new RegExp(`(["'\`])${escapeRegExp(trigger)}\\s([^\\1]*?)\\1`, "g");
10
+ const regexp = typeof trigger === "string" ? RegExp(`(["'\`])${escapeRegExp(trigger)}\\s([^\\1]*?)\\1`, "g") : trigger;
11
11
  return {
12
12
  name: "@unocss/transformer-compile-class",
13
13
  enforce: "pre",
14
14
  async transform(s, _, { uno, tokens }) {
15
- const matches = [...s.original.matchAll(regex)];
15
+ const matches = [...s.original.matchAll(regexp)];
16
16
  if (!matches.length)
17
17
  return;
18
18
  for (const match of matches) {
19
- let body = expandVariantGroup(match[2].trim());
19
+ let body = match.length === 4 && match.groups ? expandVariantGroup(match[3].trim()) : expandVariantGroup(match[2].trim());
20
20
  const start = match.index;
21
21
  const replacements = [];
22
22
  if (keepUnknown) {
@@ -28,14 +28,16 @@ function transformerCompileClass(options = {}) {
28
28
  }
29
29
  if (body) {
30
30
  body = body.split(/\s+/).sort().join(" ");
31
- const hash2 = hashFn(body);
32
- const className = `${classPrefix}${hash2}`;
31
+ const className = match.groups && match.groups.name ? `${classPrefix}${match.groups.name}` : `${classPrefix}${hashFn(body)}`;
32
+ if (tokens && tokens.has(className))
33
+ throw new Error(`duplicate compile class name '${className}', please choose different class name`);
33
34
  replacements.unshift(className);
34
35
  if (options.layer)
35
36
  uno.config.shortcuts.push([className, body, { layer: options.layer }]);
36
37
  else
37
38
  uno.config.shortcuts.push([className, body]);
38
- tokens.add(className);
39
+ if (tokens)
40
+ tokens.add(className);
39
41
  }
40
42
  s.overwrite(start + 1, start + match[0].length - 1, replacements.join(" "));
41
43
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/transformer-compile-class",
3
- "version": "0.52.2",
3
+ "version": "0.52.3",
4
4
  "description": "Compile group of classes into one class",
5
5
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
6
6
  "license": "MIT",
@@ -33,7 +33,7 @@
33
33
  "dist"
34
34
  ],
35
35
  "dependencies": {
36
- "@unocss/core": "0.52.2"
36
+ "@unocss/core": "0.52.3"
37
37
  },
38
38
  "devDependencies": {
39
39
  "magic-string": "^0.30.0"