@module-federation/metro 0.0.0-chore-bump-node-22-20260710161714

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 (126) 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.js +15 -0
  9. package/dist/commands/bundle-host/index.d.ts +13 -0
  10. package/dist/commands/bundle-host/index.js +100 -0
  11. package/dist/commands/bundle-host/index.mjs +47 -0
  12. package/dist/commands/bundle-host/options.d.ts +5 -0
  13. package/dist/commands/bundle-host/options.js +43 -0
  14. package/dist/commands/bundle-host/options.mjs +6 -0
  15. package/dist/commands/bundle-host/types.d.ts +22 -0
  16. package/dist/commands/bundle-host/types.js +18 -0
  17. package/dist/commands/bundle-host/types.mjs +1 -0
  18. package/dist/commands/bundle-remote/index.d.ts +12 -0
  19. package/dist/commands/bundle-remote/index.js +241 -0
  20. package/dist/commands/bundle-remote/index.mjs +190 -0
  21. package/dist/commands/bundle-remote/options.d.ts +32 -0
  22. package/dist/commands/bundle-remote/options.js +113 -0
  23. package/dist/commands/bundle-remote/options.mjs +66 -0
  24. package/dist/commands/bundle-remote/types.d.ts +16 -0
  25. package/dist/commands/bundle-remote/types.js +18 -0
  26. package/dist/commands/bundle-remote/types.mjs +1 -0
  27. package/dist/commands/index.d.ts +48 -0
  28. package/dist/commands/index.js +76 -0
  29. package/dist/commands/index.mjs +13 -0
  30. package/dist/commands/types.d.ts +14 -0
  31. package/dist/commands/types.js +18 -0
  32. package/dist/commands/types.mjs +1 -0
  33. package/dist/commands/utils/create-module-path-remapper.d.ts +7 -0
  34. package/dist/commands/utils/create-module-path-remapper.js +68 -0
  35. package/dist/commands/utils/create-module-path-remapper.mjs +31 -0
  36. package/dist/commands/utils/create-resolver.d.ts +20 -0
  37. package/dist/commands/utils/create-resolver.js +61 -0
  38. package/dist/commands/utils/create-resolver.mjs +24 -0
  39. package/dist/commands/utils/get-community-plugin.d.ts +28 -0
  40. package/dist/commands/utils/get-community-plugin.js +53 -0
  41. package/dist/commands/utils/get-community-plugin.mjs +17 -0
  42. package/dist/commands/utils/load-metro-config.d.ts +3 -0
  43. package/dist/commands/utils/load-metro-config.js +85 -0
  44. package/dist/commands/utils/load-metro-config.mjs +41 -0
  45. package/dist/commands/utils/path-utils.d.ts +2 -0
  46. package/dist/commands/utils/path-utils.js +51 -0
  47. package/dist/commands/utils/path-utils.mjs +11 -0
  48. package/dist/commands/utils/save-bundle-and-map.d.ts +5 -0
  49. package/dist/commands/utils/save-bundle-and-map.js +80 -0
  50. package/dist/commands/utils/save-bundle-and-map.mjs +33 -0
  51. package/dist/index.d.ts +2 -0
  52. package/dist/index.js +44 -0
  53. package/dist/index.mjs +3 -0
  54. package/dist/logger.d.ts +2 -0
  55. package/dist/logger.js +43 -0
  56. package/dist/logger.mjs +6 -0
  57. package/dist/modules/HMRClient.ts +31 -0
  58. package/dist/modules/HMRClientShim.ts +1 -0
  59. package/dist/modules/asyncRequire.ts +156 -0
  60. package/dist/modules/asyncStartup.tsx +43 -0
  61. package/dist/modules/getDevServer.ts +15 -0
  62. package/dist/modules/metroCorePlugin.ts +87 -0
  63. package/dist/plugin/babel-transformer.d.ts +11 -0
  64. package/dist/plugin/babel-transformer.js +72 -0
  65. package/dist/plugin/babel-transformer.mjs +25 -0
  66. package/dist/plugin/constants.d.ts +10 -0
  67. package/dist/plugin/constants.js +76 -0
  68. package/dist/plugin/constants.mjs +12 -0
  69. package/dist/plugin/generators.d.ts +13 -0
  70. package/dist/plugin/generators.js +164 -0
  71. package/dist/plugin/generators.mjs +102 -0
  72. package/dist/plugin/helpers.d.ts +8 -0
  73. package/dist/plugin/helpers.js +127 -0
  74. package/dist/plugin/helpers.mjs +58 -0
  75. package/dist/plugin/index.d.ts +11 -0
  76. package/dist/plugin/index.js +219 -0
  77. package/dist/plugin/index.mjs +172 -0
  78. package/dist/plugin/manifest-middleware.d.ts +12 -0
  79. package/dist/plugin/manifest-middleware.js +217 -0
  80. package/dist/plugin/manifest-middleware.mjs +170 -0
  81. package/dist/plugin/manifest.d.ts +16 -0
  82. package/dist/plugin/manifest.js +246 -0
  83. package/dist/plugin/manifest.mjs +185 -0
  84. package/dist/plugin/normalize-extra-options.d.ts +8 -0
  85. package/dist/plugin/normalize-extra-options.js +50 -0
  86. package/dist/plugin/normalize-extra-options.mjs +13 -0
  87. package/dist/plugin/normalize-options.d.ts +7 -0
  88. package/dist/plugin/normalize-options.js +139 -0
  89. package/dist/plugin/normalize-options.mjs +92 -0
  90. package/dist/plugin/resolver.d.ts +26 -0
  91. package/dist/plugin/resolver.js +203 -0
  92. package/dist/plugin/resolver.mjs +159 -0
  93. package/dist/plugin/rewrite-request.d.ts +14 -0
  94. package/dist/plugin/rewrite-request.js +89 -0
  95. package/dist/plugin/rewrite-request.mjs +42 -0
  96. package/dist/plugin/serializer.d.ts +6 -0
  97. package/dist/plugin/serializer.js +191 -0
  98. package/dist/plugin/serializer.mjs +144 -0
  99. package/dist/plugin/validate-options.d.ts +2 -0
  100. package/dist/plugin/validate-options.js +165 -0
  101. package/dist/plugin/validate-options.mjs +117 -0
  102. package/dist/runtime/host-entry.js +3 -0
  103. package/dist/runtime/init-host.js +31 -0
  104. package/dist/runtime/remote-entry.js +57 -0
  105. package/dist/runtime/remote-hmr.js +19 -0
  106. package/dist/runtime/remote-module-registry.js +57 -0
  107. package/dist/runtime/remote-module.js +2 -0
  108. package/dist/types.d.ts +26 -0
  109. package/dist/types.js +18 -0
  110. package/dist/types.mjs +1 -0
  111. package/dist/utils/errors.d.ts +8 -0
  112. package/dist/utils/errors.js +54 -0
  113. package/dist/utils/errors.mjs +14 -0
  114. package/dist/utils/federated-remote-types.d.ts +12 -0
  115. package/dist/utils/federated-remote-types.js +140 -0
  116. package/dist/utils/federated-remote-types.mjs +90 -0
  117. package/dist/utils/index.d.ts +2 -0
  118. package/dist/utils/index.js +47 -0
  119. package/dist/utils/index.mjs +3 -0
  120. package/dist/utils/metro-compat.d.ts +23 -0
  121. package/dist/utils/metro-compat.js +74 -0
  122. package/dist/utils/metro-compat.mjs +26 -0
  123. package/dist/utils/vm-manager.d.ts +21 -0
  124. package/dist/utils/vm-manager.js +118 -0
  125. package/dist/utils/vm-manager.mjs +70 -0
  126. package/package.json +106 -0
