Skip to content

Resolver: Add metro:inline specifiers for importer-anchored inline modules - #2037

Merged
robhogan merged 7 commits into
mainfrom
pr2037
Nov 4, 2026
Merged

robhogan merged 7 commits into
mainfrom
pr2037

Conversation

@robhogan

@robhogan robhogan commented Oct 7, 2026 •

Copy link
Copy Markdown
Collaborator

metro:inline lets a transform emit a module derived from the file it's transforming, without writing anything to disk. It's for authors of Babel plugins and custom transformers, rather than something to write by hand in app code.

For example, react-native-worklets bundle mode extracts each worklet into its own module. Today it writes those modules into node_modules mid-transform and patches getOrComputeSha1 to return a random SHA-1 for them, which defeats the transform cache for every worklet.

Usage

Users/frameworks must opt in to virtual modules in their Metro config for now.

module.exports = {
  resolver: {unstable_allowVirtualModules: true},
};

In your transform, encode the generated source into a specifier:

const source = `
  import {increment} from './counter';
  export default () => increment();
`;
const specifier =
  'metro:inline;base64,' + Buffer.from(source).toString('base64');

Then emit a require of that specifier into the file being transformed, as a string literal:

// App.tsx, after your transform
const generated = require('metro:inline;base64,<payload>');

metro:inline,<percent-encoded source> works too.

Metro tells Babel plugins whether this is available through caller data, so a plugin can fall back if not. supportsInlineModules is only true with the option above, on a version of Metro with this change:

const canInline = api.caller(caller => caller?.supportsInlineModules === true);

Babel transformers that set their own caller data need to forward the option. We'll follow up in @react-native/metro-babel-transformer and Expo's transformer.

What Metro does with it

Metro decodes the payload and adds a module to the graph beside its importer, named App.inline-<hash>.tsx. From there it behaves much like a real file at that path:

  • Imports inside it resolve as they would from App.tsx
  • It goes through the normal transform pipeline as a .tsx file, including your own plugin
  • The same source in two files is two modules, each with its own state. Within one file, it's one module
  • Changing the source changes the hash, so it's a new module. Unchanged source hits the transform cache

Dev bundles abbreviate these specifiers to metro:inline;base64,... in require names, so each payload only appears once, as the module itself.

Note that Metro can't serve source maps, lazy bundles or source text by URL for virtual modules yet.

This is deliberately not a data: URL. Node and browsers treat a data: URL as one module wherever it's imported, with no base URL for relative imports - inline modules are the opposite on both counts.

Changelog

 - **[Experimental]**: Resolver: `metro:inline;base64,<source>` resolves to a virtual module owned by its importer, under `resolver.unstable_allowVirtualModules`. Babel plugins can detect this with `supportsInlineModules` caller data

Test plan

New unit and integration tests.

Worklets bundle mode was prototyped against an earlier revision in https://github.com/robhogan/metro-x-worklets. That still needs updating and a device run against this PR.

@robhogan
robhogan added this pull request to stack #2038 October 7, 2026 16:00
@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 Oct 7, 2026
@robhogan
robhogan force-pushed the pr2037 branch 2 times, most recently from 6d8c827 to f87312c Compare October 7, 2026 17:24
@robhogan
robhogan force-pushed the pr2037 branch 2 times, most recently from 05dffaf to 49b9c59 Compare October 8, 2026 11:03
@robhogan
robhogan force-pushed the pr2037 branch 2 times, most recently from 598be7c to 6e66cf2 Compare October 8, 2026 12:46
@robhogan
robhogan removed this pull request from stack #2038 October 8, 2026 12:55
@robhogan
robhogan added this pull request to stack #2042 October 8, 2026 12:56
@robhogan
robhogan removed this pull request from stack #2042 October 11, 2026 14:41
@robhogan
robhogan added this pull request to stack #2057 October 11, 2026 14:41
@robhogan
robhogan force-pushed the pr2037 branch 4 times, most recently from 4ee2183 to 1c4fd57 Compare November 2, 2026 19:29
Base automatically changed from pr2036 to main November 3, 2026 10:35
…dules

