@weapp-tailwindcss/runtime 0.1.2 → 0.1.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 +29 -9
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +23 -3
- package/package.json +5 -2
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
let clsx = require("clsx");
|
|
2
|
-
let
|
|
2
|
+
let _weapp_core_escape = require("@weapp-core/escape");
|
|
3
3
|
|
|
4
4
|
//#region src/transformers.ts
|
|
5
5
|
const identity = (value) => value;
|
|
@@ -8,9 +8,9 @@ function isConfig(value) {
|
|
|
8
8
|
}
|
|
9
9
|
function mergeWithDefaultMap(map) {
|
|
10
10
|
if (!map) return;
|
|
11
|
-
if (map ===
|
|
11
|
+
if (map === _weapp_core_escape.MappingChars2String) return _weapp_core_escape.MappingChars2String;
|
|
12
12
|
return {
|
|
13
|
-
...
|
|
13
|
+
..._weapp_core_escape.MappingChars2String,
|
|
14
14
|
...map
|
|
15
15
|
};
|
|
16
16
|
}
|
|
@@ -18,7 +18,7 @@ function resolveSharedMap(options) {
|
|
|
18
18
|
const { escape, unescape } = options ?? {};
|
|
19
19
|
if (isConfig(escape) && escape.map) return mergeWithDefaultMap(escape.map);
|
|
20
20
|
if (isConfig(unescape) && unescape.map) return mergeWithDefaultMap(unescape.map);
|
|
21
|
-
return
|
|
21
|
+
return _weapp_core_escape.MappingChars2String;
|
|
22
22
|
}
|
|
23
23
|
function resolveTransformers(options) {
|
|
24
24
|
const sharedMap = resolveSharedMap(options);
|
|
@@ -39,16 +39,27 @@ function resolveTransformers(options) {
|
|
|
39
39
|
};
|
|
40
40
|
else unescapeOptions = { map: sharedMap };
|
|
41
41
|
return {
|
|
42
|
-
escape: escapeEnabled ? (value) => (0,
|
|
43
|
-
unescape: unescapeEnabled ? (value) => (0,
|
|
42
|
+
escape: escapeEnabled ? (value) => (0, _weapp_core_escape.escape)(value, escapeOptions) : identity,
|
|
43
|
+
unescape: unescapeEnabled ? (value) => (0, _weapp_core_escape.unescape)(value, unescapeOptions) : identity
|
|
44
44
|
};
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
//#endregion
|
|
48
48
|
//#region src/create-runtime.ts
|
|
49
|
+
const CACHE_LIMIT = 256;
|
|
50
|
+
function shouldUnescape(value) {
|
|
51
|
+
return value.includes("_") || /u[0-9a-f]{3,}/i.test(value);
|
|
52
|
+
}
|
|
49
53
|
function wrapClassAggregator(fn, transformers, prepareValue, restoreValue) {
|
|
54
|
+
const cache = /* @__PURE__ */ new Map();
|
|
50
55
|
return (...inputs) => {
|
|
51
|
-
|
|
56
|
+
let rawInput;
|
|
57
|
+
if (inputs.length === 1 && typeof inputs[0] === "string") rawInput = inputs[0];
|
|
58
|
+
else rawInput = (0, clsx.clsx)(...inputs);
|
|
59
|
+
if (!rawInput) return rawInput;
|
|
60
|
+
const cached = cache.get(rawInput);
|
|
61
|
+
if (cached !== void 0) return cached;
|
|
62
|
+
const normalized = shouldUnescape(rawInput) ? transformers.unescape(rawInput) : rawInput;
|
|
52
63
|
let metadata;
|
|
53
64
|
let preparedValue = normalized;
|
|
54
65
|
if (prepareValue) {
|
|
@@ -61,7 +72,13 @@ function wrapClassAggregator(fn, transformers, prepareValue, restoreValue) {
|
|
|
61
72
|
}
|
|
62
73
|
const merged = fn(preparedValue);
|
|
63
74
|
const restored = restoreValue ? restoreValue(merged, metadata) : merged;
|
|
64
|
-
|
|
75
|
+
const escaped = transformers.escape(restored);
|
|
76
|
+
if (cache.size >= CACHE_LIMIT) {
|
|
77
|
+
const firstEntry = cache.keys().next();
|
|
78
|
+
if (!firstEntry.done) cache.delete(firstEntry.value);
|
|
79
|
+
}
|
|
80
|
+
cache.set(rawInput, escaped);
|
|
81
|
+
return escaped;
|
|
65
82
|
};
|
|
66
83
|
}
|
|
67
84
|
function wrapFactory(factory, transformers, prepareValue, restoreValue) {
|
|
@@ -98,16 +115,19 @@ function escapeRegexp(value) {
|
|
|
98
115
|
function createRpxLengthTransform(prefixes = DEFAULT_PREFIXES) {
|
|
99
116
|
const arbitraryRegexps = prefixes.map((prefix) => new RegExp(`${prefix}-\\[(\\d+(?:\\.\\d+)?)rpx\\]`, "g"));
|
|
100
117
|
function prepareValue(value) {
|
|
118
|
+
if (!value || !value.includes("rpx")) return value;
|
|
119
|
+
let hasReplacements = false;
|
|
101
120
|
const replacements = {};
|
|
102
121
|
const transformed = arbitraryRegexps.reduce((acc, regex, index) => {
|
|
103
122
|
const prefix = prefixes[index];
|
|
104
123
|
return acc.replace(regex, (_match, amount) => {
|
|
105
124
|
const placeholder = `${prefix}-[length:${amount}rpx]`;
|
|
106
125
|
replacements[placeholder] = (replacements[placeholder] ?? 0) + 1;
|
|
126
|
+
hasReplacements = true;
|
|
107
127
|
return placeholder;
|
|
108
128
|
});
|
|
109
129
|
}, value);
|
|
110
|
-
if (
|
|
130
|
+
if (!hasReplacements) return value;
|
|
111
131
|
return {
|
|
112
132
|
value: transformed,
|
|
113
133
|
metadata: { replacements }
|
package/dist/index.d.cts
CHANGED
|
@@ -2,8 +2,8 @@ import { ClassValue, ClassValue as ClassValue$1, clsx as clsxFn } from "clsx";
|
|
|
2
2
|
import { EscapeOptions, UnescapeOptions } from "@weapp-core/escape";
|
|
3
3
|
|
|
4
4
|
//#region src/types.d.ts
|
|
5
|
-
type EscapeConfig =
|
|
6
|
-
type UnescapeConfig =
|
|
5
|
+
type EscapeConfig = boolean | EscapeOptions;
|
|
6
|
+
type UnescapeConfig = boolean | UnescapeOptions;
|
|
7
7
|
interface CreateOptions {
|
|
8
8
|
escape?: EscapeConfig;
|
|
9
9
|
unescape?: UnescapeConfig;
|
package/dist/index.d.mts
CHANGED
|
@@ -2,8 +2,8 @@ import { ClassValue, ClassValue as ClassValue$1, clsx as clsxFn } from "clsx";
|
|
|
2
2
|
import { EscapeOptions, UnescapeOptions } from "@weapp-core/escape";
|
|
3
3
|
|
|
4
4
|
//#region src/types.d.ts
|
|
5
|
-
type EscapeConfig =
|
|
6
|
-
type UnescapeConfig =
|
|
5
|
+
type EscapeConfig = boolean | EscapeOptions;
|
|
6
|
+
type UnescapeConfig = boolean | UnescapeOptions;
|
|
7
7
|
interface CreateOptions {
|
|
8
8
|
escape?: EscapeConfig;
|
|
9
9
|
unescape?: UnescapeConfig;
|
package/dist/index.mjs
CHANGED
|
@@ -46,9 +46,20 @@ function resolveTransformers(options) {
|
|
|
46
46
|
|
|
47
47
|
//#endregion
|
|
48
48
|
//#region src/create-runtime.ts
|
|
49
|
+
const CACHE_LIMIT = 256;
|
|
50
|
+
function shouldUnescape(value) {
|
|
51
|
+
return value.includes("_") || /u[0-9a-f]{3,}/i.test(value);
|
|
52
|
+
}
|
|
49
53
|
function wrapClassAggregator(fn, transformers, prepareValue, restoreValue) {
|
|
54
|
+
const cache = /* @__PURE__ */ new Map();
|
|
50
55
|
return (...inputs) => {
|
|
51
|
-
|
|
56
|
+
let rawInput;
|
|
57
|
+
if (inputs.length === 1 && typeof inputs[0] === "string") rawInput = inputs[0];
|
|
58
|
+
else rawInput = clsx(...inputs);
|
|
59
|
+
if (!rawInput) return rawInput;
|
|
60
|
+
const cached = cache.get(rawInput);
|
|
61
|
+
if (cached !== void 0) return cached;
|
|
62
|
+
const normalized = shouldUnescape(rawInput) ? transformers.unescape(rawInput) : rawInput;
|
|
52
63
|
let metadata;
|
|
53
64
|
let preparedValue = normalized;
|
|
54
65
|
if (prepareValue) {
|
|
@@ -61,7 +72,13 @@ function wrapClassAggregator(fn, transformers, prepareValue, restoreValue) {
|
|
|
61
72
|
}
|
|
62
73
|
const merged = fn(preparedValue);
|
|
63
74
|
const restored = restoreValue ? restoreValue(merged, metadata) : merged;
|
|
64
|
-
|
|
75
|
+
const escaped = transformers.escape(restored);
|
|
76
|
+
if (cache.size >= CACHE_LIMIT) {
|
|
77
|
+
const firstEntry = cache.keys().next();
|
|
78
|
+
if (!firstEntry.done) cache.delete(firstEntry.value);
|
|
79
|
+
}
|
|
80
|
+
cache.set(rawInput, escaped);
|
|
81
|
+
return escaped;
|
|
65
82
|
};
|
|
66
83
|
}
|
|
67
84
|
function wrapFactory(factory, transformers, prepareValue, restoreValue) {
|
|
@@ -98,16 +115,19 @@ function escapeRegexp(value) {
|
|
|
98
115
|
function createRpxLengthTransform(prefixes = DEFAULT_PREFIXES) {
|
|
99
116
|
const arbitraryRegexps = prefixes.map((prefix) => new RegExp(`${prefix}-\\[(\\d+(?:\\.\\d+)?)rpx\\]`, "g"));
|
|
100
117
|
function prepareValue(value) {
|
|
118
|
+
if (!value || !value.includes("rpx")) return value;
|
|
119
|
+
let hasReplacements = false;
|
|
101
120
|
const replacements = {};
|
|
102
121
|
const transformed = arbitraryRegexps.reduce((acc, regex, index) => {
|
|
103
122
|
const prefix = prefixes[index];
|
|
104
123
|
return acc.replace(regex, (_match, amount) => {
|
|
105
124
|
const placeholder = `${prefix}-[length:${amount}rpx]`;
|
|
106
125
|
replacements[placeholder] = (replacements[placeholder] ?? 0) + 1;
|
|
126
|
+
hasReplacements = true;
|
|
107
127
|
return placeholder;
|
|
108
128
|
});
|
|
109
129
|
}, value);
|
|
110
|
-
if (
|
|
130
|
+
if (!hasReplacements) return value;
|
|
111
131
|
return {
|
|
112
132
|
value: transformed,
|
|
113
133
|
metadata: { replacements }
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weapp-tailwindcss/runtime",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.4",
|
|
5
5
|
"description": "Shared runtime helpers (transformers, clsx wrappers, metadata) for weapp-tailwindcss 在小程序运行时的适配层。",
|
|
6
6
|
"author": "ice breaker <1324318532@qq.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -39,11 +39,14 @@
|
|
|
39
39
|
"files": [
|
|
40
40
|
"dist"
|
|
41
41
|
],
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": "^20.19.0 || >=22.12.0"
|
|
44
|
+
},
|
|
42
45
|
"publishConfig": {
|
|
43
46
|
"access": "public"
|
|
44
47
|
},
|
|
45
48
|
"dependencies": {
|
|
46
|
-
"@weapp-core/escape": "^
|
|
49
|
+
"@weapp-core/escape": "^6.0.1",
|
|
47
50
|
"clsx": "^2.1.1"
|
|
48
51
|
},
|
|
49
52
|
"scripts": {
|