@module-federation/esbuild 0.0.97 → 0.0.99

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.
@@ -114,28 +114,38 @@ const buildFederationHost = (config)=>{
114
114
  const runtimePlugin = () => ({
115
115
  name: 'import-maps-plugin',
116
116
  async init(args) {
117
- const remotePrefetch = args.options.remotes.map(async (remote) => {
118
- console.log('remote', remote);
119
- if (remote.type === 'esm') {
120
- await import(remote.entry);
121
- }
122
- });
123
-
124
- await Promise.all(remotePrefetch);
125
- if (typeof moduleMap !== 'undefined') {
126
- const map = Object.keys(moduleMap).reduce((acc, expose) => {
127
- const importMap = importShim.getImportMap().imports;
128
- const key = args.origin.name + expose.replace('.', '');
129
- if (!importMap[key]) {
130
- const encodedModule = encodeInlineESM(
131
- createVirtualRemoteModule(args.origin.name, key, moduleMap[expose].exports)
132
- );
133
- acc[key] = encodedModule;
117
+ // Load all remotes and collect their moduleMaps
118
+ const remotesWithModuleMaps = await Promise.all(
119
+ args.options.remotes.map(async (remote) => {
120
+ console.log('remote', remote);
121
+ if (remote.type === 'esm') {
122
+ const remoteModule = await import(remote.entry);
123
+ return { remote, moduleMap: remoteModule.moduleMap };
124
+ }
125
+ return { remote, moduleMap: null };
126
+ })
127
+ );
128
+
129
+ // Build import map entries for all remote modules
130
+ const allImports = {};
131
+ for (const { remote, moduleMap } of remotesWithModuleMaps) {
132
+ if (moduleMap && typeof moduleMap === 'object') {
133
+ for (const expose of Object.keys(moduleMap)) {
134
+ const currentImportMap = importShim.getImportMap().imports;
135
+ // Use remote name + expose path (e.g., "mfe1/component")
136
+ const key = remote.name + expose.replace('.', '');
137
+ if (!currentImportMap[key] && !allImports[key]) {
138
+ const encodedModule = encodeInlineESM(
139
+ createVirtualRemoteModule(remote.name, key, moduleMap[expose].exports)
140
+ );
141
+ allImports[key] = encodedModule;
142
+ }
134
143
  }
135
- return acc;
136
- }, {});
144
+ }
145
+ }
137
146
 
138
- await importShim.addImportMap({ imports: map });
147
+ if (Object.keys(allImports).length > 0) {
148
+ await importShim.addImportMap({ imports: allImports });
139
149
  }
140
150
 
141
151
  return args;
@@ -151,6 +161,33 @@ const buildFederationHost = (config)=>{
151
161
 
152
162
  await Promise.all(mfHoZJ92.initializeSharing('default', 'version-first'));
153
163
 
164
+ // Ensure import maps are registered before any code that uses remotes runs
165
+ const remotesList = ${remoteConfigs};
166
+ const allImports = {};
167
+ for (const remote of remotesList) {
168
+ if (remote.type === 'esm') {
169
+ try {
170
+ const remoteModule = await import(remote.entry);
171
+ const moduleMap = remoteModule.moduleMap;
172
+ if (moduleMap && typeof moduleMap === 'object') {
173
+ for (const expose of Object.keys(moduleMap)) {
174
+ const key = remote.name + expose.replace('.', '');
175
+ if (!allImports[key]) {
176
+ const encodedModule = encodeInlineESM(
177
+ createVirtualRemoteModule(remote.name, key, moduleMap[expose].exports)
178
+ );
179
+ allImports[key] = encodedModule;
180
+ }
181
+ }
182
+ }
183
+ } catch (e) {
184
+ console.error('Failed to load remote:', remote.name, e);
185
+ }
186
+ }
187
+ }
188
+ if (Object.keys(allImports).length > 0) {
189
+ await importShim.addImportMap({ imports: allImports });
190
+ }
154
191
 
155
192
  `;
156
193
  };
@@ -56,28 +56,38 @@ const buildFederationHost = (config)=>{
56
56
  const runtimePlugin = () => ({
57
57
  name: 'import-maps-plugin',
58
58
  async init(args) {
59
- const remotePrefetch = args.options.remotes.map(async (remote) => {
60
- console.log('remote', remote);
61
- if (remote.type === 'esm') {
62
- await import(remote.entry);
63
- }
64
- });
65
-
66
- await Promise.all(remotePrefetch);
67
- if (typeof moduleMap !== 'undefined') {
68
- const map = Object.keys(moduleMap).reduce((acc, expose) => {
69
- const importMap = importShim.getImportMap().imports;
70
- const key = args.origin.name + expose.replace('.', '');
71
- if (!importMap[key]) {
72
- const encodedModule = encodeInlineESM(
73
- createVirtualRemoteModule(args.origin.name, key, moduleMap[expose].exports)
74
- );
75
- acc[key] = encodedModule;
59
+ // Load all remotes and collect their moduleMaps
60
+ const remotesWithModuleMaps = await Promise.all(
61
+ args.options.remotes.map(async (remote) => {
62
+ console.log('remote', remote);
63
+ if (remote.type === 'esm') {
64
+ const remoteModule = await import(remote.entry);
65
+ return { remote, moduleMap: remoteModule.moduleMap };
66
+ }
67
+ return { remote, moduleMap: null };
68
+ })
69
+ );
70
+
71
+ // Build import map entries for all remote modules
72
+ const allImports = {};
73
+ for (const { remote, moduleMap } of remotesWithModuleMaps) {
74
+ if (moduleMap && typeof moduleMap === 'object') {
75
+ for (const expose of Object.keys(moduleMap)) {
76
+ const currentImportMap = importShim.getImportMap().imports;
77
+ // Use remote name + expose path (e.g., "mfe1/component")
78
+ const key = remote.name + expose.replace('.', '');
79
+ if (!currentImportMap[key] && !allImports[key]) {
80
+ const encodedModule = encodeInlineESM(
81
+ createVirtualRemoteModule(remote.name, key, moduleMap[expose].exports)
82
+ );
83
+ allImports[key] = encodedModule;
84
+ }
76
85
  }
77
- return acc;
78
- }, {});
86
+ }
87
+ }
79
88
 
80
- await importShim.addImportMap({ imports: map });
89
+ if (Object.keys(allImports).length > 0) {
90
+ await importShim.addImportMap({ imports: allImports });
81
91
  }
82
92
 
83
93
  return args;
@@ -93,6 +103,33 @@ const buildFederationHost = (config)=>{
93
103
 
94
104
  await Promise.all(mfHoZJ92.initializeSharing('default', 'version-first'));
95
105
 
106
+ // Ensure import maps are registered before any code that uses remotes runs
107
+ const remotesList = ${remoteConfigs};
108
+ const allImports = {};
109
+ for (const remote of remotesList) {
110
+ if (remote.type === 'esm') {
111
+ try {
112
+ const remoteModule = await import(remote.entry);
113
+ const moduleMap = remoteModule.moduleMap;
114
+ if (moduleMap && typeof moduleMap === 'object') {
115
+ for (const expose of Object.keys(moduleMap)) {
116
+ const key = remote.name + expose.replace('.', '');
117
+ if (!allImports[key]) {
118
+ const encodedModule = encodeInlineESM(
119
+ createVirtualRemoteModule(remote.name, key, moduleMap[expose].exports)
120
+ );
121
+ allImports[key] = encodedModule;
122
+ }
123
+ }
124
+ }
125
+ } catch (e) {
126
+ console.error('Failed to load remote:', remote.name, e);
127
+ }
128
+ }
129
+ }
130
+ if (Object.keys(allImports).length > 0) {
131
+ await importShim.addImportMap({ imports: allImports });
132
+ }
96
133
 
97
134
  `;
98
135
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/esbuild",
3
- "version": "0.0.97",
3
+ "version": "0.0.99",
4
4
  "author": "Zack Jackson (@ScriptedAlchemy)",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -58,9 +58,17 @@
58
58
  "esbuild": "^0.25.0",
59
59
  "json5": "^2.2.3",
60
60
  "npmlog": "^7.0.1",
61
- "@module-federation/sdk": "2.1.0"
61
+ "@module-federation/runtime": "2.2.1",
62
+ "@module-federation/webpack-bundler-runtime": "2.2.1",
63
+ "@module-federation/sdk": "2.2.1"
62
64
  },
63
65
  "devDependencies": {
64
66
  "@rslib/core": "^0.12.4"
67
+ },
68
+ "scripts": {
69
+ "build": "rslib build",
70
+ "lint": "ESLINT_USE_FLAT_CONFIG=false pnpm exec eslint --ignore-pattern node_modules \"**/*.ts\" \"package.json\"",
71
+ "build-debug": "FEDERATION_DEBUG=true rslib build",
72
+ "pre-release": "pnpm run build"
65
73
  }
66
74
  }