perf(textures): let textureCompression tree-shake when the flag is off - #137
Merged
Conversation
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 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
ENABLE_COMPRESSED_TEXTURESalready gated the compressed load path, but two other call sites referencedtextureCompression.jsunconditionally, so a bundler could never drop the module — even with__enableCompressedTextures__defined asfalse.Both are now behind the flag:
ImageTexture.getTextureSource()— the Canvas2D fail-fast check calledisCompressedTextureContainer()outside any guard.WebGlCtxTexture.onLoadRequest()— the'mipmaps' in tdataupload branch calleduploadCompressedTexture[type].Why
With the flag off, no
TextureDatacan carrymipmaps(onlyloadCompressedTextureproduces them) and no compressed source can load, so both branches are provably dead.package.jsonalready declaressideEffects: false, so once esbuild folds the constant tofalse, Rollup drops the branches and then the whole module.Verification
Real Vite lib build importing both files from
dist/:__enableCompressedTextures__falsetrue~14.6 kB raw (~3.3 kB gzip) removed from apps that don't opt in.
pnpm buildclean, 360 unit tests pass, prettier/eslint clean on both files.Reviewer notes
__enableCompressedTextures__leavestypeof __enableCompressedTextures__ !== 'undefined'as a runtime check and retains the module — same behavior asUSE_RTTand the other flags insrc/utils.ts. Compressed textures still correctly no-op at runtime, just without the size win.type: 'compressed'source in Canvas2D no longer gets the explicit "not supported in Canvas2D render mode" error — it falls through toloadImageand fails there instead. That path was already unreachable-by-design when the flag is off, so this changes the error message, not the outcome.🤖 Generated with Claude Code