@@ -0,0 +1,58 @@
1
+ import "node:module";
2
+ import node_fs from "node:fs";
3
+ import node_path from "node:path";
4
+ import { TMP_DIR_NAME } from "./constants.mjs";
5
+ function isUsingMFCommand(command = process.argv[2]) {
6
+ const allowedCommands = [
7
+ 'start',
8
+ 'bundle-mf-host',
9
+ 'bundle-mf-remote'
10
+ ];
11
+ return allowedCommands.includes(command);
12
+ }
13
+ function isUsingMFBundleCommand(command = process.argv[2]) {
14
+ const allowedCommands = [
15
+ 'bundle-mf-host',
16
+ 'bundle-mf-remote'
17
+ ];
18
+ return allowedCommands.includes(command);
19
+ }
20
+ function replaceExtension(filepath, extension) {
21
+ const separatorIndex = Math.max(filepath.lastIndexOf('/'), filepath.lastIndexOf('\\'));
22
+ const dotIndex = filepath.lastIndexOf('.');
23
+ const extensionStart = dotIndex > separatorIndex + 1 ? dotIndex : filepath.length;
24
+ return `${filepath.slice(0, extensionStart)}${extension}`;
25
+ }
26
+ function removeExtension(filepath) {
27
+ return replaceExtension(filepath, '');
28
+ }
29
+ function stubHostEntry(hostEntryPath) {
30
+ const stub = '// host entry stub';
31
+ node_fs.mkdirSync(node_path.dirname(hostEntryPath), {
32
+ recursive: true
33
+ });
34
+ node_fs.writeFileSync(hostEntryPath, stub, 'utf-8');
35
+ }
36
+ function stubRemoteEntry(remoteEntryPath) {
37
+ const stub = '// remote entry stub';
38
+ node_fs.mkdirSync(node_path.dirname(remoteEntryPath), {
39
+ recursive: true
40
+ });
41
+ node_fs.writeFileSync(remoteEntryPath, stub, 'utf-8');
42
+ }
43
+ function prepareTmpDir(projectRootPath) {
44
+ const nodeModulesPath = node_path.resolve(projectRootPath, 'node_modules');
45
+ const tmpDirPath = node_path.join(nodeModulesPath, TMP_DIR_NAME);
46
+ node_fs.rmSync(tmpDirPath, {
47
+ recursive: true,
48
+ force: true
49
+ });
50
+ node_fs.mkdirSync(tmpDirPath, {
51
+ recursive: true
52
+ });
53
+ return tmpDirPath;
54
+ }
55
+ function toPosixPath(value) {
56
+ return value.replaceAll('\\', '/');
57
+ }
58
+ export { isUsingMFBundleCommand, isUsingMFCommand, prepareTmpDir, removeExtension, replaceExtension, stubHostEntry, stubRemoteEntry, toPosixPath };
@@ -0,0 +1,11 @@
1
+ import type { ConfigT } from 'metro-config';
2
+ import type { ModuleFederationConfig, ModuleFederationConfigNormalized, ModuleFederationExtraOptions } from '../types';
3
+ import type { FederatedTypesMeta } from '../utils/federated-remote-types';
4
+ declare global {
5
+ var __METRO_FEDERATION_CONFIG: ModuleFederationConfigNormalized;
6
+ var __METRO_FEDERATION_ORIGINAL_ENTRY_PATH: string | undefined;
7
+ var __METRO_FEDERATION_REMOTE_ENTRY_PATH: string | undefined;
8
+ var __METRO_FEDERATION_MANIFEST_PATH: string | undefined;
9
+ var __METRO_FEDERATION_DTS_ASSETS: FederatedTypesMeta | undefined;
10
+ }
11
+ export declare function withModuleFederation(config: ConfigT, federationOptions: ModuleFederationConfig, extraOptions?: ModuleFederationExtraOptions): ConfigT;
@@ -0,0 +1,219 @@
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, getters, values)=>{
14
+ var define = (defs, kind)=>{
15
+ for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
16
+ enumerable: true,
17
+ [kind]: defs[key]
18
+ });
19
+ };
20
+ define(getters, "get");
21
+ define(values, "value");
22
+ };
23
+ })();
24
+ (()=>{
25
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
26
+ })();
27
+ (()=>{
28
+ __webpack_require__.r = (exports1)=>{
29
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
30
+ value: 'Module'
31
+ });
32
+ Object.defineProperty(exports1, '__esModule', {
33
+ value: true
34
+ });
35
+ };
36
+ })();
37
+ var __webpack_exports__ = {};
38
+ __webpack_require__.r(__webpack_exports__);
39
+ __webpack_require__.d(__webpack_exports__, {
40
+ withModuleFederation: ()=>withModuleFederation
41
+ });
42
+ const external_node_path_namespaceObject = require("node:path");
43
+ var external_node_path_default = /*#__PURE__*/ __webpack_require__.n(external_node_path_namespaceObject);
44
+ const external_node_fs_namespaceObject = require("node:fs");
45
+ const external_node_util_namespaceObject = require("node:util");
46
+ var external_node_util_default = /*#__PURE__*/ __webpack_require__.n(external_node_util_namespaceObject);
47
+ const index_js_namespaceObject = require("../utils/index.js");
48
+ const federated_remote_types_js_namespaceObject = require("../utils/federated-remote-types.js");
49
+ const external_babel_transformer_js_namespaceObject = require("./babel-transformer.js");
50
+ const external_helpers_js_namespaceObject = require("./helpers.js");
51
+ const external_manifest_js_namespaceObject = require("./manifest.js");
52
+ const external_manifest_middleware_js_namespaceObject = require("./manifest-middleware.js");
53
+ const external_normalize_extra_options_js_namespaceObject = require("./normalize-extra-options.js");
54
+ const external_normalize_options_js_namespaceObject = require("./normalize-options.js");
55
+ const external_resolver_js_namespaceObject = require("./resolver.js");
56
+ const external_rewrite_request_js_namespaceObject = require("./rewrite-request.js");
57
+ const external_serializer_js_namespaceObject = require("./serializer.js");
58
+ const external_validate_options_js_namespaceObject = require("./validate-options.js");
59
+ function withModuleFederation(config, federationOptions, extraOptions) {
60
+ if ((0, external_helpers_js_namespaceObject.isUsingMFCommand)()) return augmentConfig(config, federationOptions, extraOptions);
61
+ console.warn(external_node_util_default().styleText('yellow', 'Warning: Module Federation build is disabled for this command.\n') + external_node_util_default().styleText('yellow', 'To enable Module Federation, please use one of the dedicated bundle commands:\n') + ` ${external_node_util_default().styleText('dim', '•')} bundle-mf-host` + external_node_util_default().styleText('dim', ' - for bundling a host application\n') + ` ${external_node_util_default().styleText('dim', '•')} bundle-mf-remote` + external_node_util_default().styleText('dim', ' - for bundling a remote application\n'));
62
+ return config;
63
+ }
64
+ function augmentConfig(config, federationOptions, extraOptions) {
65
+ const isHost = !federationOptions.exposes;
66
+ const isRemote = !isHost;
67
+ const tmpDirPath = (0, external_helpers_js_namespaceObject.prepareTmpDir)(config.projectRoot);
68
+ (0, external_validate_options_js_namespaceObject.validateOptions)(federationOptions);
69
+ const options = (0, external_normalize_options_js_namespaceObject.normalizeOptions)(federationOptions, {
70
+ projectRoot: config.projectRoot,
71
+ tmpDirPath
72
+ });
73
+ const { flags } = (0, external_normalize_extra_options_js_namespaceObject.normalizeExtraOptions)(extraOptions);
74
+ const vmManager = new index_js_namespaceObject.VirtualModuleManager(config);
75
+ const { originalEntryFilename, originalEntryPath } = getOriginalEntry(config.projectRoot, 'index.js');
76
+ const hostEntryFilename = originalEntryFilename;
77
+ const hostEntryPath = external_node_path_default().resolve(tmpDirPath, hostEntryFilename);
78
+ const remoteEntryFilename = (0, external_helpers_js_namespaceObject.replaceExtension)(options.filename, '.js');
79
+ const remoteEntryPath = external_node_path_default().resolve(tmpDirPath, remoteEntryFilename);
80
+ const initHostPath = external_node_path_default().resolve(tmpDirPath, 'init-host.js');
81
+ const remoteHMRSetupPath = external_node_path_default().resolve(tmpDirPath, 'remote-hmr.js');
82
+ const remoteModuleRegistryPath = external_node_path_default().resolve(tmpDirPath, 'remote-module-registry.js');
83
+ const asyncRequirePath = require.resolve('../modules/asyncRequire.ts');
84
+ const babelTransformerPath = (0, external_babel_transformer_js_namespaceObject.createBabelTransformer)({
85
+ blacklistedPaths: [
86
+ initHostPath,
87
+ remoteEntryPath
88
+ ],
89
+ federationConfig: options,
90
+ originalBabelTransformerPath: config.transformer.babelTransformerPath,
91
+ tmpDirPath: tmpDirPath,
92
+ enableInitializeCorePatching: flags.unstable_patchInitializeCore,
93
+ enableRuntimeRequirePatching: flags.unstable_patchRuntimeRequire
94
+ });
95
+ const manifestOptions = {
96
+ projectRoot: config.projectRoot,
97
+ target: (0, external_helpers_js_namespaceObject.isUsingMFBundleCommand)() ? 'build' : 'development',
98
+ tmpDirPath
99
+ };
100
+ const manifestPath = (0, external_manifest_js_namespaceObject.createManifest)(options, tmpDirPath, manifestOptions);
101
+ (0, external_helpers_js_namespaceObject.stubHostEntry)(hostEntryPath);
102
+ (0, external_helpers_js_namespaceObject.stubRemoteEntry)(remoteEntryPath);
103
+ global.__METRO_FEDERATION_CONFIG = options;
104
+ global.__METRO_FEDERATION_HOST_ENTRY_PATH = hostEntryPath;
105
+ global.__METRO_FEDERATION_REMOTE_ENTRY_PATH = remoteEntryPath;
106
+ global.__METRO_FEDERATION_MANIFEST_PATH = manifestPath;
107
+ global.__METRO_FEDERATION_DTS_ASSETS = void 0;
108
+ maybeGenerateRemoteTypesForStart({
109
+ isRemote,
110
+ options,
111
+ projectRoot: config.projectRoot,
112
+ tmpDirPath,
113
+ manifestPath
114
+ });
115
+ return {
116
+ ...config,
117
+ serializer: {
118
+ ...config.serializer,
119
+ customSerializer: (0, external_serializer_js_namespaceObject.getModuleFederationSerializer)(options, (0, external_helpers_js_namespaceObject.isUsingMFBundleCommand)(), manifestPath, manifestOptions),
120
+ getModulesRunBeforeMainModule: (entryFilePath)=>{
121
+ if (flags.unstable_patchInitializeCore) return config.serializer.getModulesRunBeforeMainModule(entryFilePath);
122
+ if (isRemote) return [];
123
+ return [
124
+ initHostPath,
125
+ ...config.serializer.getModulesRunBeforeMainModule(entryFilePath)
126
+ ];
127
+ },
128
+ getRunModuleStatement: (moduleId)=>`${options.name}__r(${JSON.stringify(moduleId)});`,
129
+ getPolyfills: (options)=>isHost ? config.serializer.getPolyfills(options) : []
130
+ },
131
+ transformer: {
132
+ ...config.transformer,
133
+ globalPrefix: options.name,
134
+ babelTransformerPath: babelTransformerPath,
135
+ getTransformOptions: vmManager.getTransformOptions()
136
+ },
137
+ resolver: {
138
+ ...config.resolver,
139
+ resolveRequest: (0, external_resolver_js_namespaceObject.createResolveRequest)({
140
+ isRemote,
141
+ vmManager,
142
+ options,
143
+ paths: {
144
+ asyncRequire: asyncRequirePath,
145
+ originalEntry: originalEntryPath,
146
+ hostEntry: hostEntryPath,
147
+ initHost: initHostPath,
148
+ remoteModuleRegistry: remoteModuleRegistryPath,
149
+ remoteHMRSetup: remoteHMRSetupPath,
150
+ remoteEntry: remoteEntryPath,
151
+ projectDir: config.projectRoot,
152
+ tmpDir: tmpDirPath
153
+ },
154
+ hacks: {
155
+ patchHMRClient: flags.unstable_patchHMRClient,
156
+ patchInitializeCore: flags.unstable_patchInitializeCore
157
+ },
158
+ customResolver: config.resolver.resolveRequest
159
+ })
160
+ },
161
+ server: {
162
+ ...config.server,
163
+ enhanceMiddleware: (middleware, metroServer)=>{
164
+ const manifestMiddleware = (0, external_manifest_middleware_js_namespaceObject.createManifestMiddleware)({
165
+ federationConfig: options,
166
+ projectRoot: config.projectRoot,
167
+ remoteEntryPath,
168
+ tmpDirPath,
169
+ vmManager
170
+ })(middleware, metroServer);
171
+ return vmManager.getMiddleware()(manifestMiddleware, metroServer);
172
+ },
173
+ rewriteRequestUrl: (0, external_rewrite_request_js_namespaceObject.createRewriteRequest)({
174
+ config,
175
+ originalEntryFilename,
176
+ remoteEntryFilename,
177
+ manifestPath,
178
+ tmpDirPath,
179
+ getDtsAssetNames: ()=>global.__METRO_FEDERATION_DTS_ASSETS
180
+ })
181
+ }
182
+ };
183
+ }
184
+ function maybeGenerateRemoteTypesForStart(opts) {
185
+ if ('start' !== process.argv[2]) return;
186
+ if (!opts.isRemote || false === opts.options.dts) return;
187
+ (async ()=>{
188
+ try {
189
+ const typesMeta = await (0, federated_remote_types_js_namespaceObject.maybeGenerateFederatedRemoteTypes)({
190
+ federationConfig: opts.options,
191
+ projectRoot: opts.projectRoot,
192
+ outputDir: opts.tmpDirPath,
193
+ logger: console
194
+ });
195
+ if (!typesMeta) return;
196
+ global.__METRO_FEDERATION_DTS_ASSETS = typesMeta;
197
+ const manifest = JSON.parse(await external_node_fs_namespaceObject.promises.readFile(opts.manifestPath, 'utf-8'));
198
+ (0, federated_remote_types_js_namespaceObject.applyTypesMetaToManifest)(manifest, typesMeta);
199
+ await external_node_fs_namespaceObject.promises.writeFile(opts.manifestPath, JSON.stringify(manifest, void 0, 2), 'utf-8');
200
+ } catch (error) {
201
+ console.warn(`${external_node_util_default().styleText('yellow', 'Failed to generate federated types for dev server:')}\n${String(error)}`);
202
+ }
203
+ })();
204
+ }
205
+ function getOriginalEntry(projectRoot, entryFilename) {
206
+ const originalEntryFilename = external_node_path_default().basename(global.__METRO_FEDERATION_ORIGINAL_ENTRY_PATH ?? entryFilename);
207
+ const originalEntryPath = external_node_path_default().resolve(projectRoot, global.__METRO_FEDERATION_ORIGINAL_ENTRY_PATH ?? entryFilename);
208
+ return {
209
+ originalEntryFilename,
210
+ originalEntryPath
211
+ };
212
+ }
213
+ exports.withModuleFederation = __webpack_exports__.withModuleFederation;
214
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
215
+ "withModuleFederation"
216
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
217
+ Object.defineProperty(exports, '__esModule', {
218
+ value: true
219
+ });
@@ -0,0 +1,172 @@
1
+ import __rslib_shim_module__ from "node:module";
2
+ const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(/*#__PURE__*/ (()=>import.meta.url)());
3
+ import node_path from "node:path";
4
+ import { promises } from "node:fs";
5
+ import node_util from "node:util";
6
+ import { VirtualModuleManager } from "../utils/index.mjs";
7
+ import { applyTypesMetaToManifest, maybeGenerateFederatedRemoteTypes } from "../utils/federated-remote-types.mjs";
8
+ import { createBabelTransformer } from "./babel-transformer.mjs";
9
+ import { isUsingMFBundleCommand, isUsingMFCommand, prepareTmpDir, replaceExtension, stubHostEntry, stubRemoteEntry } from "./helpers.mjs";
10
+ import { createManifest } from "./manifest.mjs";
11
+ import { createManifestMiddleware } from "./manifest-middleware.mjs";
12
+ import { normalizeExtraOptions } from "./normalize-extra-options.mjs";
13
+ import { normalizeOptions } from "./normalize-options.mjs";
14
+ import { createResolveRequest } from "./resolver.mjs";
15
+ import { createRewriteRequest } from "./rewrite-request.mjs";
16
+ import { getModuleFederationSerializer } from "./serializer.mjs";
17
+ import { validateOptions } from "./validate-options.mjs";
18
+ function withModuleFederation(config, federationOptions, extraOptions) {
19
+ if (isUsingMFCommand()) return augmentConfig(config, federationOptions, extraOptions);
20
+ console.warn(node_util.styleText('yellow', 'Warning: Module Federation build is disabled for this command.\n') + node_util.styleText('yellow', 'To enable Module Federation, please use one of the dedicated bundle commands:\n') + ` ${node_util.styleText('dim', '•')} bundle-mf-host` + node_util.styleText('dim', ' - for bundling a host application\n') + ` ${node_util.styleText('dim', '•')} bundle-mf-remote` + node_util.styleText('dim', ' - for bundling a remote application\n'));
21
+ return config;
22
+ }
23
+ function augmentConfig(config, federationOptions, extraOptions) {
24
+ const isHost = !federationOptions.exposes;
25
+ const isRemote = !isHost;
26
+ const tmpDirPath = prepareTmpDir(config.projectRoot);
27
+ validateOptions(federationOptions);
28
+ const options = normalizeOptions(federationOptions, {
29
+ projectRoot: config.projectRoot,
30
+ tmpDirPath
31
+ });
32
+ const { flags } = normalizeExtraOptions(extraOptions);
33
+ const vmManager = new VirtualModuleManager(config);
34
+ const { originalEntryFilename, originalEntryPath } = getOriginalEntry(config.projectRoot, 'index.js');
35
+ const hostEntryFilename = originalEntryFilename;
36
+ const hostEntryPath = node_path.resolve(tmpDirPath, hostEntryFilename);
37
+ const remoteEntryFilename = replaceExtension(options.filename, '.js');
38
+ const remoteEntryPath = node_path.resolve(tmpDirPath, remoteEntryFilename);
39
+ const initHostPath = node_path.resolve(tmpDirPath, 'init-host.js');
40
+ const remoteHMRSetupPath = node_path.resolve(tmpDirPath, 'remote-hmr.js');
41
+ const remoteModuleRegistryPath = node_path.resolve(tmpDirPath, 'remote-module-registry.js');
42
+ const asyncRequirePath = require.resolve('../modules/asyncRequire.ts');
43
+ const babelTransformerPath = createBabelTransformer({
44
+ blacklistedPaths: [
45
+ initHostPath,
46
+ remoteEntryPath
47
+ ],
48
+ federationConfig: options,
49
+ originalBabelTransformerPath: config.transformer.babelTransformerPath,
50
+ tmpDirPath: tmpDirPath,
51
+ enableInitializeCorePatching: flags.unstable_patchInitializeCore,
52
+ enableRuntimeRequirePatching: flags.unstable_patchRuntimeRequire
53
+ });
54
+ const manifestOptions = {
55
+ projectRoot: config.projectRoot,
56
+ target: isUsingMFBundleCommand() ? 'build' : 'development',
57
+ tmpDirPath
58
+ };
59
+ const manifestPath = createManifest(options, tmpDirPath, manifestOptions);
60
+ stubHostEntry(hostEntryPath);
61
+ stubRemoteEntry(remoteEntryPath);
62
+ global.__METRO_FEDERATION_CONFIG = options;
63
+ global.__METRO_FEDERATION_HOST_ENTRY_PATH = hostEntryPath;
64
+ global.__METRO_FEDERATION_REMOTE_ENTRY_PATH = remoteEntryPath;
65
+ global.__METRO_FEDERATION_MANIFEST_PATH = manifestPath;
66
+ global.__METRO_FEDERATION_DTS_ASSETS = void 0;
67
+ maybeGenerateRemoteTypesForStart({
68
+ isRemote,
69
+ options,
70
+ projectRoot: config.projectRoot,
71
+ tmpDirPath,
72
+ manifestPath
73
+ });
74
+ return {
75
+ ...config,
76
+ serializer: {
77
+ ...config.serializer,
78
+ customSerializer: getModuleFederationSerializer(options, isUsingMFBundleCommand(), manifestPath, manifestOptions),
79
+ getModulesRunBeforeMainModule: (entryFilePath)=>{
80
+ if (flags.unstable_patchInitializeCore) return config.serializer.getModulesRunBeforeMainModule(entryFilePath);
81
+ if (isRemote) return [];
82
+ return [
83
+ initHostPath,
84
+ ...config.serializer.getModulesRunBeforeMainModule(entryFilePath)
85
+ ];
86
+ },
87
+ getRunModuleStatement: (moduleId)=>`${options.name}__r(${JSON.stringify(moduleId)});`,
88
+ getPolyfills: (options)=>isHost ? config.serializer.getPolyfills(options) : []
89
+ },
90
+ transformer: {
91
+ ...config.transformer,
92
+ globalPrefix: options.name,
93
+ babelTransformerPath: babelTransformerPath,
94
+ getTransformOptions: vmManager.getTransformOptions()
95
+ },
96
+ resolver: {
97
+ ...config.resolver,
98
+ resolveRequest: createResolveRequest({
99
+ isRemote,
100
+ vmManager,
101
+ options,
102
+ paths: {
103
+ asyncRequire: asyncRequirePath,
104
+ originalEntry: originalEntryPath,
105
+ hostEntry: hostEntryPath,
106
+ initHost: initHostPath,
107
+ remoteModuleRegistry: remoteModuleRegistryPath,
108
+ remoteHMRSetup: remoteHMRSetupPath,
109
+ remoteEntry: remoteEntryPath,
110
+ projectDir: config.projectRoot,
111
+ tmpDir: tmpDirPath
112
+ },
113
+ hacks: {
114
+ patchHMRClient: flags.unstable_patchHMRClient,
115
+ patchInitializeCore: flags.unstable_patchInitializeCore
116
+ },
117
+ customResolver: config.resolver.resolveRequest
118
+ })
119
+ },
120
+ server: {
121
+ ...config.server,
122
+ enhanceMiddleware: (middleware, metroServer)=>{
123
+ const manifestMiddleware = createManifestMiddleware({
124
+ federationConfig: options,
125
+ projectRoot: config.projectRoot,
126
+ remoteEntryPath,
127
+ tmpDirPath,
128
+ vmManager
129
+ })(middleware, metroServer);
130
+ return vmManager.getMiddleware()(manifestMiddleware, metroServer);
131
+ },
132
+ rewriteRequestUrl: createRewriteRequest({
133
+ config,
134
+ originalEntryFilename,
135
+ remoteEntryFilename,
136
+ manifestPath,
137
+ tmpDirPath,
138
+ getDtsAssetNames: ()=>global.__METRO_FEDERATION_DTS_ASSETS
139
+ })
140
+ }
141
+ };
142
+ }
143
+ function maybeGenerateRemoteTypesForStart(opts) {
144
+ if ('start' !== process.argv[2]) return;
145
+ if (!opts.isRemote || false === opts.options.dts) return;
146
+ (async ()=>{
147
+ try {
148
+ const typesMeta = await maybeGenerateFederatedRemoteTypes({
149
+ federationConfig: opts.options,
150
+ projectRoot: opts.projectRoot,
151
+ outputDir: opts.tmpDirPath,
152
+ logger: console
153
+ });
154
+ if (!typesMeta) return;
155
+ global.__METRO_FEDERATION_DTS_ASSETS = typesMeta;
156
+ const manifest = JSON.parse(await promises.readFile(opts.manifestPath, 'utf-8'));
157
+ applyTypesMetaToManifest(manifest, typesMeta);
158
+ await promises.writeFile(opts.manifestPath, JSON.stringify(manifest, void 0, 2), 'utf-8');
159
+ } catch (error) {
160
+ console.warn(`${node_util.styleText('yellow', 'Failed to generate federated types for dev server:')}\n${String(error)}`);
161
+ }
162
+ })();
163
+ }
164
+ function getOriginalEntry(projectRoot, entryFilename) {
165
+ const originalEntryFilename = node_path.basename(global.__METRO_FEDERATION_ORIGINAL_ENTRY_PATH ?? entryFilename);
166
+ const originalEntryPath = node_path.resolve(projectRoot, global.__METRO_FEDERATION_ORIGINAL_ENTRY_PATH ?? entryFilename);
167
+ return {
168
+ originalEntryFilename,
169
+ originalEntryPath
170
+ };
171
+ }
172
+ export { withModuleFederation };
@@ -0,0 +1,12 @@
1
+ import type { ServerConfigT } from 'metro-config';
2
+ import type { ModuleFederationConfigNormalized } from '../types';
3
+ import type { VirtualModuleManager } from '../utils';
4
+ type ManifestMiddlewareOptions = {
5
+ federationConfig: ModuleFederationConfigNormalized;
6
+ projectRoot: string;
7
+ remoteEntryPath: string;
8
+ tmpDirPath: string;
9
+ vmManager: Pick<VirtualModuleManager, 'registerVirtualModule'>;
10
+ };
11
+ export declare function createManifestMiddleware({ federationConfig, projectRoot, remoteEntryPath, tmpDirPath, vmManager, }: ManifestMiddlewareOptions): NonNullable<ServerConfigT['enhanceMiddleware']>;
12
+ export {};