Adds `metro:inline;base64,<payload>` (or `metro:inline,<percent-encoded>`) under the existing `metro:` scheme resolver. The payload is the module's source, and the resolver answers with a `VirtualResolution` whose virtual path sits beside the importing file, named for the importer and a hash of the source: `App.tsx` imports `App.inline-<hash>.tsx`. Relative and package imports inside the module resolve as they would from the importer, the transform treats it as the importer's kind of file, and the same payload in two files is two modules.

That is what a transform needs when it emits a module derived from the file it is transforming. `react-native-worklets` bundle mode does this today by writing files into `node_modules` mid-transform and patching `getOrComputeSha1` to return a random SHA-1 for them, which defeats the transform cache for every worklet.

This is deliberately not spelled `data:`. A `data:` URL is importer-independent and has no base URL, and Node and browsers treat the same URL as one module wherever it is imported. Inline modules are the opposite on both counts.

`keepRequireNames` output abbreviates an inline specifier to its header, so a dev bundle carries each payload once as the module rather than again in every `require` call.

Changelog: [Feature] Resolver: `metro:inline;base64,<source>` resolves to a virtual module owned by the importer
@github-actions

github-actions Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Metro review: Looks good

Risk: low · Commit: bde2cf7 · 8 commands run

Adds metro:inline specifiers that resolve to importer-anchored virtual modules, behind resolver.unstable_allowVirtualModules, and exposes supportsInlineModules to Babel plugins through caller data. Both earlier findings are largely resolved: malformed payloads now fail the build and the format is documented. One narrow base64 gap remains as a nit. One-shot builds behaved correctly with nested inline modules, package scope, empty sources, both encodings and the transform cache, but watch-mode edits were not exercised end to end.

Findings

Verified
  • ❌ yarn jest --no-watchman <resolveInlineModule, inline-module, metro-transform-worker, metro-babel-transformer, Transformer and collectDependencies tests> — 7 of 8 suites passed. A jest worker crashed with SIGSEGV in metro-transform-worker's index-test during this parallel run; see the serial rerun.
  • ✅ node_modules/.bin/jest --no-watchman -i packages/metro-transform-worker/src/__tests__/index-test.js — 32 tests pass at head and 29 at base when run serially. The SIGSEGV did not recur in this one rerun, and its cause was not investigated.
  • ✅ node_modules/.bin/jest --no-watchman -i packages/metro/src/DeltaBundler/__tests__/Graph-test.js buildSubgraph-test.js resolver-test.js — 351 tests. These suites already include virtual module cases.
  • ✅ node_modules/.bin/jest --no-watchman -i packages/metro/src/integration_tests/__tests__/review-inline-test.js — Repro on web and ios: nested inline, empty source, both encodings resolving to one module, browser field, #imports and package self-reference from a node_modules importer, ESM import and dynamic import. Dev names were abbreviated. A second build got 16 of 16 transform cache hits.
  • ✅ node_modules/.bin/jest --no-watchman -i packages/metro/src/integration_tests/__tests__/review-inline-watch-test.js (failed as expected) — The base64 cases ran and logged the silent decodes in the finding. The watch-mode case timed out with no change event after the first edit, before any inline handling, so it says nothing about the PR. Cause not found.
  • ✅ yarn typecheck — Flow: no errors.
  • ✅ yarn verify-api-snapshots — 18 public API snapshots verified.
  • ❌ yarn lint <changed source files> — The script runs eslint .. All 8 errors and 3 warnings are in this review's own repro tests and fixtures, none in the PR's files.

Not run: Watch-mode edits and live-server HMR: my harness got no change event (cause not found), so only existing Graph unit tests cover this. One bundled Bash command was auto-denied by a shell-quoting check; I didn't resend it, and made the repro files with Write instead. Windows is unverified (no machine), including the integration test's POSIX-only name regex. The worklets device run the PR lists as pending wasn't attempted. No benchmarks: the always-on costs are a dev name prefix check per dependency and one config spread at startup, neither plausibly measurable.

