@module-federation/esbuild 0.0.95 → 0.0.97

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 (61) hide show
  1. package/dist/adapters/lib/collect-exports.js +151 -0
  2. package/dist/adapters/lib/collect-exports.mjs +87 -0
  3. package/dist/adapters/lib/commonjs.js +260 -0
  4. package/dist/adapters/lib/commonjs.mjs +209 -0
  5. package/dist/adapters/lib/containerPlugin.js +232 -0
  6. package/dist/adapters/lib/containerPlugin.mjs +182 -0
  7. package/dist/adapters/lib/containerReference.js +219 -0
  8. package/dist/adapters/lib/containerReference.mjs +154 -0
  9. package/dist/adapters/lib/lexer.js +184 -0
  10. package/dist/adapters/lib/lexer.mjs +138 -0
  11. package/dist/adapters/lib/linkRemotesPlugin.js +115 -0
  12. package/dist/adapters/lib/linkRemotesPlugin.mjs +50 -0
  13. package/dist/adapters/lib/manifest.js +229 -0
  14. package/dist/adapters/lib/manifest.mjs +168 -0
  15. package/dist/adapters/lib/plugin.js +339 -0
  16. package/dist/adapters/lib/plugin.mjs +282 -0
  17. package/dist/adapters/lib/react-replacements.js +81 -0
  18. package/dist/adapters/lib/react-replacements.mjs +35 -0
  19. package/dist/adapters/lib/transform.js +169 -0
  20. package/dist/adapters/lib/transform.mjs +120 -0
  21. package/dist/adapters/lib/utils.js +83 -0
  22. package/dist/adapters/lib/utils.mjs +33 -0
  23. package/dist/build.js +131 -473
  24. package/dist/build.mjs +13 -380
  25. package/dist/index.js +41 -26
  26. package/dist/index.mjs +4 -1
  27. package/dist/lib/config/configuration-context.js +70 -0
  28. package/dist/lib/config/configuration-context.mjs +20 -0
  29. package/dist/lib/config/federation-config.js +31 -0
  30. package/dist/lib/config/federation-config.mjs +4 -0
  31. package/dist/lib/config/share-utils.js +339 -0
  32. package/dist/lib/config/share-utils.mjs +254 -0
  33. package/dist/lib/config/with-native-federation.js +118 -0
  34. package/dist/lib/config/with-native-federation.mjs +71 -0
  35. package/dist/lib/core/build-adapter.js +70 -0
  36. package/dist/lib/core/build-adapter.mjs +18 -0
  37. package/dist/lib/core/createContainerTemplate.js +231 -0
  38. package/dist/lib/core/createContainerTemplate.mjs +185 -0
  39. package/dist/lib/core/default-skip-list.js +99 -0
  40. package/dist/lib/core/default-skip-list.mjs +47 -0
  41. package/dist/lib/core/federation-options.js +31 -0
  42. package/dist/lib/core/federation-options.mjs +4 -0
  43. package/dist/lib/core/get-externals.js +65 -0
  44. package/dist/lib/core/get-externals.mjs +19 -0
  45. package/dist/lib/core/load-federation-config.js +69 -0
  46. package/dist/lib/core/load-federation-config.mjs +20 -0
  47. package/dist/lib/core/write-federation-info.js +65 -0
  48. package/dist/lib/core/write-federation-info.mjs +16 -0
  49. package/dist/lib/utils/logger.js +111 -0
  50. package/dist/lib/utils/logger.mjs +46 -0
  51. package/dist/lib/utils/mapped-paths.js +94 -0
  52. package/dist/lib/utils/mapped-paths.mjs +46 -0
  53. package/dist/lib/utils/normalize.js +63 -0
  54. package/dist/lib/utils/normalize.mjs +17 -0
  55. package/dist/lib/utils/package-info.js +324 -0
  56. package/dist/lib/utils/package-info.mjs +265 -0
  57. package/dist/plugin.d.ts +1 -0
  58. package/dist/plugin.js +79 -1231
  59. package/dist/plugin.mjs +2 -1187
  60. package/dist/resolve/esm-resolver.mjs +15 -22
  61. package/package.json +2 -2
