jerryscript-project / jerryscript

Ultra-lightweight JavaScript engine for the Internet of Things.
https://jerryscript.net
Apache License 2.0
6.87k stars 666 forks source link

Recommendation to transpile and bundle typescript for running in jerryscript #5094

Open RichardGaleSonos opened 10 months ago

RichardGaleSonos commented 10 months ago

I have a typescript node app with dependencies. I can build with webpack and run via node or execute directly with ts-node, now I would like to run via jerryscript. Are there any examples of transpiling to the appropriate versioned javascript, including dependencies, and creating a bundle?

I'm happy to use esbuild, rollup, weback, etc.

dananderson commented 9 months ago

The babel transpiler might work for your use case. If your final bundle has an export statement, you might have a bad time, as jerryscript does not support newer export syntax. The real problem is that jerryscript is a unique target and babel does not allow you to fully configure the target.

The best bet is esbuild. I set the target to es2022 and disable support for top-level-await and option-chain. There are other feature not supported by jerryscript, but these are the ones I run into the most. esbuild lets you pick and choose feature support, so you can effectively make your own custom target. jerryscript also has some parsing issues here and there, but esbuild coincidentally avoids some of the major ones.

You also have to consider the ECMA builtins you are using. For example, jerryscript is missing FinalizationRegistry, which cannot be polyfilled from js (you need native support).

There are also runtime bugs with private fields, Date and closures capturing this. These are a pain, but it is much worse when a dependency hits one of these bugs.

Finally, unless you have built a ES module system or CommonJS into your jerryscript runtime, the bundle will need to be a single file. You will need to remove/polyfill node dependencies and deal with node globals you use like console.log() or whatever.