@module-federation/metro 0.0.0-next-20250827124348

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.
Files changed (118) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +11 -0
  3. package/babel-plugin/index.js +115 -0
  4. package/babel-plugin/patch-initialize-core.js +61 -0
  5. package/babel-plugin/patch-require.js +174 -0
  6. package/bootstrap/index.d.ts +1 -0
  7. package/bootstrap/index.js +1 -0
  8. package/dist/babel/transformer.d.ts +2 -0
  9. package/dist/babel/transformer.js +15 -0
  10. package/dist/commands/bundle-host/index.d.ts +13 -0
  11. package/dist/commands/bundle-host/index.js +95 -0
  12. package/dist/commands/bundle-host/index.mjs +69 -0
  13. package/dist/commands/bundle-host/options.d.ts +5 -0
  14. package/dist/commands/bundle-host/options.js +39 -0
  15. package/dist/commands/bundle-host/options.mjs +7 -0
  16. package/dist/commands/bundle-host/types.d.ts +22 -0
  17. package/dist/commands/bundle-host/types.js +18 -0
  18. package/dist/commands/bundle-host/types.mjs +2 -0
  19. package/dist/commands/bundle-remote/index.d.ts +12 -0
  20. package/dist/commands/bundle-remote/index.js +226 -0
  21. package/dist/commands/bundle-remote/index.mjs +200 -0
  22. package/dist/commands/bundle-remote/options.d.ts +32 -0
  23. package/dist/commands/bundle-remote/options.js +109 -0
  24. package/dist/commands/bundle-remote/options.mjs +67 -0
  25. package/dist/commands/bundle-remote/types.d.ts +16 -0
  26. package/dist/commands/bundle-remote/types.js +18 -0
  27. package/dist/commands/bundle-remote/types.mjs +2 -0
  28. package/dist/commands/index.d.ts +48 -0
  29. package/dist/commands/index.js +72 -0
  30. package/dist/commands/index.mjs +13 -0
  31. package/dist/commands/types.d.ts +14 -0
  32. package/dist/commands/types.js +18 -0
  33. package/dist/commands/types.mjs +2 -0
  34. package/dist/commands/utils/create-module-path-remapper.d.ts +7 -0
  35. package/dist/commands/utils/create-module-path-remapper.js +64 -0
  36. package/dist/commands/utils/create-module-path-remapper.mjs +32 -0
  37. package/dist/commands/utils/create-resolver.d.ts +20 -0
  38. package/dist/commands/utils/create-resolver.js +57 -0
  39. package/dist/commands/utils/create-resolver.mjs +25 -0
  40. package/dist/commands/utils/get-community-plugin.d.ts +29 -0
  41. package/dist/commands/utils/get-community-plugin.js +50 -0
  42. package/dist/commands/utils/get-community-plugin.mjs +18 -0
  43. package/dist/commands/utils/load-metro-config.d.ts +3 -0
  44. package/dist/commands/utils/load-metro-config.js +84 -0
  45. package/dist/commands/utils/load-metro-config.mjs +43 -0
  46. package/dist/commands/utils/save-bundle-and-map.d.ts +5 -0
  47. package/dist/commands/utils/save-bundle-and-map.js +77 -0
  48. package/dist/commands/utils/save-bundle-and-map.mjs +57 -0
  49. package/dist/index.d.ts +2 -0
  50. package/dist/index.js +40 -0
  51. package/dist/index.mjs +5 -0
  52. package/dist/modules/HMRClient.ts +31 -0
  53. package/dist/modules/HMRClientShim.ts +1 -0
  54. package/dist/modules/asyncRequire.ts +134 -0
  55. package/dist/modules/asyncStartup.tsx +38 -0
  56. package/dist/modules/getDevServer.ts +15 -0
  57. package/dist/modules/metroCorePlugin.ts +83 -0
  58. package/dist/plugin/babel-transformer.d.ts +11 -0
  59. package/dist/plugin/babel-transformer.js +68 -0
  60. package/dist/plugin/babel-transformer.mjs +25 -0
  61. package/dist/plugin/constants.d.ts +9 -0
  62. package/dist/plugin/constants.js +68 -0
  63. package/dist/plugin/constants.mjs +12 -0
  64. package/dist/plugin/generators.d.ts +13 -0
  65. package/dist/plugin/generators.js +157 -0
  66. package/dist/plugin/generators.mjs +99 -0
  67. package/dist/plugin/helpers.d.ts +7 -0
  68. package/dist/plugin/helpers.js +119 -0
  69. package/dist/plugin/helpers.mjs +58 -0
  70. package/dist/plugin/index.d.ts +9 -0
  71. package/dist/plugin/index.js +167 -0
  72. package/dist/plugin/index.mjs +124 -0
  73. package/dist/plugin/manifest.d.ts +3 -0
  74. package/dist/plugin/manifest.js +146 -0
  75. package/dist/plugin/manifest.mjs +100 -0
  76. package/dist/plugin/normalize-extra-options.d.ts +8 -0
  77. package/dist/plugin/normalize-extra-options.js +46 -0
  78. package/dist/plugin/normalize-extra-options.mjs +14 -0
  79. package/dist/plugin/normalize-options.d.ts +7 -0
  80. package/dist/plugin/normalize-options.js +89 -0
  81. package/dist/plugin/normalize-options.mjs +46 -0
  82. package/dist/plugin/resolver.d.ts +25 -0
  83. package/dist/plugin/resolver.js +199 -0
  84. package/dist/plugin/resolver.mjs +160 -0
  85. package/dist/plugin/rewrite-request.d.ts +10 -0
  86. package/dist/plugin/rewrite-request.js +76 -0
  87. package/dist/plugin/rewrite-request.mjs +34 -0
  88. package/dist/plugin/serializer.d.ts +5 -0
  89. package/dist/plugin/serializer.js +171 -0
  90. package/dist/plugin/serializer.mjs +151 -0
  91. package/dist/plugin/validate-options.d.ts +2 -0
  92. package/dist/plugin/validate-options.js +61 -0
  93. package/dist/plugin/validate-options.mjs +29 -0
  94. package/dist/runtime/host-entry.d.ts +1 -0
  95. package/dist/runtime/host-entry.js +3 -0
  96. package/dist/runtime/init-host.d.ts +1 -0
  97. package/dist/runtime/init-host.js +31 -0
  98. package/dist/runtime/remote-entry.d.ts +1 -0
  99. package/dist/runtime/remote-entry.js +57 -0
  100. package/dist/runtime/remote-hmr.d.ts +1 -0
  101. package/dist/runtime/remote-hmr.js +19 -0
  102. package/dist/runtime/remote-module-registry.d.ts +7 -0
  103. package/dist/runtime/remote-module-registry.js +57 -0
  104. package/dist/runtime/remote-module.d.ts +1 -0
  105. package/dist/runtime/remote-module.js +2 -0
  106. package/dist/types.d.ts +26 -0
  107. package/dist/types.js +18 -0
  108. package/dist/types.mjs +2 -0
  109. package/dist/utils/errors.d.ts +8 -0
  110. package/dist/utils/errors.js +50 -0
  111. package/dist/utils/errors.mjs +15 -0
  112. package/dist/utils/index.d.ts +2 -0
  113. package/dist/utils/index.js +43 -0
  114. package/dist/utils/index.mjs +5 -0
  115. package/dist/utils/vm-manager.d.ts +21 -0
  116. package/dist/utils/vm-manager.js +116 -0
  117. package/dist/utils/vm-manager.mjs +73 -0
  118. package/package.json +91 -0
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.n = (module)=>{
5
+ var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
6
+ __webpack_require__.d(getter, {
7
+ a: getter
8
+ });
9
+ return getter;
10
+ };
11
+ })();
12
+ (()=>{
13
+ __webpack_require__.d = (exports1, definition)=>{
14
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
15
+ enumerable: true,
16
+ get: definition[key]
17
+ });
18
+ };
19
+ })();
20
+ (()=>{
21
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
22
+ })();
23
+ (()=>{
24
+ __webpack_require__.r = (exports1)=>{
25
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
26
+ value: 'Module'
27
+ });
28
+ Object.defineProperty(exports1, '__esModule', {
29
+ value: true
30
+ });
31
+ };
32
+ })();
33
+ var __webpack_exports__ = {};
34
+ __webpack_require__.r(__webpack_exports__);
35
+ __webpack_require__.d(__webpack_exports__, {
36
+ saveBundleAndMap: ()=>saveBundleAndMap
37
+ });
38
+ const external_node_fs_namespaceObject = require("node:fs");
39
+ const external_node_util_namespaceObject = require("node:util");
40
+ var external_node_util_default = /*#__PURE__*/ __webpack_require__.n(external_node_util_namespaceObject);
41
+ const relativizeSourceMap_namespaceObject = require("metro/src/lib/relativizeSourceMap");
42
+ var relativizeSourceMap_default = /*#__PURE__*/ __webpack_require__.n(relativizeSourceMap_namespaceObject);
43
+ function relativizeSerializedMap(map, sourceMapSourcesRoot) {
44
+ const sourceMap = JSON.parse(map);
45
+ relativizeSourceMap_default()(sourceMap, sourceMapSourcesRoot);
46
+ return JSON.stringify(sourceMap);
47
+ }
48
+ async function saveBundleAndMap(bundle, options, log) {
49
+ const { bundleOutput, bundleEncoding: encoding, sourcemapOutput, sourcemapSourcesRoot } = options;
50
+ const writeFns = [];
51
+ writeFns.push(async ()=>{
52
+ log(`Writing bundle output to:\n${external_node_util_default().styleText('dim', bundleOutput)}`);
53
+ await external_node_fs_namespaceObject.promises.writeFile(bundleOutput, bundle.code, encoding);
54
+ log('Done writing bundle output');
55
+ });
56
+ if (sourcemapOutput) {
57
+ let { map } = bundle;
58
+ if (null != sourcemapSourcesRoot) {
59
+ log('Start relativating source map');
60
+ map = relativizeSerializedMap(map, sourcemapSourcesRoot);
61
+ log('Finished relativating');
62
+ }
63
+ writeFns.push(async ()=>{
64
+ log(`Writing sourcemap output to:\n${external_node_util_default().styleText('dim', sourcemapOutput)}`);
65
+ await external_node_fs_namespaceObject.promises.writeFile(sourcemapOutput, map);
66
+ log('Done writing sourcemap output');
67
+ });
68
+ }
69
+ await Promise.all(writeFns.map((cb)=>cb()));
70
+ }
71
+ exports.saveBundleAndMap = __webpack_exports__.saveBundleAndMap;
72
+ for(var __webpack_i__ in __webpack_exports__)if (-1 === [
73
+ "saveBundleAndMap"
74
+ ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
75
+ Object.defineProperty(exports, '__esModule', {
76
+ value: true
77
+ });
@@ -0,0 +1,57 @@
1
+ import 'module';
2
+ /*#__PURE__*/ import.meta.url;
3
+ import { promises } from "node:fs";
4
+ import node_util from "node:util";
5
+ import { createRequire as __WEBPACK_EXTERNAL_createRequire } from "node:module";
6
+ var __webpack_require__ = {};
7
+ (()=>{
8
+ __webpack_require__.n = (module)=>{
9
+ var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
10
+ __webpack_require__.d(getter, {
11
+ a: getter
12
+ });
13
+ return getter;
14
+ };
15
+ })();
16
+ (()=>{
17
+ __webpack_require__.d = (exports, definition)=>{
18
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) Object.defineProperty(exports, key, {
19
+ enumerable: true,
20
+ get: definition[key]
21
+ });
22
+ };
23
+ })();
24
+ (()=>{
25
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
26
+ })();
27
+ const relativizeSourceMap_namespaceObject = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("metro/src/lib/relativizeSourceMap");
28
+ var relativizeSourceMap_default = /*#__PURE__*/ __webpack_require__.n(relativizeSourceMap_namespaceObject);
29
+ function relativizeSerializedMap(map, sourceMapSourcesRoot) {
30
+ const sourceMap = JSON.parse(map);
31
+ relativizeSourceMap_default()(sourceMap, sourceMapSourcesRoot);
32
+ return JSON.stringify(sourceMap);
33
+ }
34
+ async function saveBundleAndMap(bundle, options, log) {
35
+ const { bundleOutput, bundleEncoding: encoding, sourcemapOutput, sourcemapSourcesRoot } = options;
36
+ const writeFns = [];
37
+ writeFns.push(async ()=>{
38
+ log(`Writing bundle output to:\n${node_util.styleText('dim', bundleOutput)}`);
39
+ await promises.writeFile(bundleOutput, bundle.code, encoding);
40
+ log('Done writing bundle output');
41
+ });
42
+ if (sourcemapOutput) {
43
+ let { map } = bundle;
44
+ if (null != sourcemapSourcesRoot) {
45
+ log('Start relativating source map');
46
+ map = relativizeSerializedMap(map, sourcemapSourcesRoot);
47
+ log('Finished relativating');
48
+ }
49
+ writeFns.push(async ()=>{
50
+ log(`Writing sourcemap output to:\n${node_util.styleText('dim', sourcemapOutput)}`);
51
+ await promises.writeFile(sourcemapOutput, map);
52
+ log('Done writing sourcemap output');
53
+ });
54
+ }
55
+ await Promise.all(writeFns.map((cb)=>cb()));
56
+ }
57
+ export { saveBundleAndMap };
@@ -0,0 +1,2 @@
1
+ export { withModuleFederation } from './plugin';
2
+ export { updateManifest } from './plugin/manifest';
package/dist/index.js ADDED
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ withModuleFederation: ()=>index_js_namespaceObject.withModuleFederation,
28
+ updateManifest: ()=>manifest_js_namespaceObject.updateManifest
29
+ });
30
+ const index_js_namespaceObject = require("./plugin/index.js");
31
+ const manifest_js_namespaceObject = require("./plugin/manifest.js");
32
+ exports.updateManifest = __webpack_exports__.updateManifest;
33
+ exports.withModuleFederation = __webpack_exports__.withModuleFederation;
34
+ for(var __webpack_i__ in __webpack_exports__)if (-1 === [
35
+ "updateManifest",
36
+ "withModuleFederation"
37
+ ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
38
+ Object.defineProperty(exports, '__esModule', {
39
+ value: true
40
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,5 @@
1
+ import 'module';
2
+ /*#__PURE__*/ import.meta.url;
3
+ import { withModuleFederation } from "./plugin/index.mjs";
4
+ import { updateManifest } from "./plugin/manifest.mjs";
5
+ export { updateManifest, withModuleFederation };
@@ -0,0 +1,31 @@
1
+ import HMRClient from 'react-native/Libraries/Utilities/HMRClient';
2
+
3
+ let hmrOrigin: string | null = null;
4
+
5
+ const originalRegisterBundle = HMRClient.registerBundle;
6
+ const originalSetup = HMRClient.setup;
7
+
8
+ HMRClient.setup = (
9
+ platform: string,
10
+ bundleEntry: string,
11
+ host: string,
12
+ port: number | string,
13
+ isEnabled: boolean,
14
+ scheme = 'http',
15
+ ) => {
16
+ const serverHost = port !== null && port !== '' ? `${host}:${port}` : host;
17
+ hmrOrigin = `${scheme}://${serverHost}`;
18
+
19
+ return originalSetup(platform, bundleEntry, host, port, isEnabled, scheme);
20
+ };
21
+
22
+ HMRClient.registerBundle = (requestUrl: string) => {
23
+ // only process registerBundle calls from the same origin
24
+ if (!requestUrl.includes(hmrOrigin as string)) {
25
+ return;
26
+ }
27
+
28
+ return originalRegisterBundle(requestUrl);
29
+ };
30
+
31
+ export default HMRClient;
@@ -0,0 +1 @@
1
+ export { default } from 'react-native/Libraries/Utilities/HMRClientProdShim';
@@ -0,0 +1,134 @@
1
+ // join two paths
2
+ // e.g. /a/b/ + /c/d -> /a/b/c/d
3
+ function joinComponents(prefix: string, suffix: string) {
4
+ return prefix.replace(/\/+$/, '') + '/' + suffix.replace(/^\/+/, '');
5
+ }
6
+
7
+ // get the public path from the url
8
+ // e.g. http://host:8081/a/b.bundle -> http://host:8081/a
9
+ function getPublicPath(url?: string) {
10
+ return url?.split('/').slice(0, -1).join('/');
11
+ }
12
+
13
+ function isUrl(url: string) {
14
+ return url.match(/^https?:\/\//);
15
+ }
16
+
17
+ // get bundle id from the bundle path
18
+ // e.g. /a/b.bundle?platform=ios -> a/b
19
+ // e.g. http://host:8081/a/b.bundle -> a/b
20
+ function getBundleId(bundlePath: string, publicPath: string) {
21
+ let path = bundlePath;
22
+ // remove the public path if it's an url
23
+ if (isUrl(path)) {
24
+ path = path.replace(publicPath, '');
25
+ }
26
+ // remove the leading slash
27
+ if (path.startsWith('/')) {
28
+ path = path.slice(1);
29
+ }
30
+ // remove the query params
31
+ path = path.split('?')[0];
32
+ // remove the bundle extension
33
+ return path.replace('.bundle', '');
34
+ }
35
+
36
+ function isSameOrigin(url: string, originPublicPath?: string) {
37
+ // if it's not a fully qualified url, we assume it's the same origin
38
+ if (!isUrl(url)) {
39
+ return true;
40
+ }
41
+ return !!originPublicPath && url.startsWith(originPublicPath);
42
+ }
43
+
44
+ // prefix the bundle path with the public path
45
+ // e.g. /a/b.bundle -> http://host:8081/a/b.bundle
46
+ function getBundlePath(bundlePath: string, bundleOrigin?: string) {
47
+ // don't modify the path in development
48
+ if (process.env.NODE_ENV !== 'production') {
49
+ return bundlePath;
50
+ }
51
+ // don't modify fully qualified urls
52
+ // e.g. when loading container modules
53
+ if (isUrl(bundlePath)) {
54
+ return bundlePath;
55
+ }
56
+ // don't modify the path if we don't know the bundle origin
57
+ // e.g. when loading host split bundles
58
+ if (!bundleOrigin) {
59
+ return bundlePath;
60
+ }
61
+ return joinComponents(bundleOrigin, bundlePath);
62
+ }
63
+
64
+ function buildLoadBundleAsyncWrapper() {
65
+ const registry = require('mf:remote-module-registry');
66
+
67
+ const __loadBundleAsync =
68
+ // @ts-expect-error dynamic key access on global object
69
+ globalThis[`${__METRO_GLOBAL_PREFIX__ ?? ''}__loadBundleAsync`];
70
+
71
+ const loadBundleAsync =
72
+ __loadBundleAsync as typeof globalThis.__loadBundleAsync;
73
+
74
+ return async (originalBundlePath: string) => {
75
+ const scope = globalThis.__FEDERATION__.__NATIVE__[__METRO_GLOBAL_PREFIX__];
76
+
77
+ // entry is always in the root directory of assets associated with remote
78
+ // based on that, we extract the public path from the origin URL
79
+ // e.g. http://example.com/a/b/c/mf-manfiest.json -> http://example.com/a/b/c
80
+ const publicPath = getPublicPath(scope.origin);
81
+ const bundlePath = getBundlePath(originalBundlePath, publicPath);
82
+
83
+ // ../../node_modules/ -> ..%2F..%2Fnode_modules/ so that it's not automatically sanitized
84
+ const encodedBundlePath = bundlePath.replaceAll('../', '..%2F');
85
+
86
+ const result = await loadBundleAsync(encodedBundlePath);
87
+
88
+ // when the origin is not the same, it means we are loading a remote container
89
+ // we can return early since dependencies are processed differently for entry bundles
90
+ if (!isSameOrigin(bundlePath, publicPath)) {
91
+ return result;
92
+ }
93
+
94
+ // at this point the code in the bundle has been evaluated
95
+ // but not yet executed through metroRequire
96
+ // note: at this point, public path is always defined
97
+ const bundleId = getBundleId(bundlePath, publicPath!);
98
+ const shared = scope.deps.shared[bundleId];
99
+ const remotes = scope.deps.remotes[bundleId];
100
+
101
+ const promises = [];
102
+ if (shared && shared.length > 0) {
103
+ // load shared used synchronously in the bundle
104
+ promises.push(...shared.map(registry.loadSharedToRegistry));
105
+ }
106
+ if (remotes && remotes.length > 0) {
107
+ // load remotes used synchronously in the bundle
108
+ promises.push(...remotes.map(registry.loadRemoteToRegistry));
109
+ }
110
+
111
+ await Promise.all(promises);
112
+
113
+ return result;
114
+ };
115
+ }
116
+
117
+ // load expo async require if outside expo
118
+ if (!process.env.EXPO_OS) {
119
+ // @expo/metro-runtime/src/async-require/fetchAsync.native.ts requires
120
+ // process.env.EXPO_OS to be set but since expo is optional, we set it
121
+ // to an empty string as a fallback to prevent reference errors
122
+ process.env.EXPO_OS = '';
123
+
124
+ const {
125
+ buildAsyncRequire,
126
+ } = require('@expo/metro-runtime/src/async-require/buildAsyncRequire');
127
+
128
+ // @ts-expect-error dynamic key access on global object
129
+ global[`${__METRO_GLOBAL_PREFIX__}__loadBundleAsync`] = buildAsyncRequire();
130
+ }
131
+
132
+ // @ts-expect-error dynamic key access on global object
133
+ global[`${__METRO_GLOBAL_PREFIX__}__loadBundleAsync`] =
134
+ buildLoadBundleAsyncWrapper();
@@ -0,0 +1,38 @@
1
+ import type { Federation } from '@module-federation/runtime';
2
+ import React from 'react';
3
+
4
+ declare global {
5
+ var __DEV__: boolean;
6
+ var __METRO_GLOBAL_PREFIX__: string;
7
+ var __FUSEBOX_HAS_FULL_CONSOLE_SUPPORT__: boolean;
8
+ var __loadBundleAsync: (entry: string) => Promise<void>;
9
+ var __FEDERATION__: Federation;
10
+ }
11
+
12
+ type LazyComponent = { default: React.ComponentType };
13
+
14
+ function getFallbackComponent(lazyFallbackFn?: () => LazyComponent) {
15
+ if (!lazyFallbackFn) return () => null;
16
+ const fallback = lazyFallbackFn();
17
+ return fallback.default;
18
+ }
19
+
20
+ export function withAsyncStartup(
21
+ lazyAppFn: () => LazyComponent,
22
+ lazyFallbackFn?: () => LazyComponent,
23
+ ): () => () => React.JSX.Element {
24
+ const AppComponent = React.lazy(async () => {
25
+ await globalThis.__FEDERATION__.__NATIVE__[__METRO_GLOBAL_PREFIX__].init;
26
+ return lazyAppFn();
27
+ });
28
+
29
+ const FallbackComponent = getFallbackComponent(lazyFallbackFn);
30
+
31
+ return () => () => {
32
+ return (
33
+ <React.Suspense fallback={<FallbackComponent />}>
34
+ <AppComponent />
35
+ </React.Suspense>
36
+ );
37
+ };
38
+ }
@@ -0,0 +1,15 @@
1
+ export default function getDevServer() {
2
+ const scriptUrl =
3
+ globalThis.__FEDERATION__.__NATIVE__[__METRO_GLOBAL_PREFIX__].origin;
4
+
5
+ if (!scriptUrl) {
6
+ throw new Error(
7
+ `Cannot determine dev server URL for ${__METRO_GLOBAL_PREFIX__} remote`,
8
+ );
9
+ }
10
+
11
+ return {
12
+ url: scriptUrl.match(/^https?:\/\/.*?\//)![0],
13
+ fullBundleUrl: scriptUrl,
14
+ };
15
+ }
@@ -0,0 +1,83 @@
1
+ import type {
2
+ Federation,
3
+ FederationRuntimePlugin,
4
+ } from '@module-federation/runtime';
5
+
6
+ declare global {
7
+ var __DEV__: boolean;
8
+ var __METRO_GLOBAL_PREFIX__: string;
9
+ var __FUSEBOX_HAS_FULL_CONSOLE_SUPPORT__: boolean;
10
+ var __loadBundleAsync: (entry: string) => Promise<void>;
11
+ var __FEDERATION__: Federation;
12
+ }
13
+
14
+ const getQueryParams = () => {
15
+ const isFuseboxEnabled = !!globalThis.__FUSEBOX_HAS_FULL_CONSOLE_SUPPORT__;
16
+ const queryParams: Record<string, string> = {
17
+ platform: require('react-native').Platform.OS,
18
+ dev: 'true',
19
+ lazy: 'true',
20
+ minify: 'false',
21
+ runModule: 'true',
22
+ modulesOnly: 'false',
23
+ };
24
+
25
+ if (isFuseboxEnabled) {
26
+ queryParams.excludeSource = 'true';
27
+ queryParams.sourcePaths = 'url-server';
28
+ }
29
+
30
+ return new URLSearchParams(queryParams);
31
+ };
32
+
33
+ const buildUrlForEntryBundle = (entry: string) => {
34
+ if (__DEV__) {
35
+ return `${entry}?${getQueryParams().toString()}`;
36
+ }
37
+ return entry;
38
+ };
39
+
40
+ const MetroCorePlugin: () => FederationRuntimePlugin = () => ({
41
+ name: 'metro-core-plugin',
42
+ loadEntry: async ({ remoteInfo }) => {
43
+ const { entry, entryGlobalName } = remoteInfo;
44
+
45
+ const __loadBundleAsync =
46
+ // @ts-expect-error dynamic key access on global object
47
+ globalThis[`${__METRO_GLOBAL_PREFIX__ ?? ''}__loadBundleAsync`];
48
+
49
+ const loadBundleAsync =
50
+ __loadBundleAsync as typeof globalThis.__loadBundleAsync;
51
+
52
+ if (!loadBundleAsync) {
53
+ throw new Error('loadBundleAsync is not defined');
54
+ }
55
+
56
+ try {
57
+ const entryUrl = buildUrlForEntryBundle(entry);
58
+ await loadBundleAsync(entryUrl);
59
+
60
+ if (!globalThis.__FEDERATION__.__NATIVE__[entryGlobalName]) {
61
+ throw new Error(`Remote entry ${entryGlobalName} failed to register.`);
62
+ }
63
+
64
+ globalThis.__FEDERATION__.__NATIVE__[entryGlobalName].origin = entryUrl;
65
+
66
+ return globalThis.__FEDERATION__.__NATIVE__[entryGlobalName].exports;
67
+ } catch (error) {
68
+ throw new Error(
69
+ `Failed to load remote entry: ${entryGlobalName}. Reason: ${error}`,
70
+ );
71
+ }
72
+ },
73
+ generatePreloadAssets: async () => {
74
+ // noop for compatibility
75
+ return Promise.resolve({
76
+ cssAssets: [],
77
+ jsAssetsWithoutEntry: [],
78
+ entryAssets: [],
79
+ });
80
+ },
81
+ });
82
+
83
+ export default MetroCorePlugin;
@@ -0,0 +1,11 @@
1
+ import type { ModuleFederationConfigNormalized } from '../types';
2
+ interface CreateBabelTransformerOptions {
3
+ blacklistedPaths: string[];
4
+ federationConfig: ModuleFederationConfigNormalized;
5
+ originalBabelTransformerPath: string;
6
+ tmpDirPath: string;
7
+ enableInitializeCorePatching: boolean;
8
+ enableRuntimeRequirePatching: boolean;
9
+ }
10
+ export declare function createBabelTransformer({ blacklistedPaths, federationConfig, originalBabelTransformerPath, tmpDirPath, enableInitializeCorePatching, enableRuntimeRequirePatching, }: CreateBabelTransformerOptions): string;
11
+ export {};
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.n = (module)=>{
5
+ var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
6
+ __webpack_require__.d(getter, {
7
+ a: getter
8
+ });
9
+ return getter;
10
+ };
11
+ })();
12
+ (()=>{
13
+ __webpack_require__.d = (exports1, definition)=>{
14
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
15
+ enumerable: true,
16
+ get: definition[key]
17
+ });
18
+ };
19
+ })();
20
+ (()=>{
21
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
22
+ })();
23
+ (()=>{
24
+ __webpack_require__.r = (exports1)=>{
25
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
26
+ value: 'Module'
27
+ });
28
+ Object.defineProperty(exports1, '__esModule', {
29
+ value: true
30
+ });
31
+ };
32
+ })();
33
+ var __webpack_exports__ = {};
34
+ __webpack_require__.r(__webpack_exports__);
35
+ __webpack_require__.d(__webpack_exports__, {
36
+ createBabelTransformer: ()=>createBabelTransformer
37
+ });
38
+ const external_node_fs_namespaceObject = require("node:fs");
39
+ var external_node_fs_default = /*#__PURE__*/ __webpack_require__.n(external_node_fs_namespaceObject);
40
+ const external_node_path_namespaceObject = require("node:path");
41
+ var external_node_path_default = /*#__PURE__*/ __webpack_require__.n(external_node_path_namespaceObject);
42
+ function createBabelTransformer({ blacklistedPaths, federationConfig, originalBabelTransformerPath, tmpDirPath, enableInitializeCorePatching, enableRuntimeRequirePatching }) {
43
+ const outputPath = external_node_path_default().join(tmpDirPath, 'babel-transformer.js');
44
+ const templatePath = require.resolve('../babel/transformer.js');
45
+ const transformerTemplate = external_node_fs_default().readFileSync(templatePath, 'utf-8');
46
+ const plugins = [
47
+ [
48
+ '@module-federation/metro/babel-plugin',
49
+ {
50
+ blacklistedPaths,
51
+ remotes: federationConfig.remotes,
52
+ shared: federationConfig.shared
53
+ }
54
+ ],
55
+ enableInitializeCorePatching ? '@module-federation/metro/babel-plugin/patch-initialize-core' : void 0,
56
+ enableRuntimeRequirePatching ? '@module-federation/metro/babel-plugin/patch-require' : void 0
57
+ ].filter(Boolean);
58
+ const babelTransformer = transformerTemplate.replaceAll('__BABEL_TRANSFORMER_PATH__', originalBabelTransformerPath).replaceAll('__BABEL_PLUGINS__', JSON.stringify(plugins));
59
+ external_node_fs_default().writeFileSync(outputPath, babelTransformer, 'utf-8');
60
+ return outputPath;
61
+ }
62
+ exports.createBabelTransformer = __webpack_exports__.createBabelTransformer;
63
+ for(var __webpack_i__ in __webpack_exports__)if (-1 === [
64
+ "createBabelTransformer"
65
+ ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
66
+ Object.defineProperty(exports, '__esModule', {
67
+ value: true
68
+ });
@@ -0,0 +1,25 @@
1
+ import __rslib_shim_module__ from 'module';
2
+ const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(import.meta.url);
3
+ import node_fs from "node:fs";
4
+ import node_path from "node:path";
5
+ function createBabelTransformer({ blacklistedPaths, federationConfig, originalBabelTransformerPath, tmpDirPath, enableInitializeCorePatching, enableRuntimeRequirePatching }) {
6
+ const outputPath = node_path.join(tmpDirPath, 'babel-transformer.js');
7
+ const templatePath = require.resolve('../babel/transformer.js');
8
+ const transformerTemplate = node_fs.readFileSync(templatePath, 'utf-8');
9
+ const plugins = [
10
+ [
11
+ '@module-federation/metro/babel-plugin',
12
+ {
13
+ blacklistedPaths,
14
+ remotes: federationConfig.remotes,
15
+ shared: federationConfig.shared
16
+ }
17
+ ],
18
+ enableInitializeCorePatching ? '@module-federation/metro/babel-plugin/patch-initialize-core' : void 0,
19
+ enableRuntimeRequirePatching ? '@module-federation/metro/babel-plugin/patch-require' : void 0
20
+ ].filter(Boolean);
21
+ const babelTransformer = transformerTemplate.replaceAll('__BABEL_TRANSFORMER_PATH__', originalBabelTransformerPath).replaceAll('__BABEL_PLUGINS__', JSON.stringify(plugins));
22
+ node_fs.writeFileSync(outputPath, babelTransformer, 'utf-8');
23
+ return outputPath;
24
+ }
25
+ export { createBabelTransformer };
@@ -0,0 +1,9 @@
1
+ export declare const INIT_HOST = "mf:init-host";
2
+ export declare const ASYNC_REQUIRE = "mf:async-require";
3
+ export declare const REMOTE_MODULE_REGISTRY = "mf:remote-module-registry";
4
+ export declare const REMOTE_HMR_SETUP = "mf:remote-hmr";
5
+ export declare const DEFAULT_ENTRY_FILENAME = "remoteEntry.bundle";
6
+ export declare const GET_DEV_SERVER_REGEX: RegExp;
7
+ export declare const HMR_CLIENT_REGEX: RegExp;
8
+ export declare const TMP_DIR_NAME = ".mf-metro";
9
+ export declare const MANIFEST_FILENAME = "mf-manifest.json";