@unocss/transformer-compile-class 0.58.9 → 0.59.0-beta.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.
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@unocss/transformer-compile-class",
3
- "version": "0.58.9",
3
+ "type": "module",
4
+ "version": "0.59.0-beta.1",
4
5
  "description": "Compile group of classes into one class",
5
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
6
7
  "license": "MIT",
@@ -21,19 +22,18 @@
21
22
  "sideEffects": false,
22
23
  "exports": {
23
24
  ".": {
24
- "types": "./dist/index.d.ts",
25
- "import": "./dist/index.mjs",
26
- "require": "./dist/index.cjs"
25
+ "types": "./dist/index.d.mts",
26
+ "default": "./dist/index.mjs"
27
27
  }
28
28
  },
29
- "main": "./dist/index.cjs",
29
+ "main": "./dist/index.mjs",
30
30
  "module": "./dist/index.mjs",
31
31
  "types": "./dist/index.d.ts",
32
32
  "files": [
33
33
  "dist"
34
34
  ],
35
35
  "dependencies": {
36
- "@unocss/core": "0.58.9"
36
+ "@unocss/core": "0.59.0-beta.1"
37
37
  },
38
38
  "devDependencies": {
39
39
  "magic-string": "^0.30.8"
package/dist/index.cjs DELETED
@@ -1,79 +0,0 @@
1
- 'use strict';
2
-
3
- const core = require('@unocss/core');
4
-
5
- function transformerCompileClass(options = {}) {
6
- const {
7
- trigger = /(["'`]):uno(?:-)?(?<name>[^\s\1]+)?:\s([^\1]*?)\1/g,
8
- classPrefix = "uno-",
9
- hashFn = hash,
10
- keepUnknown = true,
11
- alwaysHash = false
12
- } = options;
13
- const compiledClass = /* @__PURE__ */ new Set();
14
- const regexp = typeof trigger === "string" ? RegExp(`(["'\`])${core.escapeRegExp(trigger)}\\s([^\\1]*?)\\1`, "g") : trigger;
15
- return {
16
- name: "@unocss/transformer-compile-class",
17
- enforce: "pre",
18
- async transform(s, _, { uno, tokens, invalidate }) {
19
- const matches = [...s.original.matchAll(regexp)];
20
- if (!matches.length)
21
- return;
22
- const size = compiledClass.size;
23
- for (const match of matches) {
24
- let body = match.length === 4 && match.groups ? core.expandVariantGroup(match[3].trim()) : core.expandVariantGroup(match[2].trim());
25
- const start = match.index;
26
- const replacements = [];
27
- if (keepUnknown) {
28
- const result = await Promise.all(body.split(/\s+/).filter(Boolean).map(async (i) => [i, !!await uno.parseToken(i)]));
29
- const known = result.filter(([, matched]) => matched).map(([i]) => i);
30
- const unknown = result.filter(([, matched]) => !matched).map(([i]) => i);
31
- replacements.push(...unknown);
32
- body = known.join(" ");
33
- }
34
- if (body) {
35
- body = body.split(/\s+/).sort().join(" ");
36
- let hash2;
37
- let explicitName = false;
38
- if (match.groups && match.groups.name) {
39
- hash2 = match.groups.name;
40
- if (alwaysHash)
41
- hash2 += `-${hashFn(body)}`;
42
- explicitName = true;
43
- } else {
44
- hash2 = hashFn(body);
45
- }
46
- const className = `${classPrefix}${hash2}`;
47
- if (tokens && tokens.has(className) && explicitName) {
48
- const existing = uno.config.shortcuts.find((i) => i[0] === className);
49
- if (existing && existing[1] !== body)
50
- throw new Error(`Duplicated compile class name "${className}". One is "${body}" and the other is "${existing[1]}". Please choose different class name or set 'alwaysHash' to 'true'.`);
51
- }
52
- compiledClass.add(className);
53
- replacements.unshift(className);
54
- if (options.layer)
55
- uno.config.shortcuts.push([className, body, { layer: options.layer }]);
56
- else
57
- uno.config.shortcuts.push([className, body]);
58
- if (tokens)
59
- tokens.add(className);
60
- }
61
- s.overwrite(start + 1, start + match[0].length - 1, replacements.join(" "));
62
- }
63
- if (compiledClass.size > size)
64
- invalidate();
65
- }
66
- };
67
- }
68
- function hash(str) {
69
- let i;
70
- let l;
71
- let hval = 2166136261;
72
- for (i = 0, l = str.length; i < l; i++) {
73
- hval ^= str.charCodeAt(i);
74
- hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
75
- }
76
- return `00000${(hval >>> 0).toString(36)}`.slice(-6);
77
- }
78
-
79
- module.exports = transformerCompileClass;
package/dist/index.d.cts DELETED
@@ -1,62 +0,0 @@
1
- import { SourceCodeTransformer } from '@unocss/core';
2
-
3
- interface CompileClassOptions {
4
- /**
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(?:-)?(?<name>[^\s\1]+)?:\s([^\1]*?)\1/g`
32
- */
33
- trigger?: string | RegExp;
34
- /**
35
- * Prefix for compile class name
36
- * @default 'uno-'
37
- */
38
- classPrefix?: string;
39
- /**
40
- * Hash function
41
- */
42
- hashFn?: (str: string) => string;
43
- /**
44
- * Allow add hash to class name even if the class name is explicitly defined
45
- *
46
- * @default false
47
- */
48
- alwaysHash?: boolean;
49
- /**
50
- * Left unknown classes inside the string
51
- *
52
- * @default true
53
- */
54
- keepUnknown?: boolean;
55
- /**
56
- * The layer name of generated rules
57
- */
58
- layer?: string;
59
- }
60
- declare function transformerCompileClass(options?: CompileClassOptions): SourceCodeTransformer;
61
-
62
- export { type CompileClassOptions, transformerCompileClass as default };