metro-runtime 0.76.2 → 0.76.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "metro-runtime",
3
- "version": "0.76.2",
3
+ "version": "0.76.3",
4
4
  "description": "🚇 Module required for evaluating Metro bundles.",
5
5
  "main": "src",
6
6
  "repository": {
@@ -1,17 +1,45 @@
1
+ "use strict";
2
+
1
3
  /**
2
4
  * Copyright (c) Meta Platforms, Inc. and affiliates.
3
5
  *
4
6
  * This source code is licensed under the MIT license found in the
5
7
  * LICENSE file in the root directory of this source tree.
6
8
  *
7
- * @format
8
9
  *
10
+ * @format
11
+ * @oncall react_native
9
12
  */
10
13
 
11
- "use strict";
12
-
13
- // $FlowExpectedError Flow does not know about Metro's require extensions.
14
- const dynamicRequire = require;
15
- module.exports = function (moduleID) {
16
- return Promise.resolve().then(() => dynamicRequire.importAll(moduleID));
14
+ const DEFAULT_OPTIONS = {
15
+ isPrefetchOnly: false,
16
+ };
17
+ async function asyncRequireImpl(moduleID, paths, options) {
18
+ const loadBundle = global[`${__METRO_GLOBAL_PREFIX__}__loadBundleAsync`];
19
+ if (loadBundle != null) {
20
+ const stringModuleID = String(moduleID);
21
+ if (paths != null) {
22
+ const bundlePath = paths[stringModuleID];
23
+ if (bundlePath != null) {
24
+ // NOTE: Errors will be swallowed by asyncRequire.prefetch
25
+ await loadBundle(bundlePath);
26
+ }
27
+ }
28
+ }
29
+ if (!options.isPrefetchOnly) {
30
+ return require.importAll(moduleID);
31
+ }
32
+ return undefined;
33
+ }
34
+ async function asyncRequire(moduleID, paths, moduleName) {
35
+ return asyncRequireImpl(moduleID, paths, DEFAULT_OPTIONS);
36
+ }
37
+ asyncRequire.prefetch = function (moduleID, paths, moduleName) {
38
+ asyncRequireImpl(moduleID, paths, {
39
+ isPrefetchOnly: true,
40
+ }).then(
41
+ () => {},
42
+ () => {}
43
+ );
17
44
  };
45
+ module.exports = asyncRequire;
@@ -4,14 +4,69 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  *
7
+ * @flow strict-local
7
8
  * @format
8
- * @flow strict
9
+ * @oncall react_native
9
10
  */
10
11
 
11
- 'use strict';
12
+ type Options = {isPrefetchOnly: boolean, ...};
13
+ type MetroRequire = {
14
+ (number): mixed,
15
+ importAll: number => mixed,
16
+ ...
17
+ };
18
+
19
+ declare var require: MetroRequire;
20
+
21
+ const DEFAULT_OPTIONS = {isPrefetchOnly: false};
22
+
23
+ type DependencyMapPaths = ?$ReadOnly<{[moduleID: number | string]: mixed}>;
24
+
25
+ declare var __METRO_GLOBAL_PREFIX__: string;
26
+
27
+ async function asyncRequireImpl(
28
+ moduleID: number,
29
+ paths: DependencyMapPaths,
30
+ options: Options,
31
+ ): Promise<mixed> {
32
+ const loadBundle: (bundlePath: mixed) => Promise<void> =
33
+ global[`${__METRO_GLOBAL_PREFIX__}__loadBundleAsync`];
12
34
 
13
- // $FlowExpectedError Flow does not know about Metro's require extensions.
14
- const dynamicRequire = (require: {importAll: mixed => mixed});
15
- module.exports = function (moduleID: mixed): Promise<mixed> {
16
- return Promise.resolve().then(() => dynamicRequire.importAll(moduleID));
35
+ if (loadBundle != null) {
36
+ const stringModuleID = String(moduleID);
37
+ if (paths != null) {
38
+ const bundlePath = paths[stringModuleID];
39
+ if (bundlePath != null) {
40
+ // NOTE: Errors will be swallowed by asyncRequire.prefetch
41
+ await loadBundle(bundlePath);
42
+ }
43
+ }
44
+ }
45
+
46
+ if (!options.isPrefetchOnly) {
47
+ return require.importAll(moduleID);
48
+ }
49
+
50
+ return undefined;
51
+ }
52
+
53
+ async function asyncRequire(
54
+ moduleID: number,
55
+ paths: DependencyMapPaths,
56
+ moduleName?: string,
57
+ ): Promise<mixed> {
58
+ return asyncRequireImpl(moduleID, paths, DEFAULT_OPTIONS);
59
+ }
60
+
61
+ asyncRequire.prefetch = function (
62
+ moduleID: number,
63
+ paths: DependencyMapPaths,
64
+ moduleName?: string,
65
+ ): void {
66
+ asyncRequireImpl(moduleID, paths, {isPrefetchOnly: true}).then(
67
+ () => {},
68
+ () => {},
69
+ );
17
70
  };
71
+
72
+ module.exports = asyncRequire;