From 67dbd26630f414694174a7cf632b572968ce6787 Mon Sep 17 00:00:00 2001 From: Chris Lorenzo Date: Tue, 4 Aug 2026 20:31:31 -0400 Subject: [PATCH] perf(textures): let textureCompression tree-shake when the flag is off MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ENABLE_COMPRESSED_TEXTURES already gated the compressed *load* path, but two other call sites referenced textureCompression.js unconditionally, so bundlers could never drop the module even with __enableCompressedTextures__ defined as false: - ImageTexture: the Canvas2D fail-fast check called isCompressedTextureContainer() outside any flag guard. - WebGlCtxTexture: the `'mipmaps' in tdata` upload branch called uploadCompressedTexture[type]. Both are now behind ENABLE_COMPRESSED_TEXTURES. With the flag off no TextureData can carry mipmaps (only loadCompressedTexture produces them) and no compressed source can load, so both branches are provably dead. package.json already declares sideEffects: false, so once the constant folds to false Rollup drops the branches and then the whole module. Verified with a Vite lib build importing both files from dist/: flag off 28.57 kB / 0 references, flag on 43.16 kB / 9 references — ~14.6 kB raw (~3.3 kB gzip) removed. Co-Authored-By: Claude Opus 5 --- src/core/renderers/webgl/WebGlCtxTexture.ts | 11 +++++++++-- src/core/textures/ImageTexture.ts | 5 ++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/core/renderers/webgl/WebGlCtxTexture.ts b/src/core/renderers/webgl/WebGlCtxTexture.ts index 65d8b02..3f530d1 100644 --- a/src/core/renderers/webgl/WebGlCtxTexture.ts +++ b/src/core/renderers/webgl/WebGlCtxTexture.ts @@ -6,7 +6,10 @@ import { uploadCompressedTexture } from '../../lib/textureCompression.js'; import { CoreContextTexture } from '../CoreContextTexture.js'; import { isHTMLImageElement } from './internal/RendererUtils.js'; import type { Bound } from '../../lib/utils.js'; -import { isProductionEnvironment } from '../../../utils.js'; +import { + ENABLE_COMPRESSED_TEXTURES, + isProductionEnvironment, +} from '../../../utils.js'; const TRANSPARENT_TEXTURE_DATA = new Uint8Array([0, 0, 0, 0]); @@ -223,7 +226,11 @@ export class WebGlCtxTexture extends CoreContextTexture { TRANSPARENT_TEXTURE_DATA, ); this.setTextureMemUse(TRANSPARENT_TEXTURE_DATA.byteLength); - } else if ('mipmaps' in tdata && tdata.mipmaps) { + } else if ( + ENABLE_COMPRESSED_TEXTURES && + 'mipmaps' in tdata && + tdata.mipmaps + ) { const { mipmaps, type, blockInfo } = tdata; uploadCompressedTexture[type]!(glw, this._nativeCtxTexture, tdata); diff --git a/src/core/textures/ImageTexture.ts b/src/core/textures/ImageTexture.ts index 9caba10..98781b1 100644 --- a/src/core/textures/ImageTexture.ts +++ b/src/core/textures/ImageTexture.ts @@ -276,7 +276,10 @@ export class ImageTexture extends Texture { override async getTextureSource(): Promise { // Compressed textures are not supported by the Canvas2D renderer. // Fail fast here before incurring a network fetch or binary decode. - if (this.txManager.renderer?.mode === 'canvas') { + if ( + ENABLE_COMPRESSED_TEXTURES && + this.txManager.renderer?.mode === 'canvas' + ) { const { src, type } = this.props; if ( type === 'compressed' ||