Skip to content

Add unstable_lazilyDefineModules serializer (switch-bundle output) - #1861

Closed
robhogan wants to merge 1 commit into
mainfrom
export-D113676086
Closed

Add unstable_lazilyDefineModules serializer (switch-bundle output)#1861
robhogan wants to merge 1 commit into
mainfrom
export-D113676086

Conversation

@robhogan

Copy link
Copy Markdown
Contributor

Summary:
Ports the metro-buck "plain bundle with switch" output format to OSS Metro, behind a new opt-in serializer.unstable_lazilyDefineModules.

baseJSBundleWithLazyModuleDefinition emits the graph's modules inside a single segment definer:

__registerSegment(0, function (moduleId) { switch (...) })

...so each module's __d(...) runs lazily on first require, rather than eagerly at startup. Polyfills/runtime (pre-modules) and run-module calls stay eager. This mirrors plain-bundle-with-switch.js in the metro-buck worker, built on the same OSS wrapModule primitive, but parameterised on OSS Module/graph types.

Because the switch wraps all modules into one function, the flat index-map path (fromRawMappingsIndexed, which assumes the plain top-level __d layout) would misalign, so code and source map are assembled together via metro-source-map's BundleBuilder, which offsets each module's map section by its actual position in the output.

The structured graph is untouched, so deltas and HMR (addressed per-module via hmrJSBundle) are unaffected: an HMR update remains a top-level __d(...) that shadows the switch branch, and the runtime materialises a not-yet-required module on demand via its segment definer.

This diff only adds the serializer + config. Wiring into Server (dev + prod) follows in the next diff.

Changelog:

 - **[Experimental]**: Add serializer.unstable_lazyDefineModules for deferred __d calls

Reviewed By: huntie

Differential Revision: D113676086

Summary:
Ports the metro-buck "plain bundle with switch" output format to OSS Metro, behind a new opt-in `serializer.unstable_lazilyDefineModules`.

`baseJSBundleWithLazyModuleDefinition` emits the graph's modules inside a single segment definer:
```
__registerSegment(0, function (moduleId) { switch (...) })
```

...so each module's `__d(...)` runs lazily on first require, rather than eagerly at startup. Polyfills/runtime (pre-modules) and run-module calls stay eager. This mirrors `plain-bundle-with-switch.js` in the metro-buck worker, built on the same OSS `wrapModule` primitive, but parameterised on OSS `Module`/graph types.

Because the switch wraps all modules into one function, the flat index-map path (`fromRawMappingsIndexed`, which assumes the plain top-level `__d` layout) would misalign, so code and source map are assembled together via `metro-source-map`'s `BundleBuilder`, which offsets each module's map section by its actual position in the output.

The structured graph is untouched, so deltas and HMR (addressed per-module via `hmrJSBundle`) are unaffected: an HMR update remains a top-level `__d(...)` that shadows the switch branch, and the runtime materialises a not-yet-required module on demand via its segment definer. 

This diff only adds the serializer + config. Wiring into Server (dev + prod) follows in the next diff.

Changelog:
```
 - **[Experimental]**: Add serializer.unstable_lazyDefineModules for deferred __d calls
```

Reviewed By: huntie

Differential Revision: D113676086
@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 Aug 17, 2026
@meta-codesync

meta-codesync Bot commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

@robhogan has exported this pull request. If you are a Meta employee, you can view the originating Diff in D113676086.

@meta-codesync

meta-codesync Bot commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

This pull request has been merged in 8d92267.

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. Merged meta-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant