@weapp-tailwindcss/runtime 0.1.2 → 0.1.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
@@ -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
- const normalized = transformers.unescape((0, clsx.clsx)(...inputs));
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
- return transformers.escape(restored);
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 (Object.keys(replacements).length === 0) return transformed;
130
+ if (!hasReplacements) return value;
111
131
  return {
112
132
  value: transformed,
113
133
  metadata: { replacements }
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
- const normalized = transformers.unescape(clsx(...inputs));
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
- return transformers.escape(restored);
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 (Object.keys(replacements).length === 0) return transformed;
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.2",
4
+ "version": "0.1.3",
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",
@@ -43,7 +43,7 @@
43
43
  "access": "public"
44
44
  },
45
45
  "dependencies": {
46
- "@weapp-core/escape": "^5.0.1",
46
+ "@weapp-core/escape": "^6.0.1",
47
47
  "clsx": "^2.1.1"
48
48
  },
49
49
  "scripts": {