Conversation
|
Hey @tjzel, do you have an example on how this is used within Reanimated to register virtual modules? In other words, how does the Metro public API looks like for VMs. For context, In Module Federation implementation for Metro we have hacked our own virtual modules implementation but it's very brittle long-term and would love to move to this stable solution 🙌 |
|
Hey @jbroma, sorry I missed your message. The API to use it isn't pretty unfortunately since it's handled during Babel transpilation. You can see it being used on my draft branch: Long story short, we generate the full code of the virtual module and register it in the transpilation process metadata. On the other hand, with current Metro APIs I believe it's quite a challenge to create a convenient API for such a feature. |
Summary
Changelog: [Feature] Virtual Modules handling - modules that are in-memory only.
PR based on rejected
This PR adds an early implementation of virtual modules handling in Metro, modules which exist only in memory and not on the disk.
My motivation for implementing virtual modules is that
react-native-workletswould like to emit extra, encapsulated modules, based on some code, during the Babel transformation of the user's source code.Basic idea
A Babel plugin during transpilation of (physical or virtual) module can register virtual modules via APIs provided by Metro. Registration consists of providing the following data:
The registering module can either depend on the generated virtual module or not.
A module cannot depend on a virtual module it didn't generate - it might not be possible to synchronize the registrations in time. In the future this could be loosened.
Ideally the virtual module would land in the Metro's virtual FS and treated as a regular file for the purpose of getting its contents, calculating SHA etc. However I don't know yet how feasible that is since there's a great deal of asynchronicity between Metro workers and its core components, like the FS.
Implementation
Because of the great amount of decentralization that is present in the Metro bundling system adding this feature seems to somehow add a lot of redundancy. Here are the simplified key points of the implementation:
transformResult.metadata?.metro?.virtualModulesproperty.Test plan
TBA
I got it working properly in Reanimated monorepo.