From f07ce5c455a76b63d47c5ccec9611fc8cd66cd0e Mon Sep 17 00:00:00 2001 From: Moti Zilberman Date: Mon, 24 Apr 2023 01:28:09 -0700 Subject: [PATCH] Don't assert that `paths` object is present in asyncRequire 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 --- .../metro-runtime/src/modules/asyncRequire.js | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/packages/metro-runtime/src/modules/asyncRequire.js b/packages/metro-runtime/src/modules/asyncRequire.js index c0f9fd460c..2d50bbf7d3 100644 --- a/packages/metro-runtime/src/modules/asyncRequire.js +++ b/packages/metro-runtime/src/modules/asyncRequire.js @@ -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); + } } } @@ -68,11 +70,3 @@ asyncRequire.prefetch = function ( }; module.exports = asyncRequire; - -// Inline definition to save on a dependency -function nullthrows(value: ?T): T { - if (value == null) { - throw new Error('Unexpected null or undefined'); - } - return value; -}