Visitar URL original
Update esbuild's `__toBinary` helper to use `Uint8Array.fromBase64` if available · Issue #4286 · evanw/esbuild · GitHub
Skip to content

Update esbuild's __toBinary helper to use Uint8Array.fromBase64 if available #4286

Closed

Description

@dragoncoder047

I am implementing something where it would be useful to have a base64-to-uint8array decoder at runtime. Since I'm already loading a few assets with the 'binary' loader, esbuild includes its own fast base64 decoder. However when I try to access it using its name __toBinary esbuild renames it to __toBinary2 because it thinks my __toBinary is something else in the global scope and so it should not clobber that. I could just paste esbuild's implementation into my own files, but esbuild doesn't (can't) remove the second copy which just seems a little dumb. Is there any way to allow module code to reference the same functions that esbuild has when the same thing needs to be done? If not, can one be added?

__toBinary getting renamed:
https://esbuild.github.io/try/#YgAwLjI1LjkALS1sb2FkZXI6LmJpbj1iaW5hcnkgLS1idW5kbGUgLS1mb3JtYXQ9ZXNtAGUAZW50cnkuanMAaW1wb3J0IHggZnJvbSAiLi9mb28uYmluIjsKY29uc29sZS5sb2coX190b0JpbmFyeSgiYWFhYSIpKTsKY29uc29sZS5sb2coeCk7AABmb28uYmluAGFhYWE

__toBinary included twice:
https://esbuild.github.io/try/#YgAwLjI1LjkALS1sb2FkZXI6LmJpbj1iaW5hcnkgLS1idW5kbGUgLS1mb3JtYXQ9ZXNtAGUAZW50cnkuanMAaW1wb3J0IHggZnJvbSAiLi9mb28uYmluIjsKaW1wb3J0IF9fdG9CaW5hcnkgZnJvbSAiLi90b0JpbmFyeS5qcyIKY29uc29sZS5sb2coX190b0JpbmFyeSgiYWFhYSIpKTsKY29uc29sZS5sb2coeCk7AABmb28uYmluAGFhYWEAAHRvQmluYXJ5LmpzAHZhciB0YWJsZSA9IG5ldyBVaW50OEFycmF5KDEyOCk7CmZvciAodmFyIGkgPSAwOyBpIDwgNjQ7IGkrKykgdGFibGVbaSA8IDI2ID8gaSArIDY1IDogaSA8IDUyID8gaSArIDcxIDogaSA8IDYyID8gaSAtIDQgOiBpICogNCAtIDIwNV0gPSBpOwoKZXhwb3J0IGRlZmF1bHQgKGJhc2U2NCkgPT4gewogICAgdmFyIG4gPSBiYXNlNjQubGVuZ3RoLCBieXRlcyA9IG5ldyBVaW50OEFycmF5KChuIC0gKGJhc2U2NFtuIC0gMV0gPT0gIj0iKSAtIChiYXNlNjRbbiAtIDJdID09ICI9IikpICogMyAvIDQgfCAwKTsKICAgIGZvciAodmFyIGkyID0gMCwgaiA9IDA7IGkyIDwgbjsgKSB7CiAgICAgICAgdmFyIGMwID0gdGFibGVbYmFzZTY0LmNoYXJDb2RlQXQoaTIrKyldLCBjMSA9IHRhYmxlW2Jhc2U2NC5jaGFyQ29kZUF0KGkyKyspXTsKICAgICAgICB2YXIgYzIgPSB0YWJsZVtiYXNlNjQuY2hhckNvZGVBdChpMisrKV0sIGMzID0gdGFibGVbYmFzZTY0LmNoYXJDb2RlQXQoaTIrKyldOwogICAgICAgIGJ5dGVzW2orK10gPSBjMCA8PCAyIHwgYzEgPj4gNDsKICAgICAgICBieXRlc1tqKytdID0gYzEgPDwgNCB8IGMyID4+IDI7CiAgICAgICAgYnl0ZXNbaisrXSA9IGMyIDw8IDYgfCBjMzsKICAgIH0KICAgIHJldHVybiBieXRlczsKfTs

Activity

  1. evanw commented on Sep 16, 2025

    @evanw
    Owner

    This is being renamed because it's an internal implementation detail of esbuild and is supposed to not collide with user code. As such, it's not something that I'm inclined to expose even if it would be useful. This is because esbuild may change it in the future and because the purpose of esbuild isn't to be a helper library of useful functions (and it would be strange to add a special case for this single function).

    Along those lines, I should probably consider removing this helper function altogether. At least when targeting a modern browser. This functionality has since been added to the browser itself: Uint8Array.fromBase64(). I recommend using Uint8Array.fromBase64 or (an equivalent polyfill) instead of trying to use esbuild's internal API.

  2. dragoncoder047 commented on Sep 16, 2025

    @dragoncoder047
    Author

    This is being renamed because it's an internal implementation detail of esbuild and is supposed to not collide with user code.

    I expected that. (in hindsight I was just checking to make sure esbuild wasn't broken!)

    This functionality has since been added to the browser itself: Uint8Array.fromBase64()

    Oh I didn't know about Uint8Array.fromBase64. Thanks!!!

  3. added a commit that references this issue on Sep 16, 2025
    73a0b2a
  4. evanw commented on Sep 17, 2025

    @evanw
    Owner

    Reopening this to track esbuild switching to Uint8Array.fromBase64. I think that should probably be considered a breaking change as it may come up in certain workflows (e.g. using esbuild to bundle for an older version of node without using something like --target=node22).

  5. reopened this on Sep 17, 2025
  6. changed the title [-]feat: let modules access the same __toBinary that esbuild uses[/-] [+]Update esbuild's `__toBinary` helper to use `Uint8Array.fromBase64` if available[/+] on Sep 17, 2025
  7. added a commit that references this issue on Oct 5, 2025
    64d9633
  8. added a commit that references this issue on Nov 9, 2025
    536b70c
  9. added a commit that references this issue on Nov 9, 2025
    22b425c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions