One of biggest features of Lynx is Dual-Thread Architecture(read Thinking in ReactLynx for more information), JavaScript code runs on two threads: the main thread and the background thread. The two threads use different JavaScript engines as their runtime.
The Lynx main thread is responsible for handling tasks that directly affect the screen pixel-pipeline, including executing main thread scripts, handling layout, and rendering graphics.
The main thread uses PrimJS, maintained by the Lynx team, as its runtime. PrimJS is a lightweight, high-performance JavaScript engine based on QuickJS, providing excellent runtime performance for the main thread.
As opposed to the main thread, Lynx's background threads handle tasks that do not directly affect the display of screen pixels. This includes scripts and tasks that run in the background, separate from the main thread. This allows the main thread to focus on handling user interaction and rendering, which improves overall performance.
While these environments are very similar, you may end up hitting some inconsistencies. It is best to avoid relying on specifics of any runtime.
The Lynx dual-thread runtime supports the following maximum ECMAScript versions:
Of course, you can use new JavaScript syntax to write your code. During the build process, SWC will be used as Syntax Transformer to transform your code, so you don't have to wait for JavaScript runtime support.
Only injects polyfills on iOS.
A full list of Lynx's polyfills can be found in Lynx repository
Besides syntax transformers, many built-in objects and standard functions are also available. Including:
You can use JavaScript module when developing Lynx project.
Lynx currently supports ESModule and CommonJS. The usage of ESModule and CommonJS can be mixed.
It is recommended to use ESModule, which allows for better Tree Shaking.
Both ESModule and CommonJS, a module name needs to be specified. It can be one of:
./common.js
lodash
(C++ addon and NodeJS builtin packages are not supported)@common/foo.js
CommonJS uses require(path)
to import a module. Uses module.exports
or exports
to export a module.
require
can be anywhere in your code, not required to be at top.
require
will synchronously execute the target module.
require
will cache the returned object. Multiple require
to the same path will return the same value. e.g:
ESModule uses import
to import a module. Uses export
to export a module.
import
and export
must be placed at the top level of source file.
ESModule can also use import()
to dynamically import a module.