Automated review by Claude. Advisory only: it never approves or blocks, and a maintainer makes the call.

Comment thread docs/Configuration.md Resolved
* The result is a pure function of (origin module, specifier). Nothing on disk
* can invalidate it.
*/
export function resolveInlineModule(

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

FYI reviewers - this is the meat of this PR. The rest is mainly tests, the feature flag, and plumbing.

@robhogan
robhogan requested review from javache and motiz88 November 4, 2026 16:45

@huntie huntie left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, one curiosity question!

* The result is a pure function of (origin module, specifier). Nothing on disk
* can invalidate it.
*/
export function resolveInlineModule(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Slightly naive question: Why isn't this in metro-resolver?

@robhogan robhogan Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fair question - if this were a totally standalone type it could sit in the resolver, but it's here because I've made it part of the pre-existing metro: scheme (to colocate with other Metro-specific specifiers), and the scheme lives in metro because it touches things that aren't (IMO shouldn't be) in the pure metro-resolver.

E.g, metro:runtime always resolves to metro's metro-runtime dependency, and metro:babel-runtime similarly. On an implementation level I wouldn't want to introduce a metro-resolver -> metro-runtime dependency, and conceptually the fact that we even have a runtime or use Babel shouldn't be a concern of metro-resolver - it's a step higher. So metro plugs its metro: scheme into the resolver just as expo: or react-native: might in future.

@robhogan
robhogan merged commit e7abf93 into main Nov 4, 2026
16 checks passed
@robhogan
robhogan deleted the pr2037 branch November 4, 2026 18:40
meta-codesync Bot pushed a commit that referenced this pull request Nov 9, 2026
Summary:


Bump Metro to 0.88.0, a breaking release: https://github.com/react/metro/releases/tag/v0.88.0

Almost all of the breaking changes are integrator-facing, the others are resolver spec alignment. Users are most likely to notice:

 - Packages' `"exports"` are read from the package root, and `"browser"` redirections apply after them, so some subpath imports resolve to a different build. `resolver.unstable_resolveExportsFromPackageRoot` and `resolver.unstable_applyBrowserSpecAfterExports` turn either off.
 - `resolver.emptyModulePath` defaults to `metro:runtime/modules/empty-module`. Custom resolvers already have to delegate `metro:` specifiers they don't handle, for `metro:babel-runtime`, but code that reads the option as a file path breaks.
 
 **NOTE: *I intend to pick this to 0.89.0-rc.1***

### Other changes in this PR

 - Removes the config defaults that now match Metro's core defaults (`transformer.asyncRequireModulePath`, `transformer.allowOptionalDependencies` and `watchFolders: []`)
 - Remove two Flow suppressions for transformer options that Metro's types now declare.
 - `react-native/metro-babel-transformer` passes `supportsInlineModules` to Babel in the caller, as `metro-babel-transformer` does, so that presets and plugins can tell whether `metro:inline` specifiers (#2037) will resolve.

## Changelog

[General] [Changed] - Bump Metro to 0.88.0
[General] [Added] - `react-native/metro-babel-transformer` passes Metro's `supportsInlineModules` to Babel presets and plugins

X-link: react/react-native#59831

Test Plan:
```
yarn install --frozen-lockfile --ignore-scripts
yarn flow-check
yarn test packages/metro-config packages/react-native-babel-transformer packages/community-cli-plugin
```

New unit test for `supportsInlineModules`, which fails for `true` and `false` without the change. `getDefaultConfig()` returns the same values for every option with and without the removed defaults.

Metro 0.88.0 was tested end to end in a React Native 0.89.0-rc.0 template app before release (#2192).

Reviewed By: vzaidman

Differential Revision: D130252821

Pulled By: cortinico
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.

2 participants