Skip to content

perf(textures): let textureCompression tree-shake when the flag is off - #137

Merged
chiefcll merged 1 commit into
mainfrom
perf/tree-shake-texture-compression
Aug 5, 2026
Merged

perf(textures): let textureCompression tree-shake when the flag is off#137
chiefcll merged 1 commit into
mainfrom
perf/tree-shake-texture-compression

Conversation

@chiefcll

@chiefcll chiefcll commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

What

ENABLE_COMPRESSED_TEXTURES already gated the compressed load path, but two other call sites referenced textureCompression.js unconditionally, so a bundler could never drop the module — even with __enableCompressedTextures__ defined as false.

Both are now behind the flag:

  • ImageTexture.getTextureSource() — the Canvas2D fail-fast check called isCompressedTextureContainer() outside any guard.
  • WebGlCtxTexture.onLoadRequest() — the 'mipmaps' in tdata upload branch called uploadCompressedTexture[type].

Why

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 esbuild folds the constant to false, Rollup drops the branches and then the whole module.

Verification

Real Vite lib build importing both files from dist/:

__enableCompressedTextures__ bundle references to compression symbols
false 28.57 kB 0
true 43.16 kB 9

~14.6 kB raw (~3.3 kB gzip) removed from apps that don't opt in.

pnpm build clean, 360 unit tests pass, prettier/eslint clean on both files.

Reviewer notes

  • Elimination depends on the consumer's bundler constant-folding the flag. A consumer that never defines __enableCompressedTextures__ leaves typeof __enableCompressedTextures__ !== 'undefined' as a runtime check and retains the module — same behavior as USE_RTT and the other flags in src/utils.ts. Compressed textures still correctly no-op at runtime, just without the size win.
  • With the flag off, a type: 'compressed' source in Canvas2D no longer gets the explicit "not supported in Canvas2D render mode" error — it falls through to loadImage and 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

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>
@chiefcll
chiefcll merged commit f3d8a3b into main Aug 5, 2026
3 checks passed
@chiefcll
chiefcll deleted the perf/tree-shake-texture-compression branch August 5, 2026 00:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant