@weapp-tailwindcss/runtime 0.1.8 → 0.1.10
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 +45 -12
- package/dist/index.d.cts +11 -6
- package/dist/index.d.mts +11 -6
- package/dist/index.mjs +45 -13
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1,8 +1,22 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
let clsx = require("clsx");
|
|
3
3
|
let _weapp_core_escape = require("@weapp-core/escape");
|
|
4
|
+
//#region src/transformer-probes.ts
|
|
5
|
+
const probes = /* @__PURE__ */ new WeakMap();
|
|
6
|
+
const unescapePattern = /u[0-9a-f]{3,}/i;
|
|
7
|
+
const escapeNeedles = Object.keys(_weapp_core_escape.MappingChars2String).filter(Boolean);
|
|
8
|
+
/** 仅为已知默认转换器登记可证明安全的跳过条件,不改变公开类型。 */
|
|
9
|
+
function registerTransformProbe(transform, direction) {
|
|
10
|
+
probes.set(transform, direction === "identity" ? () => false : direction === "unescape" ? (value) => value.includes("_") || unescapePattern.test(value) : (value) => escapeNeedles.some((needle) => value.includes(needle)));
|
|
11
|
+
}
|
|
12
|
+
/** 自定义转换器没有探针时必须执行,不能用默认映射推断其行为。 */
|
|
13
|
+
function needsTransform(transform, value) {
|
|
14
|
+
return probes.get(transform)?.(value) ?? true;
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
4
17
|
//#region src/transformers.ts
|
|
5
18
|
const identity = (value) => value;
|
|
19
|
+
registerTransformProbe(identity, "identity");
|
|
6
20
|
function isConfig(value) {
|
|
7
21
|
return typeof value === "object" && value !== null;
|
|
8
22
|
}
|
|
@@ -42,19 +56,33 @@ function resolveTransformers(options) {
|
|
|
42
56
|
};
|
|
43
57
|
else unescapeOptions = { map: sharedMap };
|
|
44
58
|
}
|
|
59
|
+
const escapeFn = escapeEnabled ? (value) => (0, _weapp_core_escape.escape)(value, escapeOptions) : identity;
|
|
60
|
+
const unescapeFn = unescapeEnabled ? (value) => (0, _weapp_core_escape.unescape)(value, unescapeOptions) : identity;
|
|
61
|
+
if (sharedMap === _weapp_core_escape.MappingChars2String) {
|
|
62
|
+
if (escapeEnabled && !isConfig(escapeConfig)) registerTransformProbe(escapeFn, "escape");
|
|
63
|
+
if (unescapeEnabled && !isConfig(unescapeConfig)) registerTransformProbe(unescapeFn, "unescape");
|
|
64
|
+
}
|
|
45
65
|
return {
|
|
46
|
-
escape:
|
|
47
|
-
unescape:
|
|
66
|
+
escape: escapeFn,
|
|
67
|
+
unescape: unescapeFn
|
|
48
68
|
};
|
|
49
69
|
}
|
|
50
70
|
//#endregion
|
|
51
71
|
//#region src/create-runtime.ts
|
|
52
72
|
const CACHE_LIMIT = 256;
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
73
|
+
function hasWhitespace(value) {
|
|
74
|
+
return value.includes(" ") || value.includes(" ") || value.includes("\n") || value.includes("\r") || value.includes("\f") || value.includes("\v");
|
|
75
|
+
}
|
|
76
|
+
function transformTokens(value, transformFn) {
|
|
77
|
+
if (!value) return value;
|
|
78
|
+
if (!hasWhitespace(value)) return transformFn(value);
|
|
79
|
+
return value.split(/\s+/).filter(Boolean).map(transformFn).join(" ");
|
|
56
80
|
}
|
|
57
|
-
|
|
81
|
+
/**
|
|
82
|
+
* 为任意 class 聚合函数套上 unescape / 可选 prepare-restore / escape 与有界 LRU。
|
|
83
|
+
* 命中缓存时直接返回已转义结果,避免重复跑引擎。
|
|
84
|
+
*/
|
|
85
|
+
function wrapRuntimeAggregator(fn, transformers, prepareValue, restoreValue) {
|
|
58
86
|
const cache = /* @__PURE__ */ new Map();
|
|
59
87
|
return (...inputs) => {
|
|
60
88
|
let rawInput;
|
|
@@ -62,8 +90,12 @@ function wrapClassAggregator(fn, transformers, prepareValue, restoreValue) {
|
|
|
62
90
|
else rawInput = (0, clsx.clsx)(...inputs);
|
|
63
91
|
if (!rawInput) return rawInput;
|
|
64
92
|
const cached = cache.get(rawInput);
|
|
65
|
-
if (cached !== void 0)
|
|
66
|
-
|
|
93
|
+
if (cached !== void 0) {
|
|
94
|
+
cache.delete(rawInput);
|
|
95
|
+
cache.set(rawInput, cached);
|
|
96
|
+
return cached;
|
|
97
|
+
}
|
|
98
|
+
const normalized = needsTransform(transformers.unescape, rawInput) ? transformTokens(rawInput, transformers.unescape) : rawInput;
|
|
67
99
|
let metadata;
|
|
68
100
|
let preparedValue = normalized;
|
|
69
101
|
if (prepareValue) {
|
|
@@ -76,7 +108,7 @@ function wrapClassAggregator(fn, transformers, prepareValue, restoreValue) {
|
|
|
76
108
|
}
|
|
77
109
|
const merged = fn(preparedValue);
|
|
78
110
|
const restored = restoreValue ? restoreValue(merged, metadata) : merged;
|
|
79
|
-
const escaped = transformers.escape(restored);
|
|
111
|
+
const escaped = needsTransform(transformers.escape, restored) ? transformTokens(restored, transformers.escape) : restored;
|
|
80
112
|
if (cache.size >= CACHE_LIMIT) {
|
|
81
113
|
const firstEntry = cache.keys().next();
|
|
82
114
|
if (!firstEntry.done) cache.delete(firstEntry.value);
|
|
@@ -87,15 +119,15 @@ function wrapClassAggregator(fn, transformers, prepareValue, restoreValue) {
|
|
|
87
119
|
}
|
|
88
120
|
function wrapFactory(factory, transformers, prepareValue, restoreValue) {
|
|
89
121
|
return (...args) => {
|
|
90
|
-
return
|
|
122
|
+
return wrapRuntimeAggregator(factory(...args), transformers, prepareValue, restoreValue);
|
|
91
123
|
};
|
|
92
124
|
}
|
|
93
125
|
function createRuntimeFactory(options) {
|
|
94
126
|
const { version, twMerge: twMergeImpl, twJoin: twJoinImpl, extendTailwindMerge: extendFactory, createTailwindMerge: createFactory, prepareValue, restoreValue } = options;
|
|
95
127
|
return function createRuntime(createOptions) {
|
|
96
128
|
const transformers = resolveTransformers(createOptions);
|
|
97
|
-
const twMerge =
|
|
98
|
-
const twJoin =
|
|
129
|
+
const twMerge = wrapRuntimeAggregator(twMergeImpl, transformers, prepareValue, restoreValue);
|
|
130
|
+
const twJoin = wrapRuntimeAggregator(twJoinImpl, transformers, prepareValue, restoreValue);
|
|
99
131
|
const extendTailwindMerge = wrapFactory(extendFactory, transformers, prepareValue, restoreValue);
|
|
100
132
|
const createTailwindMerge = wrapFactory(createFactory, transformers, prepareValue, restoreValue);
|
|
101
133
|
return {
|
|
@@ -171,3 +203,4 @@ exports.createRuntimeFactory = createRuntimeFactory;
|
|
|
171
203
|
exports.identity = identity;
|
|
172
204
|
exports.resolveTransformers = resolveTransformers;
|
|
173
205
|
exports.weappTwIgnore = weappTwIgnore;
|
|
206
|
+
exports.wrapRuntimeAggregator = wrapRuntimeAggregator;
|
package/dist/index.d.cts
CHANGED
|
@@ -37,7 +37,12 @@ interface CreateRuntimeFactoryOptions<TTwMerge extends TailwindMergeLibraryFn, T
|
|
|
37
37
|
*/
|
|
38
38
|
restoreValue?: (value: string, metadata?: unknown) => string;
|
|
39
39
|
}
|
|
40
|
-
|
|
40
|
+
/**
|
|
41
|
+
* 为任意 class 聚合函数套上 unescape / 可选 prepare-restore / escape 与有界 LRU。
|
|
42
|
+
* 命中缓存时直接返回已转义结果,避免重复跑引擎。
|
|
43
|
+
*/
|
|
44
|
+
export declare function wrapRuntimeAggregator(fn: TailwindMergeLibraryFn, transformers: Transformers, prepareValue?: (value: string) => string | TransformResult, restoreValue?: (value: string, metadata?: unknown) => string): TailwindMergeRuntime;
|
|
45
|
+
export declare function createRuntimeFactory<TTwMerge extends TailwindMergeLibraryFn, TTwJoin extends TailwindMergeLibraryFn, TExtendFactory extends TailwindMergeFactoryFn, TCreateFactory extends TailwindMergeFactoryFn>(options: CreateRuntimeFactoryOptions<TTwMerge, TTwJoin, TExtendFactory, TCreateFactory>): (createOptions?: CreateOptions) => {
|
|
41
46
|
version: TailwindMergeVersion;
|
|
42
47
|
twMerge: TailwindMergeRuntime;
|
|
43
48
|
twJoin: TailwindMergeRuntime;
|
|
@@ -47,7 +52,7 @@ declare function createRuntimeFactory<TTwMerge extends TailwindMergeLibraryFn, T
|
|
|
47
52
|
//#endregion
|
|
48
53
|
//#region src/rpx-length.d.ts
|
|
49
54
|
type ReplacementCounters = Record<string, number>;
|
|
50
|
-
declare function createRpxLengthTransform(prefixes?: readonly ["text", "border", "bg", "outline", "ring"]): {
|
|
55
|
+
export declare function createRpxLengthTransform(prefixes?: readonly ["text", "border", "bg", "outline", "ring"]): {
|
|
51
56
|
prepareValue: (value: string) => string | {
|
|
52
57
|
value: string;
|
|
53
58
|
metadata: {
|
|
@@ -59,12 +64,12 @@ declare function createRpxLengthTransform(prefixes?: readonly ["text", "border",
|
|
|
59
64
|
//#endregion
|
|
60
65
|
//#region src/transformers.d.ts
|
|
61
66
|
declare const identity: (value: string) => string;
|
|
62
|
-
declare function resolveTransformers(options?: CreateOptions): Transformers;
|
|
67
|
+
export declare function resolveTransformers(options?: CreateOptions): Transformers;
|
|
63
68
|
//#endregion
|
|
64
69
|
//#region src/index.d.ts
|
|
65
|
-
declare const weappTwIgnore: (template: {
|
|
70
|
+
export declare const weappTwIgnore: (template: {
|
|
66
71
|
raw: readonly string[] | ArrayLike<string>;
|
|
67
72
|
}, ...substitutions: any[]) => string;
|
|
68
|
-
type ClsxFn = typeof clsxFn;
|
|
73
|
+
export type ClsxFn = typeof clsxFn;
|
|
69
74
|
//#endregion
|
|
70
|
-
export { type ClassValue,
|
|
75
|
+
export { type ClassValue, type CreateOptions, type EscapeConfig, type TailwindMergeFactory, type TailwindMergeLibraryFn, type TailwindMergeRuntime, type TailwindMergeVersion, type Transformers, type UnescapeConfig, clsxFn as clsx, identity };
|
package/dist/index.d.mts
CHANGED
|
@@ -37,7 +37,12 @@ interface CreateRuntimeFactoryOptions<TTwMerge extends TailwindMergeLibraryFn, T
|
|
|
37
37
|
*/
|
|
38
38
|
restoreValue?: (value: string, metadata?: unknown) => string;
|
|
39
39
|
}
|
|
40
|
-
|
|
40
|
+
/**
|
|
41
|
+
* 为任意 class 聚合函数套上 unescape / 可选 prepare-restore / escape 与有界 LRU。
|
|
42
|
+
* 命中缓存时直接返回已转义结果,避免重复跑引擎。
|
|
43
|
+
*/
|
|
44
|
+
export declare function wrapRuntimeAggregator(fn: TailwindMergeLibraryFn, transformers: Transformers, prepareValue?: (value: string) => string | TransformResult, restoreValue?: (value: string, metadata?: unknown) => string): TailwindMergeRuntime;
|
|
45
|
+
export declare function createRuntimeFactory<TTwMerge extends TailwindMergeLibraryFn, TTwJoin extends TailwindMergeLibraryFn, TExtendFactory extends TailwindMergeFactoryFn, TCreateFactory extends TailwindMergeFactoryFn>(options: CreateRuntimeFactoryOptions<TTwMerge, TTwJoin, TExtendFactory, TCreateFactory>): (createOptions?: CreateOptions) => {
|
|
41
46
|
version: TailwindMergeVersion;
|
|
42
47
|
twMerge: TailwindMergeRuntime;
|
|
43
48
|
twJoin: TailwindMergeRuntime;
|
|
@@ -47,7 +52,7 @@ declare function createRuntimeFactory<TTwMerge extends TailwindMergeLibraryFn, T
|
|
|
47
52
|
//#endregion
|
|
48
53
|
//#region src/rpx-length.d.ts
|
|
49
54
|
type ReplacementCounters = Record<string, number>;
|
|
50
|
-
declare function createRpxLengthTransform(prefixes?: readonly ["text", "border", "bg", "outline", "ring"]): {
|
|
55
|
+
export declare function createRpxLengthTransform(prefixes?: readonly ["text", "border", "bg", "outline", "ring"]): {
|
|
51
56
|
prepareValue: (value: string) => string | {
|
|
52
57
|
value: string;
|
|
53
58
|
metadata: {
|
|
@@ -59,12 +64,12 @@ declare function createRpxLengthTransform(prefixes?: readonly ["text", "border",
|
|
|
59
64
|
//#endregion
|
|
60
65
|
//#region src/transformers.d.ts
|
|
61
66
|
declare const identity: (value: string) => string;
|
|
62
|
-
declare function resolveTransformers(options?: CreateOptions): Transformers;
|
|
67
|
+
export declare function resolveTransformers(options?: CreateOptions): Transformers;
|
|
63
68
|
//#endregion
|
|
64
69
|
//#region src/index.d.ts
|
|
65
|
-
declare const weappTwIgnore: (template: {
|
|
70
|
+
export declare const weappTwIgnore: (template: {
|
|
66
71
|
raw: readonly string[] | ArrayLike<string>;
|
|
67
72
|
}, ...substitutions: any[]) => string;
|
|
68
|
-
type ClsxFn = typeof clsxFn;
|
|
73
|
+
export type ClsxFn = typeof clsxFn;
|
|
69
74
|
//#endregion
|
|
70
|
-
export { type ClassValue,
|
|
75
|
+
export { type ClassValue, type CreateOptions, type EscapeConfig, type TailwindMergeFactory, type TailwindMergeLibraryFn, type TailwindMergeRuntime, type TailwindMergeVersion, type Transformers, type UnescapeConfig, clsxFn as clsx, identity };
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,21 @@
|
|
|
1
1
|
import { clsx, clsx as clsxFn } from "clsx";
|
|
2
2
|
import { MappingChars2String, escape, unescape } from "@weapp-core/escape";
|
|
3
|
+
//#region src/transformer-probes.ts
|
|
4
|
+
const probes = /* @__PURE__ */ new WeakMap();
|
|
5
|
+
const unescapePattern = /u[0-9a-f]{3,}/i;
|
|
6
|
+
const escapeNeedles = Object.keys(MappingChars2String).filter(Boolean);
|
|
7
|
+
/** 仅为已知默认转换器登记可证明安全的跳过条件,不改变公开类型。 */
|
|
8
|
+
function registerTransformProbe(transform, direction) {
|
|
9
|
+
probes.set(transform, direction === "identity" ? () => false : direction === "unescape" ? (value) => value.includes("_") || unescapePattern.test(value) : (value) => escapeNeedles.some((needle) => value.includes(needle)));
|
|
10
|
+
}
|
|
11
|
+
/** 自定义转换器没有探针时必须执行,不能用默认映射推断其行为。 */
|
|
12
|
+
function needsTransform(transform, value) {
|
|
13
|
+
return probes.get(transform)?.(value) ?? true;
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
3
16
|
//#region src/transformers.ts
|
|
4
17
|
const identity = (value) => value;
|
|
18
|
+
registerTransformProbe(identity, "identity");
|
|
5
19
|
function isConfig(value) {
|
|
6
20
|
return typeof value === "object" && value !== null;
|
|
7
21
|
}
|
|
@@ -41,19 +55,33 @@ function resolveTransformers(options) {
|
|
|
41
55
|
};
|
|
42
56
|
else unescapeOptions = { map: sharedMap };
|
|
43
57
|
}
|
|
58
|
+
const escapeFn = escapeEnabled ? (value) => escape(value, escapeOptions) : identity;
|
|
59
|
+
const unescapeFn = unescapeEnabled ? (value) => unescape(value, unescapeOptions) : identity;
|
|
60
|
+
if (sharedMap === MappingChars2String) {
|
|
61
|
+
if (escapeEnabled && !isConfig(escapeConfig)) registerTransformProbe(escapeFn, "escape");
|
|
62
|
+
if (unescapeEnabled && !isConfig(unescapeConfig)) registerTransformProbe(unescapeFn, "unescape");
|
|
63
|
+
}
|
|
44
64
|
return {
|
|
45
|
-
escape:
|
|
46
|
-
unescape:
|
|
65
|
+
escape: escapeFn,
|
|
66
|
+
unescape: unescapeFn
|
|
47
67
|
};
|
|
48
68
|
}
|
|
49
69
|
//#endregion
|
|
50
70
|
//#region src/create-runtime.ts
|
|
51
71
|
const CACHE_LIMIT = 256;
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
72
|
+
function hasWhitespace(value) {
|
|
73
|
+
return value.includes(" ") || value.includes(" ") || value.includes("\n") || value.includes("\r") || value.includes("\f") || value.includes("\v");
|
|
74
|
+
}
|
|
75
|
+
function transformTokens(value, transformFn) {
|
|
76
|
+
if (!value) return value;
|
|
77
|
+
if (!hasWhitespace(value)) return transformFn(value);
|
|
78
|
+
return value.split(/\s+/).filter(Boolean).map(transformFn).join(" ");
|
|
55
79
|
}
|
|
56
|
-
|
|
80
|
+
/**
|
|
81
|
+
* 为任意 class 聚合函数套上 unescape / 可选 prepare-restore / escape 与有界 LRU。
|
|
82
|
+
* 命中缓存时直接返回已转义结果,避免重复跑引擎。
|
|
83
|
+
*/
|
|
84
|
+
function wrapRuntimeAggregator(fn, transformers, prepareValue, restoreValue) {
|
|
57
85
|
const cache = /* @__PURE__ */ new Map();
|
|
58
86
|
return (...inputs) => {
|
|
59
87
|
let rawInput;
|
|
@@ -61,8 +89,12 @@ function wrapClassAggregator(fn, transformers, prepareValue, restoreValue) {
|
|
|
61
89
|
else rawInput = clsx(...inputs);
|
|
62
90
|
if (!rawInput) return rawInput;
|
|
63
91
|
const cached = cache.get(rawInput);
|
|
64
|
-
if (cached !== void 0)
|
|
65
|
-
|
|
92
|
+
if (cached !== void 0) {
|
|
93
|
+
cache.delete(rawInput);
|
|
94
|
+
cache.set(rawInput, cached);
|
|
95
|
+
return cached;
|
|
96
|
+
}
|
|
97
|
+
const normalized = needsTransform(transformers.unescape, rawInput) ? transformTokens(rawInput, transformers.unescape) : rawInput;
|
|
66
98
|
let metadata;
|
|
67
99
|
let preparedValue = normalized;
|
|
68
100
|
if (prepareValue) {
|
|
@@ -75,7 +107,7 @@ function wrapClassAggregator(fn, transformers, prepareValue, restoreValue) {
|
|
|
75
107
|
}
|
|
76
108
|
const merged = fn(preparedValue);
|
|
77
109
|
const restored = restoreValue ? restoreValue(merged, metadata) : merged;
|
|
78
|
-
const escaped = transformers.escape(restored);
|
|
110
|
+
const escaped = needsTransform(transformers.escape, restored) ? transformTokens(restored, transformers.escape) : restored;
|
|
79
111
|
if (cache.size >= CACHE_LIMIT) {
|
|
80
112
|
const firstEntry = cache.keys().next();
|
|
81
113
|
if (!firstEntry.done) cache.delete(firstEntry.value);
|
|
@@ -86,15 +118,15 @@ function wrapClassAggregator(fn, transformers, prepareValue, restoreValue) {
|
|
|
86
118
|
}
|
|
87
119
|
function wrapFactory(factory, transformers, prepareValue, restoreValue) {
|
|
88
120
|
return (...args) => {
|
|
89
|
-
return
|
|
121
|
+
return wrapRuntimeAggregator(factory(...args), transformers, prepareValue, restoreValue);
|
|
90
122
|
};
|
|
91
123
|
}
|
|
92
124
|
function createRuntimeFactory(options) {
|
|
93
125
|
const { version, twMerge: twMergeImpl, twJoin: twJoinImpl, extendTailwindMerge: extendFactory, createTailwindMerge: createFactory, prepareValue, restoreValue } = options;
|
|
94
126
|
return function createRuntime(createOptions) {
|
|
95
127
|
const transformers = resolveTransformers(createOptions);
|
|
96
|
-
const twMerge =
|
|
97
|
-
const twJoin =
|
|
128
|
+
const twMerge = wrapRuntimeAggregator(twMergeImpl, transformers, prepareValue, restoreValue);
|
|
129
|
+
const twJoin = wrapRuntimeAggregator(twJoinImpl, transformers, prepareValue, restoreValue);
|
|
98
130
|
const extendTailwindMerge = wrapFactory(extendFactory, transformers, prepareValue, restoreValue);
|
|
99
131
|
const createTailwindMerge = wrapFactory(createFactory, transformers, prepareValue, restoreValue);
|
|
100
132
|
return {
|
|
@@ -164,4 +196,4 @@ function createRpxLengthTransform(prefixes = DEFAULT_PREFIXES) {
|
|
|
164
196
|
}
|
|
165
197
|
const weappTwIgnore = String.raw;
|
|
166
198
|
//#endregion
|
|
167
|
-
export { clsxFn as clsx, createRpxLengthTransform, createRuntimeFactory, identity, resolveTransformers, weappTwIgnore };
|
|
199
|
+
export { clsxFn as clsx, createRpxLengthTransform, createRuntimeFactory, identity, resolveTransformers, weappTwIgnore, wrapRuntimeAggregator };
|
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.10",
|
|
5
5
|
"description": "Shared runtime adapters for cross-platform Tailwind CSS class utilities. 跨端 Tailwind CSS 类名工具的共享运行时适配层。",
|
|
6
6
|
"author": "ice breaker <1324318532@qq.com>",
|
|
7
7
|
"license": "MIT",
|