Skip to content

Commit de65730

Browse files
committed
fix(ac): compact dynamic group parsing and bump size limit
The backward-compatible dynamic group-length parsing in ac() adds ~18B over the previous single hardcoded slice (master was 328B, near the 332B ceiling). Compact the loop to keep the increase minimal (346B) and bump the ac size limit to 355B to accommodate the genuine capability of supporting both the legacy 4-char and collision-resistant 6-char group formats. ac is an opt-in runtime (only imported when classNameCompressionMap is used).
1 parent 2ecf545 commit de65730

2 files changed

Lines changed: 14 additions & 24 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@
151151
},
152152
{
153153
"path": "./packages/react/dist/browser/runtime/ac.js",
154-
"limit": "332B",
154+
"limit": "355B",
155155
"import": "ac, { clearCache }"
156156
},
157157
{

packages/react/src/runtime/ac.ts

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -81,31 +81,21 @@ export function ac(
8181

8282
for (let x = 0; x < groups.length; x++) {
8383
const atomic = groups[x];
84+
// For an atomic class (`_`-prefixed), a second underscore marks the end of
85+
// a compressed class's group (`_<group>_<name>`); `sep` is its index, or -1
86+
// when uncompressed, in which case the group is everything except the
87+
// fixed-width value hash (matching `ax()`). Non-atomic classes use -1 too
88+
// and fall through to being kept whole via the value check below.
89+
// Length-agnostic: legacy 4-char and new 6-char groups both work.
8490
const isAtomic = atomic.charCodeAt(0) === UNDERSCORE_UNICODE;
85-
86-
// A compressed atomic class has the form `_<group>_<compressedName>`, i.e.
87-
// a second underscore after the leading one marks the end of the group.
88-
// We locate it dynamically so both the legacy 4-char group and the
89-
// collision-resistant 6-char group are parsed correctly (the group hash
90-
// itself never contains an underscore).
91-
const compressedSeparatorIndex = isAtomic ? atomic.indexOf('_', 1) : -1;
92-
const isCompressed = compressedSeparatorIndex !== -1;
93-
94-
let atomicGroupName: string;
95-
if (isCompressed) {
96-
// Group is everything up to the separating underscore.
97-
atomicGroupName = atomic.slice(0, compressedSeparatorIndex);
98-
} else if (isAtomic) {
99-
// Uncompressed atomic class: the group is everything except the fixed
100-
// width value hash — length-agnostic, matching `ax()`.
101-
atomicGroupName = atomic.slice(0, atomic.length - ATOMIC_VALUE_HASH_LENGTH);
102-
} else {
103-
atomicGroupName = atomic;
104-
}
105-
91+
const sep = isAtomic ? atomic.indexOf('_', 1) : 0;
10692
atomicGroups.set(
107-
atomicGroupName,
108-
isCompressed ? atomic.slice(compressedSeparatorIndex + 1) : atomic
93+
sep > 0
94+
? atomic.slice(0, sep)
95+
: isAtomic
96+
? atomic.slice(0, -ATOMIC_VALUE_HASH_LENGTH)
97+
: atomic,
98+
sep > 0 ? atomic.slice(sep + 1) : atomic
10999
);
110100
}
111101
} else {

0 commit comments

Comments
 (0)