Repository navigation
Conversation
6d8c827 to
f87312c
Compare
05dffaf to
49b9c59
Compare
598be7c to
6e66cf2
Compare
4ee2183 to
1c4fd57
Compare
…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
Metro review: Looks goodRisk: low · Commit: Adds Findings
Verified
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. |
| * The result is a pure function of (origin module, specifier). Nothing on disk | ||
| * can invalidate it. | ||
| */ | ||
| export function resolveInlineModule( |
There was a problem hiding this comment.
FYI reviewers - this is the meat of this PR. The rest is mainly tests, the feature flag, and plumbing.
huntie
left a comment
There was a problem hiding this comment.
LGTM, one curiosity question!
| * The result is a pure function of (origin module, specifier). Nothing on disk | ||
| * can invalidate it. | ||
| */ | ||
| export function resolveInlineModule( |
There was a problem hiding this comment.
Slightly naive question: Why isn't this in metro-resolver?
There was a problem hiding this comment.
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.
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
metro:inlinelets 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-workletsbundle mode extracts each worklet into its own module. Today it writes those modules intonode_modulesmid-transform and patchesgetOrComputeSha1to 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.
In your transform, encode the generated source into a specifier:
Then emit a
requireof that specifier into the file being transformed, as a string literal: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.
supportsInlineModulesis onlytruewith the option above, on a version of Metro with this change:Babel transformers that set their own caller data need to forward the option. We'll follow up in
@react-native/metro-babel-transformerand 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:App.tsx.tsxfile, including your own pluginDev bundles abbreviate these specifiers to
metro:inline;base64,...inrequirenames, 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 adata:URL as one module wherever it's imported, with no base URL for relative imports - inline modules are the opposite on both counts.Changelog
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.