Skip to content

Conversation

robhogan
Copy link
Contributor

@robhogan robhogan commented Apr 27, 2025

Currently, Metro leaves import.meta accesses untransformed, but provides no import pseudoglobal, so (unless polyfilled) this throws at runtime.

import.meta is specified as a writable null-proto object. Though the spec is silent on any properties, it provides a mechanism (HostGetImportMetaProperties) for hosts to set properties, given a module object. Eg: url, resolve are defined by Node.js and browsers (and notably env by Vite).

We emulate that mechanism here with the __getImportMetaProperties(module) => {[key: string]: mixed} optional, framework-defined global function.

@facebook-github-bot facebook-github-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 Apr 27, 2025
@satya164
Copy link
Contributor

What do you think about adding import.meta.resolve in addition to import.meta.url?

It's supported in both browsers and node, and would be a replacement for require.resolve.

@robhogan
Copy link
Contributor Author

robhogan commented Apr 27, 2025

What do you think about adding import.meta.resolve in addition to import.meta.url?

It's supported in both browsers and node, and would be a replacement for require.resolve.

The plan is for RN (or Expo) to define properties using the __getImportMetaProperties hook, which is analogous to host-defined props in the ES spec (HostGetImportMetaProperties) - Metro doesn't define url either (it does here only in an integration test).

Metro could define some more defaults though..

Comment on lines +31 to +34
__getImportMetaProperties: module => {
return {
url: new URL(String(module.id), 'metro://'),
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this code will be added to react native itself?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, Metro leaves `import.meta` accesses untransformed, but provides no `import` pseudoglobal, so (unless polyfilled) this throws at runtime.

`import.meta` is [specified](https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-meta-properties) as a writable null-proto object. Though the spec is silent on any properties, it provides a mechanism ([`HostGetImportMetaProperties`](https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-hostgetimportmetaproperties)) for hosts to set properties, given a module object. Eg: `url`, `resolve` are defined by Node.js and browsers (and notably `env` by Vite).

We emulate that mechanism here with the `__getImportMetaProperties(module) => {[key: string]: mixed}` optional, framework-defined global function.
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants