jerryscript icon indicating copy to clipboard operation
jerryscript copied to clipboard

Recommendation to transpile and bundle typescript for running in jerryscript

Open RichardGaleSonos opened this issue 1 year ago • 1 comments

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.

RichardGaleSonos avatar Aug 24 '23 18:08 RichardGaleSonos

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.

dananderson avatar Sep 15 '23 18:09 dananderson