-
Notifications
You must be signed in to change notification settings - Fork 669
feat: add globalPrefix
to remaining require polyfill methods
#1512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
on the other hand, we discussed that it might not be needed to add the prefix to |
cc @robhogan do you think we should create a separate prefix for We've added tests to the PR and marked it as ready to review, let me know if there is anything left to be done here to proceed with this PR 🙏 |
We'll need to update this as well so users with a custom global prefix but default metro/packages/metro-config/src/defaults/index.js Lines 63 to 64 in 35f2e48
|
LGTM in principle, importing to run internal tests |
there is 2 ways to achieve this:
what would you suggest here? |
@robhogan any suggestions on how to move forward with this PR? |
…Statement` Summary: To allow prefixing the global function `__r` (e.g., #1512), expose the prefix to the configured [`getRunModuleStatement`](https://metrobundler.dev/docs/configuration/#getrunmodulestatement), which returns the `__r()` call as a string. ``` - **[Feature]** Expose `globalPrefix` to `getRunModuleStatement` ``` ## Alternative? Runtime prefixing We could alternatively add prefix at runtime by emitting: ``` 'globalThis[__METRO_GLOBAL_PREFIX__ + '__r'](/*...*/)' ``` There are a couple of downsides - - These runModule statements are currently outside IIFEs that inject `global` based on (something like) `global = globalThis ?? global ?? window` - we don't rely on the existence of `globalThis` elsewhere (though it's probably safe to now - that's a separate breaking change). - Concatenation/interpolation + object access is more verbose and slightly slower, and there's just no need for it in code emitted by Metro itself (as opposed to framework/userland code, which must use it). Differential Revision: D81476621
Ideally I think we add the prefix at build time - should be possible to add on top of #1566 |
…Statement` (#1566) Summary: Pull Request resolved: #1566 To allow prefixing the global function `__r` (e.g., #1512), expose the prefix to the configured [`getRunModuleStatement`](https://metrobundler.dev/docs/configuration/#getrunmodulestatement), which returns the `__r()` call as a string. ``` - **[Feature]** Expose `globalPrefix` to `getRunModuleStatement` ``` ## Alternative? Runtime prefixing We could alternatively add prefix at runtime by emitting: ``` "globalThis[__METRO_GLOBAL_PREFIX__ + '__r'](/*...*/)" ``` There are a couple of downsides - - These runModule statements are currently outside IIFEs that inject `global` based on (something like) `global = globalThis ?? global ?? window` - we don't rely on the existence of `globalThis` elsewhere (though it's probably safe to now - that's a separate breaking change). - Concatenation/interpolation + object access is more verbose and slightly slower, and there's just no need for it in code emitted by Metro itself (as opposed to framework/userland code, which must use it). Reviewed By: huntie Differential Revision: D81476621 fbshipit-source-id: 90641cf21491711c56606131ec4c5797b7b00020
1 similar comment
I believe that because of changes in #1566 we should also change the default to include the |
Yes, but tbh I think these need to be the same PR or at least merged simultaneously - users may have already configured (Related, but separately, does anyone need to customise CC @tjzel - IIRC worklets rely on the details here. |
yeah, let's aim to merge those 2 one after another 👍 |
Thanks for the ping. We don't use Just to make sure, after these changes, to obtain the - auto require = rt.global().getPropertyAsFunction(rt, "__r");
+ auto metroGlobalPrefix = rt.global().getProperty(rt, "__METRO_GLOBAL_PREFIX__");
+ auto requireName = metroGlobalPrefix + "__r";
+ auto require = rt.global().getPropertyAsFunction(rt, requireName); |
Correct 👍 I had a quick look but it seems to be one of the only uses of that in OSS 😅 |
Summary
Part of #1480
This PR adds
__METRO_GLOBAL_PREFIX__
to otherrequire
polyfill methods. With Module Federation we use multiple module system in runtime which creates a need for separating module systems from each other withglobalPrefix
.Changelog: [Feature] Add global prefix to other module functions in require polyfill
Test plan
TBD