1
1
# Vanilla JS example
2
2
3
3
> ⚠️ Please, avoid using vanilla JavaScript if possible on Telegram Mini Apps
4
- > platform. It is better to use ES modules at least. [ Learn more ] ( #about-iife ) .
4
+ > platform. It is better to use ES modules at least.
5
5
6
6
This example shows how developer could use Vanilla JavaScript to start developing at
7
7
Telegram Mini Apps platform.
@@ -10,7 +10,7 @@ This template demonstrates how developers can implement an application on the Te
10
10
Mini Apps platform using the following technologies and libraries
11
11
12
12
- [ TON Connect] ( https://docs.ton.org/develop/dapps/ton-connect/overview )
13
- - [ @ telegram-apps SDK] ( https://docs .telegram-mini-apps.com/packages/telegram-apps-sdk )
13
+ - [ Telegram SDK] ( https://core .telegram.org/bots/webapps#initializing-mini-apps )
14
14
15
15
> This boilerplate was created using [ pnpm] ( https://pnpm.io/ ) . Therefore, it is required to use
16
16
> it for this project as well.
@@ -73,15 +73,9 @@ devices in the same network with the current device.
73
73
To view the application, you need to open the ` Local `
74
74
link (` http://localhost:3000 ` in this example) in your browser.
75
75
76
- It is important to note that some libraries in this template, such as ` @telegram-apps/sdk ` , are not
76
+ It is important to note that some libraries in this template, such as Telegram SDK , are not
77
77
intended for use outside of Telegram.
78
78
79
- Nevertheless, they appear to function properly. This is because the ` dist/js/mockEnv.ts ` file, which is
80
- imported in the application's entry point (` dist/index.html ` ), employs the ` mockTelegramEnv ` function
81
- to simulate the Telegram environment. This trick convinces the application that it is running in a
82
- Telegram-based environment. Therefore, be cautious not to use this function in production mode
83
- unless you fully understand its implications.
84
-
85
79
### Run Inside Telegram
86
80
87
81
Although it is possible to run the application outside of Telegram, it is recommended to develop it
@@ -108,69 +102,6 @@ to [@BotFather](https://t.me/botfather). Then, navigate
108
102
to [ https://web.telegram.org/k/ ] ( https://web.telegram.org/k/ ) , find your bot, and launch the
109
103
Telegram Mini App. This approach provides the full development experience.
110
104
111
- ## About IIFE
112
-
113
- ### Dependencies
114
-
115
- Some of the packages use other ` @tma.js ` packages as dependencies. In this case there are 2
116
- ways of importing them:
117
-
118
- 1 . ** By inserting another ` script ` tag which loads the dependency** .
119
- This way makes usage of package with a lot of dependencies almost unreal.
120
- 2 . ** By inlining these packages** .
121
- This way leads to code duplication between several packages using the same package as dependency.
122
-
123
- As you can see, there is no optimal solution between both of them. As the additional problem
124
- developer gets here, is bundler is unable to
125
- use [ tree shaking] ( https://stackoverflow.com/questions/45884414/what-is-tree-shaking-and-why-would-i-need-it ) ,
126
- making browser to load the code not used in the application. Imagine using the only 1 function from
127
- some library like ` lodash ` , but fully load it.
128
-
129
- ### Unknown target
130
-
131
- The other problem developer can face is IIFE packages are built for the specific browser of specific
132
- version. So, the package author does not know which target he should choose as long as he doesn't
133
- know it when creating such package. That's why the the package target should be lowered to support
134
- most part of browsers, but this also make final bunlde bigger.
135
-
136
- ### Conclusion
137
-
138
- Unfortunately, developer is unable to avoid these problems when using IIFE format. This is the
139
- reason why it is recommended to use modern technologies along with ESM format.
140
-
141
- ### When there is no other choice
142
-
143
- First of all, it is required to load the package. Developer could use [ JSDelivr] ( https://www.jsdelivr.com/ )
144
- to do it:
145
-
146
- ``` html
147
-
148
- <head >
149
- <script src =" https://cdn.jsdelivr.net/npm/@telegram-apps/sdk/dist/index.iife.js" ></script >
150
- </head >
151
- ```
152
-
153
- Loaded packages of ` @telegram-apps ` in IIFE format are accessible by path ` window.telegramApps.* ` :
154
-
155
- ``` html
156
-
157
- <head >
158
- <script src =" https://cdn.jsdelivr.net/npm/@telegram-apps/sdk/dist/index.iife.js" ></script >
159
- </head >
160
- <body >
161
- <script >
162
- var sdk = window .telegramApps .sdk ;
163
- console .log (sdk .retrieveLaunchData ());
164
- </script >
165
- </body >
166
- ```
167
-
168
- > ⚠️ In this example we did not specify the exact version of required package. In this case,
169
- > JSDelivr CDN will return the latest version of the package which in some cases may lead to
170
- > unexpected behavior. To prevent such case, specify the exact version.
171
-
172
-
173
-
174
105
## Deploy
175
106
176
107
This boilerplate uses GitHub Pages as the way to host the application externally. GitHub Pages
0 commit comments