From b03ed5ae262d78048d6e6c432f9a5cfe2844ed4c Mon Sep 17 00:00:00 2001 From: lete114 Date: Fri, 19 Jun 2026 14:45:47 +0800 Subject: [PATCH] fix(utils): remove #REVERSE static field for better tree-shaking Replace Object.fromEntries-based #REVERSE lookup table with #CHARS.indexOf() to eliminate function calls in class static field initializers. This allows Vite/Rollup to properly tree-shake Base64 when unused. --- packages/utils/src/base64.ts | 9 ++++----- .../tsnapi/@mcbe-mods/utils/index.snapshot.js | 1 - 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/utils/src/base64.ts b/packages/utils/src/base64.ts index bec5498..b06db01 100644 --- a/packages/utils/src/base64.ts +++ b/packages/utils/src/base64.ts @@ -2,7 +2,6 @@ import { utf8Decode, utf8Encode } from './textCodec' export class Base64 { static #CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' - static #REVERSE = Object.fromEntries([...Base64.#CHARS].map((c, i) => [c.charCodeAt(0), i])) static #toBase64(bytes: Uint8Array): string { let result = '' @@ -19,10 +18,10 @@ export class Base64 { const sanitized = input.replace(/[^A-Z0-9+/=]/gi, '') const bytes: number[] = [] for (let i = 0; i < sanitized.length; i += 4) { - const enc1 = Base64.#REVERSE[sanitized[i].charCodeAt(0)] - const enc2 = Base64.#REVERSE[sanitized[i + 1].charCodeAt(0)] - const enc3 = sanitized[i + 2] === '=' ? 0 : Base64.#REVERSE[sanitized[i + 2].charCodeAt(0)] - const enc4 = sanitized[i + 3] === '=' ? 0 : Base64.#REVERSE[sanitized[i + 3].charCodeAt(0)] + const enc1 = Base64.#CHARS.indexOf(sanitized[i]) + const enc2 = Base64.#CHARS.indexOf(sanitized[i + 1]) + const enc3 = sanitized[i + 2] === '=' ? 0 : Base64.#CHARS.indexOf(sanitized[i + 2]) + const enc4 = sanitized[i + 3] === '=' ? 0 : Base64.#CHARS.indexOf(sanitized[i + 3]) const b = (enc1 << 18) | (enc2 << 12) | (enc3 << 6) | enc4 bytes.push(b >> 16 & 0xFF) if (sanitized[i + 2] !== '=') { diff --git a/packages/utils/test/__snapshots__/tsnapi/@mcbe-mods/utils/index.snapshot.js b/packages/utils/test/__snapshots__/tsnapi/@mcbe-mods/utils/index.snapshot.js index 65e8691..b56f1e7 100644 --- a/packages/utils/test/__snapshots__/tsnapi/@mcbe-mods/utils/index.snapshot.js +++ b/packages/utils/test/__snapshots__/tsnapi/@mcbe-mods/utils/index.snapshot.js @@ -4,7 +4,6 @@ // #region Classes export class Base64 { static CHARS - static REVERSE static toBase64(_) {} static fromBase64(_) {} static encode(_) {}