Skip to content

Pass globalPrefix through to serializer and expose to getRunModuleStatement#1566

Closed
robhogan wants to merge 1 commit intomainfrom
export-D81476621
Closed

Pass globalPrefix through to serializer and expose to getRunModuleStatement#1566
robhogan wants to merge 1 commit intomainfrom
export-D81476621

Conversation

@robhogan
Copy link
Contributor

@robhogan robhogan commented Sep 2, 2025

Summary:
To allow prefixing the global function __r (e.g., #1512), expose the prefix to the configured 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

…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
@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 2, 2025
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D81476621

@facebook-github-bot
Copy link
Contributor

This pull request has been merged in db241c9.

@jbroma
Copy link
Contributor

jbroma commented Sep 8, 2025

this is perfect, thank you @robhogan 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported Merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants