Skip to content

Commit

Permalink
Don't assert that paths object is present in asyncRequire
Browse files Browse the repository at this point in the history
Summary:
Makes it possible to provide a `__loadBundleAsync` implementations in bundles that don't have `experimentalImportBundleSupport` (or the upcoming `lazy` flag) enabled, and thus don't serialise a `paths` object to pass to `asyncRequire` calls. React Native injects one on `main` since D43600052 so this is technically a fix - it prevents the non-lazy `import()` behaviour from breaking.

Changelog:
NOTE: Merge changelog entry with D43600050 and link to both commits.
* **[Feature]**: Support custom `__loadBundleAsync` implementations in the default `asyncRequire` function. See the [lazy bundling RFC](https://github.com/react-native-community/discussions-and-proposals/blob/main/proposals/0605-lazy-bundling.md) for more details.

Reviewed By: huntie

Differential Revision: D45161543

fbshipit-source-id: 8a9624b8ee0e083179ee03b5fa8f2760b6e79147
  • Loading branch information
motiz88 authored and facebook-github-bot committed Apr 24, 2023
1 parent ac3adce commit f07ce5c
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions packages/metro-runtime/src/modules/asyncRequire.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ async function asyncRequireImpl(

if (loadBundle != null) {
const stringModuleID = String(moduleID);
const bundlePath = nullthrows(paths)[stringModuleID];
if (bundlePath != null) {
// NOTE: Errors will be swallowed by asyncRequire.prefetch
await loadBundle(bundlePath);
if (paths != null) {
const bundlePath = paths[stringModuleID];
if (bundlePath != null) {
// NOTE: Errors will be swallowed by asyncRequire.prefetch
await loadBundle(bundlePath);
}
}
}

Expand Down Expand Up @@ -68,11 +70,3 @@ asyncRequire.prefetch = function (
};

module.exports = asyncRequire;

// Inline definition to save on a dependency
function nullthrows<T>(value: ?T): T {
if (value == null) {
throw new Error('Unexpected null or undefined');
}
return value;
}

0 comments on commit f07ce5c

Please sign in to comment.