mtcute/docs/guide/advanced/treeshaking.md
alina sireneva 05077aa640
Some checks failed
Build and deploy typedoc / build (push) Waiting to run
Build and deploy docs / build (push) Successful in 1m56s
Tests / test-deno (push) Successful in 1m56s
Tests / test-bun (push) Successful in 2m5s
Tests / test-node (node22) (push) Successful in 2m14s
Tests / test-node (node20) (push) Successful in 2m21s
Tests / test-node (node18) (push) Successful in 2m29s
Tests / test-web (chromium) (push) Successful in 2m21s
Tests / test-web (firefox) (push) Successful in 1m33s
Tests / lint (push) Failing after 7m0s
Tests / e2e (push) Has been skipped
Tests / e2e-deno (push) Has been skipped
chore: moved docs inside the main repo
Co-authored-by: Kamilla 'ova <me@kamillaova.dev>
Co-authored-by: Alina Chebakova <chebakov05@gmail.com>
Co-authored-by: Kravets <57632712+kravetsone@users.noreply.github.com>
Co-authored-by: starkow <hello@starkow.dev>
Co-authored-by: sireneva <150665887+sireneva@users.noreply.github.com>
2025-01-17 08:50:23 +03:00

994 B

Tree-shaking

Being a ESM-first library, mtcute supports tree-shaking out of the box.

This means that you can import only the parts of the library that you need, and the bundler will remove all the unused code.

Usage

To start using tree-shaking, there are a few things to keep in mind:

  • Do not use TelegramClient. Use BaseTelegramClient instead, and import the needed methods.

    For example, instead of this:

    import { TelegramClient } from '@mtcute/web'
    
    const tg = new TelegramClient({ ... })
    
    await tg.sendText(...)
    

    you should use this:

    import { BaseTelegramClient } from '@mtcute/web'
    import { sendText } from '@mtcute/web/methods.js'
    
    const tg = new BaseTelegramClient({ ... })
    
    await sendText(tg, ...)
    
  • TL serialization is currently not tree-shakeable, because it is done via a global map of constructors. There's no ETA on when (or whether at all) this will be changed, so there will be ~300 KB of non-shakeable code.