@@ -0,0 +1,339 @@
1
+ "use strict";
2
+ const __rslib_import_meta_url__ = /*#__PURE__*/ (function () {
3
+ return typeof document === 'undefined'
4
+ ? new (require('url'.replace('', '')).URL)('file:' + __filename).href
5
+ : (document.currentScript && document.currentScript.src) ||
6
+ new URL('main.js', document.baseURI).href;
7
+ })();
8
+ ;
9
+ var __webpack_modules__ = ({
10
+ "esbuild": (function (module) {
11
+ module.exports = require("esbuild");
12
+
13
+ }),
14
+
15
+ });
16
+ /************************************************************************/
17
+ // The module cache
18
+ var __webpack_module_cache__ = {};
19
+
20
+ // The require function
21
+ function __webpack_require__(moduleId) {
22
+
23
+ // Check if module is in cache
24
+ var cachedModule = __webpack_module_cache__[moduleId];
25
+ if (cachedModule !== undefined) {
26
+ return cachedModule.exports;
27
+ }
28
+ // Create a new module (and put it into the cache)
29
+ var module = (__webpack_module_cache__[moduleId] = {
30
+ exports: {}
31
+ });
32
+ // Execute the module function
33
+ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
34
+
35
+ // Return the exports of the module
36
+ return module.exports;
37
+
38
+ }
39
+
40
+ /************************************************************************/
41
+ // webpack/runtime/compat_get_default_export
42
+ (() => {
43
+ // getDefaultExport function for compatibility with non-ESM modules
44
+ __webpack_require__.n = (module) => {
45
+ var getter = module && module.__esModule ?
46
+ () => (module['default']) :
47
+ () => (module);
48
+ __webpack_require__.d(getter, { a: getter });
49
+ return getter;
50
+ };
51
+
52
+ })();
53
+ // webpack/runtime/define_property_getters
54
+ (() => {
55
+ __webpack_require__.d = (exports, definition) => {
56
+ for(var key in definition) {
57
+ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
58
+ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
59
+ }
60
+ }
61
+ };
62
+ })();
63
+ // webpack/runtime/has_own_property
64
+ (() => {
65
+ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
66
+ })();
67
+ // webpack/runtime/make_namespace_object
68
+ (() => {
69
+ // define __esModule on exports
70
+ __webpack_require__.r = (exports) => {
71
+ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
72
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
73
+ }
74
+ Object.defineProperty(exports, '__esModule', { value: true });
75
+ };
76
+ })();
77
+ /************************************************************************/
78
+ var __webpack_exports__ = {};
79
+ // This entry needs to be wrapped in an IIFE because it needs to be isolated against other modules in the chunk.
80
+ (() => {
81
+ // ESM COMPAT FLAG
82
+ __webpack_require__.r(__webpack_exports__);
83
+
84
+ // EXPORTS
85
+ __webpack_require__.d(__webpack_exports__, {
86
+ createVirtualRemoteModule: () => (/* binding */ createVirtualRemoteModule),
87
+ createVirtualShareModule: () => (/* binding */ createVirtualShareModule),
88
+ moduleFederationPlugin: () => (/* binding */ moduleFederationPlugin)
89
+ });
90
+
91
+ ;// CONCATENATED MODULE: external "fs"
92
+ const external_fs_namespaceObject = require("fs");
93
+ var external_fs_default = /*#__PURE__*/__webpack_require__.n(external_fs_namespaceObject);
94
+ ;// CONCATENATED MODULE: external "./collect-exports.js"
95
+ const external_collect_exports_js_namespaceObject = require("./collect-exports.js");
96
+ ;// CONCATENATED MODULE: external "path"
97
+ const external_path_namespaceObject = require("path");
98
+ var external_path_default = /*#__PURE__*/__webpack_require__.n(external_path_namespaceObject);
99
+ ;// CONCATENATED MODULE: external "./manifest.js"
100
+ const external_manifest_js_namespaceObject = require("./manifest.js");
101
+ ;// CONCATENATED MODULE: external "./containerPlugin.js"
102
+ const external_containerPlugin_js_namespaceObject = require("./containerPlugin.js");
103
+ ;// CONCATENATED MODULE: external "./containerReference.js"
104
+ const external_containerReference_js_namespaceObject = require("./containerReference.js");
105
+ ;// CONCATENATED MODULE: external "./linkRemotesPlugin.js"
106
+ const external_linkRemotesPlugin_js_namespaceObject = require("./linkRemotesPlugin.js");
107
+ ;// CONCATENATED MODULE: external "./commonjs.js"
108
+ const external_commonjs_js_namespaceObject = require("./commonjs.js");
109
+ ;// CONCATENATED MODULE: external "../../lib/core/get-externals.js"
110
+ const get_externals_js_namespaceObject = require("../../lib/core/get-externals.js");
111
+ ;// CONCATENATED MODULE: ./src/adapters/lib/plugin.ts
112
+
113
+
114
+
115
+
116
+
117
+
118
+
119
+
120
+
121
+ // Creates a virtual module for sharing dependencies
122
+ const createVirtualShareModule = (name, ref, exports)=>`
123
+ const container = __FEDERATION__.__INSTANCES__.find(container => container.name === ${JSON.stringify(name)}) || __FEDERATION__.__INSTANCES__[0]
124
+
125
+ const mfLsZJ92 = await container.loadShare(${JSON.stringify(ref)})
126
+
127
+ ${exports.map((e)=>e === 'default' ? `export default mfLsZJ92.default` : `export const ${e} = mfLsZJ92[${JSON.stringify(e)}];`).join('\n')}
128
+ `;
129
+ const createVirtualRemoteModule = (name, ref)=>`
130
+ export * from ${JSON.stringify('federationRemote/' + ref)}
131
+ `;
132
+ // Plugin to transform CommonJS modules to ESM
133
+ const cjsToEsmPlugin = {
134
+ name: 'cjs-to-esm',
135
+ setup (build) {
136
+ build.onLoad({
137
+ filter: /.*/,
138
+ namespace: 'esm-shares'
139
+ }, async (args)=>{
140
+ var _build_initialOptions_external;
141
+ let esbuild_shim;
142
+ const require_esbuild = ()=>build.esbuild || (esbuild_shim ||= __webpack_require__("esbuild"));
143
+ const packageBuilder = await require_esbuild().build({
144
+ ...build.initialOptions,
145
+ external: (_build_initialOptions_external = build.initialOptions.external) === null || _build_initialOptions_external === void 0 ? void 0 : _build_initialOptions_external.filter((e)=>{
146
+ if (e.includes('*')) {
147
+ const prefix = e.split('*')[0];
148
+ return !args.path.startsWith(prefix);
149
+ }
150
+ return e !== args.path;
151
+ }),
152
+ entryPoints: [
153
+ args.path
154
+ ],
155
+ plugins: [
156
+ (0,external_commonjs_js_namespaceObject.commonjs)({
157
+ filter: /.*/
158
+ })
159
+ ],
160
+ write: false
161
+ });
162
+ return {
163
+ contents: packageBuilder.outputFiles[0].text,
164
+ loader: 'js',
165
+ resolveDir: args.pluginData.resolveDir
166
+ };
167
+ });
168
+ }
169
+ };
170
+ // Plugin to link shared dependencies
171
+ const linkSharedPlugin = (config)=>({
172
+ name: 'linkShared',
173
+ setup (build) {
174
+ const filter = new RegExp(Object.keys(config.shared || {}).map((name)=>`${name}$`).join('|'));
175
+ build.onResolve({
176
+ filter
177
+ }, (args)=>{
178
+ if (args.namespace === 'esm-shares') return null;
179
+ return {
180
+ path: args.path,
181
+ namespace: 'virtual-share-module',
182
+ pluginData: {
183
+ kind: args.kind,
184
+ resolveDir: args.resolveDir
185
+ }
186
+ };
187
+ });
188
+ build.onResolve({
189
+ filter: /.*/,
190
+ namespace: 'esm-shares'
191
+ }, (args)=>{
192
+ if (filter.test(args.path)) {
193
+ return {
194
+ path: args.path,
195
+ namespace: 'virtual-share-module',
196
+ pluginData: {
197
+ kind: args.kind,
198
+ resolveDir: args.resolveDir
199
+ }
200
+ };
201
+ }
202
+ if (filter.test(args.importer)) {
203
+ return {
204
+ path: args.path,
205
+ namespace: 'esm-shares',
206
+ pluginData: {
207
+ kind: args.kind,
208
+ resolveDir: args.resolveDir
209
+ }
210
+ };
211
+ }
212
+ return undefined;
213
+ });
214
+ build.onResolve({
215
+ filter: /^federationShare/
216
+ }, async (args)=>({
217
+ path: args.path.replace('federationShare/', ''),
218
+ namespace: 'esm-shares',
219
+ pluginData: {
220
+ kind: args.kind,
221
+ resolveDir: args.resolveDir
222
+ }
223
+ }));
224
+ build.onLoad({
225
+ filter,
226
+ namespace: 'virtual-share-module'
227
+ }, async (args)=>{
228
+ const exp = await (0,external_collect_exports_js_namespaceObject.getExports)(args.path);
229
+ return {
230
+ contents: createVirtualShareModule(config.name, args.path, exp),
231
+ loader: 'js',
232
+ resolveDir: external_path_default().dirname(args.path)
233
+ };
234
+ });
235
+ }
236
+ });
237
+ // Main module federation plugin
238
+ const moduleFederationPlugin = (config)=>({
239
+ name: 'module-federation',
240
+ setup (build) {
241
+ build.initialOptions.metafile = true;
242
+ const externals = (0,get_externals_js_namespaceObject.getExternals)(config);
243
+ if (build.initialOptions.external) {
244
+ build.initialOptions.external = [
245
+ ...new Set([
246
+ ...build.initialOptions.external,
247
+ ...externals
248
+ ])
249
+ ];
250
+ } else {
251
+ build.initialOptions.external = externals;
252
+ }
253
+ const pluginStack = [];
254
+ const remotes = Object.keys(config.remotes || {}).length;
255
+ const shared = Object.keys(config.shared || {}).length;
256
+ const exposes = Object.keys(config.exposes || {}).length;
257
+ const entryPoints = build.initialOptions.entryPoints;
258
+ const filename = config.filename || 'remoteEntry.js';
259
+ if (remotes) {
260
+ pluginStack.push((0,external_linkRemotesPlugin_js_namespaceObject.linkRemotesPlugin)(config));
261
+ }
262
+ if (shared) {
263
+ pluginStack.push(linkSharedPlugin(config));
264
+ }
265
+ if (!entryPoints) {
266
+ build.initialOptions.entryPoints = [];
267
+ }
268
+ if (exposes) {
269
+ if (Array.isArray(entryPoints)) {
270
+ entryPoints.push(filename);
271
+ } else if (entryPoints && typeof entryPoints === 'object') {
272
+ entryPoints[filename] = filename;
273
+ } else {
274
+ build.initialOptions.entryPoints = [
275
+ filename
276
+ ];
277
+ }
278
+ }
279
+ [
280
+ (0,external_containerReference_js_namespaceObject.initializeHostPlugin)(config),
281
+ (0,external_containerPlugin_js_namespaceObject.createContainerPlugin)(config),
282
+ cjsToEsmPlugin,
283
+ ...pluginStack
284
+ ].forEach((plugin)=>plugin.setup(build));
285
+ build.onEnd(async (result)=>{
286
+ if (!result.metafile) return;
287
+ if (exposes) {
288
+ const exposedConfig = config.exposes || {};
289
+ const remoteFile = config.filename;
290
+ const exposedEntries = {};
291
+ const outputMapWithoutExt = Object.entries(result.metafile.outputs).reduce((acc, [chunkKey, chunkValue])=>{
292
+ //@ts-ignore
293
+ const { entryPoint } = chunkValue;
294
+ const key = entryPoint || chunkKey;
295
+ const trimKey = key.substring(0, key.lastIndexOf('.')) || key;
296
+ //@ts-ignore
297
+ acc[trimKey] = {
298
+ ...chunkValue,
299
+ chunk: chunkKey
300
+ };
301
+ return acc;
302
+ }, {});
303
+ for (const [expose, value] of Object.entries(exposedConfig)){
304
+ const exposedFound = //@ts-ignore
305
+ outputMapWithoutExt[value.replace('./', '')] || //@ts-ignore
306
+ outputMapWithoutExt[expose.replace('./', '')];
307
+ if (exposedFound) {
308
+ exposedEntries[expose] = {
309
+ entryPoint: exposedFound.entryPoint,
310
+ exports: exposedFound.exports
311
+ };
312
+ }
313
+ }
314
+ for (const [outputPath, value] of Object.entries(result.metafile.outputs)){
315
+ if (!value.entryPoint) continue;
316
+ if (!value.entryPoint.startsWith('container:')) continue;
317
+ if (!value.entryPoint.endsWith(remoteFile)) continue;
318
+ const container = external_fs_default().readFileSync(outputPath, 'utf-8');
319
+ const withExports = container.replace('"__MODULE_MAP__"', `${JSON.stringify(exposedEntries)}`).replace("'__MODULE_MAP__'", `${JSON.stringify(exposedEntries)}`);
320
+ external_fs_default().writeFileSync(outputPath, withExports, 'utf-8');
321
+ }
322
+ }
323
+ await (0,external_manifest_js_namespaceObject.writeRemoteManifest)(config, result);
324
+ console.log(`build ended with ${result.errors.length} errors`);
325
+ });
326
+ }
327
+ });
328
+
329
+ })();
330
+
331
+ exports.createVirtualRemoteModule = __webpack_exports__.createVirtualRemoteModule;
332
+ exports.createVirtualShareModule = __webpack_exports__.createVirtualShareModule;
333
+ exports.moduleFederationPlugin = __webpack_exports__.moduleFederationPlugin;
334
+ for(var __webpack_i__ in __webpack_exports__) {
335
+ if(["createVirtualRemoteModule","createVirtualShareModule","moduleFederationPlugin"].indexOf(__webpack_i__) === -1) {
336
+ exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
337
+ }
338
+ }
339
+ Object.defineProperty(exports, '__esModule', { value: true });
@@ -0,0 +1,282 @@
1
+ import * as __WEBPACK_EXTERNAL_MODULE_esbuild__ from "esbuild";
2
+ import fs from "fs";
3
+ import { getExports } from "./collect-exports.mjs";
4
+ import path from "path";
5
+ import { writeRemoteManifest } from "./manifest.mjs";
6
+ import { createContainerPlugin } from "./containerPlugin.mjs";
7
+ import { initializeHostPlugin } from "./containerReference.mjs";
8
+ import { linkRemotesPlugin } from "./linkRemotesPlugin.mjs";
9
+ import { commonjs } from "./commonjs.mjs";
10
+ import { getExternals } from "../../lib/core/get-externals.mjs";
11
+ var __webpack_modules__ = ({
12
+ "esbuild": (function (module) {
13
+
14
+ module.exports = __WEBPACK_EXTERNAL_MODULE_esbuild__;
15
+
16
+
17
+ }),
18
+
19
+ });
20
+ /************************************************************************/
21
+ // The module cache
22
+ var __webpack_module_cache__ = {};
23
+
24
+ // The require function
25
+ function __webpack_require__(moduleId) {
26
+
27
+ // Check if module is in cache
28
+ var cachedModule = __webpack_module_cache__[moduleId];
29
+ if (cachedModule !== undefined) {
30
+ return cachedModule.exports;
31
+ }
32
+ // Create a new module (and put it into the cache)
33
+ var module = (__webpack_module_cache__[moduleId] = {
34
+ exports: {}
35
+ });
36
+ // Execute the module function
37
+ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
38
+
39
+ // Return the exports of the module
40
+ return module.exports;
41
+
42
+ }
43
+
44
+ /************************************************************************/
45
+
46
+ ;// CONCATENATED MODULE: external "fs"
47
+
48
+ ;// CONCATENATED MODULE: external "./collect-exports.mjs"
49
+
50
+ ;// CONCATENATED MODULE: external "path"
51
+
52
+ ;// CONCATENATED MODULE: external "./manifest.mjs"
53
+
54
+ ;// CONCATENATED MODULE: external "./containerPlugin.mjs"
55
+
56
+ ;// CONCATENATED MODULE: external "./containerReference.mjs"
57
+
58
+ ;// CONCATENATED MODULE: external "./linkRemotesPlugin.mjs"
59
+
60
+ ;// CONCATENATED MODULE: external "./commonjs.mjs"
61
+
62
+ ;// CONCATENATED MODULE: external "../../lib/core/get-externals.mjs"
63
+
64
+ ;// CONCATENATED MODULE: ./src/adapters/lib/plugin.ts
65
+
66
+
67
+
68
+
69
+
70
+
71
+
72
+
73
+
74
+ // Creates a virtual module for sharing dependencies
75
+ const createVirtualShareModule = (name, ref, exports)=>`
76
+ const container = __FEDERATION__.__INSTANCES__.find(container => container.name === ${JSON.stringify(name)}) || __FEDERATION__.__INSTANCES__[0]
77
+
78
+ const mfLsZJ92 = await container.loadShare(${JSON.stringify(ref)})
79
+
80
+ ${exports.map((e)=>e === 'default' ? `export default mfLsZJ92.default` : `export const ${e} = mfLsZJ92[${JSON.stringify(e)}];`).join('\n')}
81
+ `;
82
+ const createVirtualRemoteModule = (name, ref)=>`
83
+ export * from ${JSON.stringify('federationRemote/' + ref)}
84
+ `;
85
+ // Plugin to transform CommonJS modules to ESM
86
+ const cjsToEsmPlugin = {
87
+ name: 'cjs-to-esm',
88
+ setup (build) {
89
+ build.onLoad({
90
+ filter: /.*/,
91
+ namespace: 'esm-shares'
92
+ }, async (args)=>{
93
+ var _build_initialOptions_external;
94
+ let esbuild_shim;
95
+ const require_esbuild = ()=>build.esbuild || (esbuild_shim ||= __webpack_require__("esbuild"));
96
+ const packageBuilder = await require_esbuild().build({
97
+ ...build.initialOptions,
98
+ external: (_build_initialOptions_external = build.initialOptions.external) === null || _build_initialOptions_external === void 0 ? void 0 : _build_initialOptions_external.filter((e)=>{
99
+ if (e.includes('*')) {
100
+ const prefix = e.split('*')[0];
101
+ return !args.path.startsWith(prefix);
102
+ }
103
+ return e !== args.path;
104
+ }),
105
+ entryPoints: [
106
+ args.path
107
+ ],
108
+ plugins: [
109
+ commonjs({
110
+ filter: /.*/
111
+ })
112
+ ],
113
+ write: false
114
+ });
115
+ return {
116
+ contents: packageBuilder.outputFiles[0].text,
117
+ loader: 'js',
118
+ resolveDir: args.pluginData.resolveDir
119
+ };
120
+ });
121
+ }
122
+ };
123
+ // Plugin to link shared dependencies
124
+ const linkSharedPlugin = (config)=>({
125
+ name: 'linkShared',
126
+ setup (build) {
127
+ const filter = new RegExp(Object.keys(config.shared || {}).map((name)=>`${name}$`).join('|'));
128
+ build.onResolve({
129
+ filter
130
+ }, (args)=>{
131
+ if (args.namespace === 'esm-shares') return null;
132
+ return {
133
+ path: args.path,
134
+ namespace: 'virtual-share-module',
135
+ pluginData: {
136
+ kind: args.kind,
137
+ resolveDir: args.resolveDir
138
+ }
139
+ };
140
+ });
141
+ build.onResolve({
142
+ filter: /.*/,
143
+ namespace: 'esm-shares'
144
+ }, (args)=>{
145
+ if (filter.test(args.path)) {
146
+ return {
147
+ path: args.path,
148
+ namespace: 'virtual-share-module',
149
+ pluginData: {
150
+ kind: args.kind,
151
+ resolveDir: args.resolveDir
152
+ }
153
+ };
154
+ }
155
+ if (filter.test(args.importer)) {
156
+ return {
157
+ path: args.path,
158
+ namespace: 'esm-shares',
159
+ pluginData: {
160
+ kind: args.kind,
161
+ resolveDir: args.resolveDir
162
+ }
163
+ };
164
+ }
165
+ return undefined;
166
+ });
167
+ build.onResolve({
168
+ filter: /^federationShare/
169
+ }, async (args)=>({
170
+ path: args.path.replace('federationShare/', ''),
171
+ namespace: 'esm-shares',
172
+ pluginData: {
173
+ kind: args.kind,
174
+ resolveDir: args.resolveDir
175
+ }
176
+ }));
177
+ build.onLoad({
178
+ filter,
179
+ namespace: 'virtual-share-module'
180
+ }, async (args)=>{
181
+ const exp = await getExports(args.path);
182
+ return {
183
+ contents: createVirtualShareModule(config.name, args.path, exp),
184
+ loader: 'js',
185
+ resolveDir: path.dirname(args.path)
186
+ };
187
+ });
188
+ }
189
+ });
190
+ // Main module federation plugin
191
+ const moduleFederationPlugin = (config)=>({
192
+ name: 'module-federation',
193
+ setup (build) {
194
+ build.initialOptions.metafile = true;
195
+ const externals = getExternals(config);
196
+ if (build.initialOptions.external) {
197
+ build.initialOptions.external = [
198
+ ...new Set([
199
+ ...build.initialOptions.external,
200
+ ...externals
201
+ ])
202
+ ];
203
+ } else {
204
+ build.initialOptions.external = externals;
205
+ }
206
+ const pluginStack = [];
207
+ const remotes = Object.keys(config.remotes || {}).length;
208
+ const shared = Object.keys(config.shared || {}).length;
209
+ const exposes = Object.keys(config.exposes || {}).length;
210
+ const entryPoints = build.initialOptions.entryPoints;
211
+ const filename = config.filename || 'remoteEntry.js';
212
+ if (remotes) {
213
+ pluginStack.push(linkRemotesPlugin(config));
214
+ }
215
+ if (shared) {
216
+ pluginStack.push(linkSharedPlugin(config));
217
+ }
218
+ if (!entryPoints) {
219
+ build.initialOptions.entryPoints = [];
220
+ }
221
+ if (exposes) {
222
+ if (Array.isArray(entryPoints)) {
223
+ entryPoints.push(filename);
224
+ } else if (entryPoints && typeof entryPoints === 'object') {
225
+ entryPoints[filename] = filename;
226
+ } else {
227
+ build.initialOptions.entryPoints = [
228
+ filename
229
+ ];
230
+ }
231
+ }
232
+ [
233
+ initializeHostPlugin(config),
234
+ createContainerPlugin(config),
235
+ cjsToEsmPlugin,
236
+ ...pluginStack
237
+ ].forEach((plugin)=>plugin.setup(build));
238
+ build.onEnd(async (result)=>{
239
+ if (!result.metafile) return;
240
+ if (exposes) {
241
+ const exposedConfig = config.exposes || {};
242
+ const remoteFile = config.filename;
243
+ const exposedEntries = {};
244
+ const outputMapWithoutExt = Object.entries(result.metafile.outputs).reduce((acc, [chunkKey, chunkValue])=>{
245
+ //@ts-ignore
246
+ const { entryPoint } = chunkValue;
247
+ const key = entryPoint || chunkKey;
248
+ const trimKey = key.substring(0, key.lastIndexOf('.')) || key;
249
+ //@ts-ignore
250
+ acc[trimKey] = {
251
+ ...chunkValue,
252
+ chunk: chunkKey
253
+ };
254
+ return acc;
255
+ }, {});
256
+ for (const [expose, value] of Object.entries(exposedConfig)){
257
+ const exposedFound = //@ts-ignore
258
+ outputMapWithoutExt[value.replace('./', '')] || //@ts-ignore
259
+ outputMapWithoutExt[expose.replace('./', '')];
260
+ if (exposedFound) {
261
+ exposedEntries[expose] = {
262
+ entryPoint: exposedFound.entryPoint,
263
+ exports: exposedFound.exports
264
+ };
265
+ }
266
+ }
267
+ for (const [outputPath, value] of Object.entries(result.metafile.outputs)){
268
+ if (!value.entryPoint) continue;
269
+ if (!value.entryPoint.startsWith('container:')) continue;
270
+ if (!value.entryPoint.endsWith(remoteFile)) continue;
271
+ const container = fs.readFileSync(outputPath, 'utf-8');
272
+ const withExports = container.replace('"__MODULE_MAP__"', `${JSON.stringify(exposedEntries)}`).replace("'__MODULE_MAP__'", `${JSON.stringify(exposedEntries)}`);
273
+ fs.writeFileSync(outputPath, withExports, 'utf-8');
274
+ }
275
+ }
276
+ await writeRemoteManifest(config, result);
277
+ console.log(`build ended with ${result.errors.length} errors`);
278
+ });
279
+ }
280
+ });
281
+
282
+ export { createVirtualRemoteModule, createVirtualShareModule, moduleFederationPlugin };