Commit 5898c46
committed
Remove ESM generated code
## Details
- This PR basically reverts PR #32 because this is hell. More about this in **Why???** section below.
- Only generate CommonJS code in `lib/` instead of both ESM and CommonJS code.
- Remove Babel stuff and rely on `tsc` to generate CommonJS code and TypeScript declaration files in `lib/`.
## Why???
- Because this is hell. Totally hell.
- Before this PR, rtp.js can not be used in TypeScript projects that use Webpack to generate a bundled JS file. Why? Because Webpack assumes that everything is CommonJS but then it miserably fails to locate/resolve '.cjs' files.
- If we have '.ts' files in `src/` folder, then `tsc` will generate 'js' files rather than '.mjs' files, and that means problems with loaders/bundlers that assume that '.js' files contain CommonJS and then they fail to process them because those '.js' files contain 'import/export' rather than 'require/module.exports'.
- And if we have '.mts' files in `src` then `tsc` will generate `.mjs` with ESM code but it's literally impossible to have another `tsconfig-cjs.json` file to make `tsc` generate CommonJS code.
- So then we moved to Babel to generate ESM and CJS code (see PR #32), and we have to include tricks to replace the extension in the `import` and `require()` calls in the generated ESM and CJS code in `lib/`, which is terrible. But then Babel also generates `.js.map` files that reference a `.mts` file in `lib/` folder which obviously doesn't exist.
- And another problem is that Babel only generates JavaScript code so we still need to use `tsc` to generate TypeScript declaration files and, since we have '.mts' files in `src/`, those declaration files have `d.mts` extension. But they many text editors and TypeScript consumers asusme that declaraion files must be '.d.ts' only. And if we rename their extension then we have also to munge generated `.mjs` (ESM) and `.cjs` files in `lib/` because those files contain special commented mapping lines that reference the declaration file (with '.d.mts').
- And then it also happens that in CommonJS importing a file without extendion is ok, but in ESM you must write `import foo from './foo.ts'` rather than `from './foo'`. So here another needed rewrite in generated `lib/` code.
- etc etc etc, and **trust me**, more etc etc etc. I've tried EVERYTHING, much more than what I've told here.
## Conclusion
- It's impossible to generate ESM and CJS file in a proper way because it entirely affects the way the TypeScript code must be written, and it includes whether to use `.ts` or `.mts` extension in source files, whether to import internal files with or without extension, and `tsconfig.json` doesn't expose options enough to cover all cases we need when generating ESM or CJS code. I've checked them all, and the same with Babel.
- My conclusion is that, if you want to publish a library in ESM format, you only generate ESM code ('.mjs' files) from TypeScript '.mts' files. But you cannot generate **also** CJS files. And the other way around.1 parent 9d94239 commit 5898c46
File tree
71 files changed
+4173
-7445
lines changed- src
- packets
- RTCP
- extendedReports
- RTP
- test
- packets
- RTCP
- RTP
- utils
- utils
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
71 files changed
+4173
-7445
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
81 | | - | |
| 81 | + | |
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
| |||
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
110 | | - | |
| 110 | + | |
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
114 | | - | |
| 114 | + | |
115 | 115 | | |
116 | | - | |
| 116 | + | |
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
120 | | - | |
| 120 | + | |
121 | 121 | | |
122 | 122 | | |
123 | | - | |
124 | | - | |
| 123 | + | |
| 124 | + | |
125 | 125 | | |
126 | 126 | | |
127 | 127 | | |
| |||
165 | 165 | | |
166 | 166 | | |
167 | 167 | | |
168 | | - | |
| 168 | + | |
169 | 169 | | |
170 | | - | |
| 170 | + | |
171 | 171 | | |
172 | 172 | | |
173 | 173 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | | - | |
6 | | - | |
| 4 | + | |
7 | 5 | | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
| 6 | + | |
17 | 7 | | |
18 | | - | |
| 8 | + | |
19 | 9 | | |
20 | 10 | | |
21 | 11 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | 10 | | |
14 | 11 | | |
15 | 12 | | |
| |||
25 | 22 | | |
26 | 23 | | |
27 | 24 | | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | 25 | | |
32 | 26 | | |
33 | 27 | | |
| |||
161 | 155 | | |
162 | 156 | | |
163 | 157 | | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
174 | | - | |
| 158 | + | |
175 | 159 | | |
176 | 160 | | |
177 | | - | |
178 | | - | |
| 161 | + | |
| 162 | + | |
179 | 163 | | |
180 | 164 | | |
181 | 165 | | |
182 | 166 | | |
183 | 167 | | |
184 | 168 | | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | 169 | | |
189 | 170 | | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
| 171 | + | |
208 | 172 | | |
209 | 173 | | |
210 | 174 | | |
| |||
0 commit comments