Question regarding Hermes Modules
Problem
Hi, I was looking for bundle splitting options for Hermes. Needed some clarifications around Hermes Modules, and whether I'm correctly realising its purpose.
Will be glad if you can answer these:
- Will the
Modulesconfiguration help me break the Hermes bundle down into a base bundle and subsequent chunks? - Will I be able to lazy load these chunks?
- If yes^, will the lazy loading automatically be handled by the bundler as and when the chunks are required?
Thanks!
What Are Hermes Modules?
The purpose of Hermes Modules is to provide a built-in implementation of CommonJS and (currently only partial) ESM.
Traditionally that is provided by the bundler/packager (like Metro). By comparison Hermes Modules:
- Enable using modules directly without an external packager (superficially similar to NodeJS).
- Provide the Hermes compiler with knowledge about the modules, unlocking more possibilities for optimization.
- Provide an extremely fast implementation of
require(a single VM instruction).
As described in the documentation, Hermes Modules additionally allow splitting the bundle into multiple segments, which have to be described in the metadata. Additional segments must be loaded using the loadSegment() API. Note that the API expects a buffer with the already loaded bytecode (from disk, network, etc) and is not exposed to JavaScript, so it is the integrator's responsibility to expose it or to call it in other ways. The Hermes REPL exposes a version for testing purposes, which can be seen in tests.
Should I Use Hermes Modules?
The short answer in the context of React Native is No. Unfortunately Hermes Modules are not yet integrated with or supported by React Native because React Native's bundler (Metro) doesn't support them yet.
Realistically, to use Hermes Modules one would need either to implement support for them in an existing packager/bundler, or or write a mini-bundler to generate the needed metadata and deal with other details like resources, etc.
Got it, thanks @tmikov for the quick reply. From what I understand, we can segment the Hermes bundle using Hermes Modules. To lazy load them, we can write our own mini bundler and integrate the API you have mentioned.
To be clear, that probably won't work with the existing React Native infrastructure which already transforms require() calls.
I should emphasize that you don't need Hermes Modules in order to split a bundle. Hermes Modules provide integration between the builtin Hermes CommonJS implementation and bundle-splitting. But there is nothing inherently magical or special about splitting a bundle. You can already do that by simply compiling multiple JS files separately and then loading them whenever you want in the same VM instance - it is all about source conventions and integration with your toolchain.
A very simple example, two files main.js and seg1.js:
main.js:
my_log("main running");
if (typeof seg1 === "undefined") {
my_log("seg1 not loaded. Loading");
my_load("seg1");
}
seg1();
seg1.js
function seg1() {
my_log("running seg1");
}
my_log("seg1 loaded");
Compile both of the segments:
hermesc main.js -emit-binary -o main.hbc
hermesc seg1.js -emit-binary -o seg1.hbc
In this example my_log() and my_load() are native function that must be provided and exported by you. my_load() needs to locate seg1.hbc, load it in memory, and load it into the VM using facebook::jsi::Runtime::evaluateJavaScript().
So you can have bundle splitting without Hermes Modules (and with Hermes Modules you still have to provide essentially the same my_load(), the only difference being that it calls loadSegment() instead of evaluateJavaScript().