@module-federation/nextjs-mf 5.3.0 → 5.3.2

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.
@@ -1,7 +1,1560 @@
1
1
  'use strict';
2
2
 
3
- var NextFederationPlugin = require('./NextFederationPlugin2.js');
3
+ var fs = require('fs');
4
+ var path = require('path');
5
+ var options = require('webpack/lib/container/options');
6
+ var utils = require('webpack/lib/sharing/utils');
4
7
 
8
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
5
9
 
10
+ var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
11
+ var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
12
+
13
+ // the share scope we attach by default
14
+ // in hosts we re-key them to prevent webpack moving the modules into their own chunks (cause eager error)
15
+ // in remote these are marked as import:false as we always expect the host to prove them
16
+ const DEFAULT_SHARE_SCOPE = {
17
+ react: {
18
+ singleton: true,
19
+ requiredVersion: false,
20
+ },
21
+ 'react/jsx-runtime': {
22
+ singleton: true,
23
+ requiredVersion: false,
24
+ },
25
+ 'react-dom': {
26
+ singleton: true,
27
+ requiredVersion: false,
28
+ },
29
+ 'next/dynamic': {
30
+ requiredVersion: false,
31
+ singleton: true,
32
+ },
33
+ 'styled-jsx': {
34
+ requiredVersion: false,
35
+ singleton: true,
36
+ },
37
+ 'next/link': {
38
+ requiredVersion: false,
39
+ singleton: true,
40
+ },
41
+ 'next/router': {
42
+ requiredVersion: false,
43
+ singleton: true,
44
+ },
45
+ 'next/script': {
46
+ requiredVersion: false,
47
+ singleton: true,
48
+ },
49
+ 'next/head': {
50
+ requiredVersion: false,
51
+ singleton: true,
52
+ },
53
+ };
54
+ // put host infront of any shared module key, so "hostreact"
55
+ const reKeyHostShared = (options) => {
56
+ return Object.entries({
57
+ ...(options || {}),
58
+ ...DEFAULT_SHARE_SCOPE,
59
+ }).reduce((acc, item) => {
60
+ const [itemKey, shareOptions] = item;
61
+
62
+ const shareKey = 'host' + (item.shareKey || itemKey);
63
+ acc[shareKey] = shareOptions;
64
+ if (!shareOptions.import) {
65
+ acc[shareKey].import = itemKey;
66
+ }
67
+ if (!shareOptions.shareKey) {
68
+ acc[shareKey].shareKey = itemKey;
69
+ }
70
+
71
+ if (DEFAULT_SHARE_SCOPE[itemKey]) {
72
+ acc[shareKey].packageName = itemKey;
73
+ }
74
+ return acc;
75
+ }, {});
76
+ };
77
+
78
+ // split the @ syntax into url and global
79
+ const extractUrlAndGlobal = (urlAndGlobal) => {
80
+ const index = urlAndGlobal.indexOf('@');
81
+ if (index <= 0 || index === urlAndGlobal.length - 1) {
82
+ throw new Error(`Invalid request "${urlAndGlobal}"`);
83
+ }
84
+ return [urlAndGlobal.substring(index + 1), urlAndGlobal.substring(0, index)];
85
+ };
86
+
87
+ // browser template to convert remote into promise new promise and use require.loadChunk to load the chunk
88
+ const generateRemoteTemplate = (url, global) => {
89
+ return `promise new Promise(function (resolve, reject) {
90
+ var __webpack_error__ = new Error();
91
+ if (typeof ${global} !== 'undefined') return resolve();
92
+ __webpack_require__.l(
93
+ ${JSON.stringify(url)},
94
+ function (event) {
95
+ if (typeof ${global} !== 'undefined') return resolve();
96
+ var errorType = event && (event.type === 'load' ? 'missing' : event.type);
97
+ var realSrc = event && event.target && event.target.src;
98
+ __webpack_error__.message =
99
+ 'Loading script failed.\\n(' + errorType + ': ' + realSrc + ')';
100
+ __webpack_error__.name = 'ScriptExternalLoadError';
101
+ __webpack_error__.type = errorType;
102
+ __webpack_error__.request = realSrc;
103
+ reject(__webpack_error__);
104
+ },
105
+ ${JSON.stringify(global)},
106
+ );
107
+ }).then(function () {
108
+ const proxy = {
109
+ get: ${global}.get,
110
+ init: function(shareScope) {
111
+ const handler = {
112
+ get(target, prop) {
113
+ if (target[prop]) {
114
+ Object.values(target[prop]).forEach(function(o) {
115
+ if(o.from === '_N_E') {
116
+ o.loaded = 1
117
+ }
118
+ })
119
+ }
120
+ return target[prop]
121
+ },
122
+ set(target, property, value, receiver) {
123
+ if (target[property]) {
124
+ return target[property]
125
+ }
126
+ target[property] = value
127
+ return true
128
+ }
129
+ }
130
+ try {
131
+ ${global}.init(new Proxy(shareScope, handler))
132
+ } catch (e) {
133
+
134
+ }
135
+ ${global}.__initialized = true
136
+ }
137
+ }
138
+ if (!${global}.__initialized) {
139
+ proxy.init()
140
+ }
141
+ return proxy
142
+ })`;
143
+ };
144
+
145
+ const parseShareOptions = (options$1) => {
146
+ const sharedOptions = options.parseOptions(
147
+ options$1.shared,
148
+ (item, key) => {
149
+ if (typeof item !== 'string')
150
+ throw new Error('Unexpected array in shared');
151
+ /** @type {SharedConfig} */
152
+ const config =
153
+ item === key || !utils.isRequiredVersion(item)
154
+ ? {
155
+ import: item,
156
+ }
157
+ : {
158
+ import: key,
159
+ requiredVersion: item,
160
+ };
161
+ return config;
162
+ },
163
+ (item) => item
164
+ );
165
+ return sharedOptions.reduce((acc, [key, options]) => {
166
+ acc[key] = {
167
+ import: options.import,
168
+ shareKey: options.shareKey || key,
169
+ shareScope: options.shareScope,
170
+ requiredVersion: options.requiredVersion,
171
+ strictVersion: options.strictVersion,
172
+ singleton: options.singleton,
173
+ packageName: options.packageName,
174
+ eager: options.eager,
175
+ };
176
+ return acc;
177
+ }, {});
178
+ };
179
+
180
+ // shared packages must be compiled into webpack bundle, not require() pass through
181
+ const internalizeSharedPackages = (options, compiler) => {
182
+ //TODO: should use this util for other areas where we read MF options from userland
183
+ if (!options.shared) {
184
+ return;
185
+ }
186
+ const sharedOptions = parseShareOptions(options);
187
+ // get share keys from user, filter out ones that need to be external
188
+ const internalizableKeys = Object.keys(sharedOptions).filter((key) => {
189
+ if (!DEFAULT_SHARE_SCOPE[key]) {
190
+ return true;
191
+ }
192
+ if (!DEFAULT_SHARE_SCOPE[sharedOptions[key].import]) {
193
+ return true;
194
+ }
195
+ });
196
+ // take original externals regex
197
+ const backupExternals = compiler.options.externals[0];
198
+ // if externals is a function (like when you're not running in serverless mode or creating a single build)
199
+ if (typeof backupExternals === 'function') {
200
+ // replace externals function with short-circuit, or fall back to original algo
201
+ compiler.options.externals[0] = (mod, callback) => {
202
+ if (!internalizableKeys.some((v) => mod.request.includes(v))) {
203
+ return backupExternals(mod, callback);
204
+ }
205
+ // bundle it
206
+ return Promise.resolve();
207
+ };
208
+ }
209
+ };
210
+
211
+ const externalizedShares = Object.entries(DEFAULT_SHARE_SCOPE).reduce(
212
+ (acc, item) => {
213
+ const [key, value] = item;
214
+ acc[key] = { ...value, import: false };
215
+ if (key === 'react/jsx-runtime') {
216
+ delete acc[key].import;
217
+ }
218
+ return acc;
219
+ },
220
+ {}
221
+ );
222
+
223
+ // determine output base path, derives .next folder location
224
+ const getOutputPath = (compiler) => {
225
+ const isServer = compiler.options.target !== 'client';
226
+ let outputPath = compiler.options.output.path.split(path__default["default"].sep);
227
+ const foundIndex = outputPath.findIndex((i) => {
228
+ return i === (isServer ? 'server' : 'static');
229
+ });
230
+ outputPath = outputPath
231
+ .slice(0, foundIndex > 0 ? foundIndex : outputPath.length)
232
+ .join(path__default["default"].sep);
233
+
234
+ return outputPath;
235
+ };
236
+
237
+ const removePlugins = [
238
+ 'NextJsRequireCacheHotReloader',
239
+ 'BuildManifestPlugin',
240
+ 'WellKnownErrorsPlugin',
241
+ 'WebpackBuildEventsPlugin',
242
+ 'HotModuleReplacementPlugin',
243
+ 'NextMiniCssExtractPlugin',
244
+ 'NextFederationPlugin',
245
+ 'CopyFilePlugin',
246
+ 'ProfilingPlugin',
247
+ 'DropClientPage',
248
+ 'ReactFreshWebpackPlugin',
249
+ ];
250
+
251
+ /**
252
+ * loadScript(baseURI, fileName, cb)
253
+ * loadScript(scriptUrl, cb)
254
+ */
255
+
256
+ var loadScript = `
257
+ function loadScript(url,cb,chunkID) {
258
+ var url;
259
+ var cb = arguments[arguments.length - 1];
260
+ if (typeof cb !== "function") {
261
+ throw new Error("last argument should be a function");
262
+ }
263
+ if (arguments.length === 2) {
264
+ url = arguments[0];
265
+ } else if (arguments.length === 3) {
266
+ url = new URL(arguments[1], arguments[0]).toString();
267
+ } else {
268
+ throw new Error("invalid number of arguments");
269
+ }
270
+ if(global.webpackChunkLoad){
271
+ global.webpackChunkLoad(url).then(function(resp){
272
+ return resp.text();
273
+ }).then(function(rawData){
274
+ cb(null, rawData);
275
+ }).catch(function(err){
276
+ console.error('Federated Chunk load failed', error);
277
+ return cb(error)
278
+ });
279
+ } else {
280
+ //TODO https support
281
+ let request = (url.startsWith('https') ? require('https') : require('http')).get(url, function (resp) {
282
+ if (resp.statusCode === 200) {
283
+ let rawData = '';
284
+ resp.setEncoding('utf8');
285
+ resp.on('data', chunk => {
286
+ rawData += chunk;
287
+ });
288
+ resp.on('end', () => {
289
+ cb(null, rawData);
290
+ });
291
+ } else {
292
+ cb(resp);
293
+ }
294
+ });
295
+ request.on('error', error => {
296
+ console.error('Federated Chunk load failed', error);
297
+ return cb(error)
298
+ });
299
+ }
300
+ }
301
+ `;
302
+
303
+ /*
304
+ MIT License http://www.opensource.org/licenses/mit-license.php
305
+ */
306
+
307
+ const RuntimeGlobals$1 = require('webpack/lib/RuntimeGlobals');
308
+ const RuntimeModule = require('webpack/lib/RuntimeModule');
309
+ const Template = require('webpack/lib/Template');
310
+ const compileBooleanMatcher = require('webpack/lib/util/compileBooleanMatcher');
311
+ const { getUndoPath } = require('webpack/lib/util/identifier');
312
+
313
+ class ReadFileChunkLoadingRuntimeModule extends RuntimeModule {
314
+ constructor(runtimeRequirements, options, context) {
315
+ super('readFile chunk loading', RuntimeModule.STAGE_ATTACH);
316
+ this.runtimeRequirements = runtimeRequirements;
317
+ this.options = options;
318
+ this.context = context;
319
+ }
320
+
321
+ /**
322
+ * @private
323
+ * @param {Chunk} chunk chunk
324
+ * @param {string} rootOutputDir root output directory
325
+ * @returns {string} generated code
326
+ */
327
+ _generateBaseUri(chunk, rootOutputDir) {
328
+ const options = chunk.getEntryOptions();
329
+ if (options && options.baseUri) {
330
+ return `${RuntimeGlobals$1.baseURI} = ${JSON.stringify(options.baseUri)};`;
331
+ }
332
+
333
+ return `${RuntimeGlobals$1.baseURI} = require("url").pathToFileURL(${
334
+ rootOutputDir
335
+ ? `__dirname + ${JSON.stringify('/' + rootOutputDir)}`
336
+ : '__filename'
337
+ });`;
338
+ }
339
+
340
+ /**
341
+ * @returns {string} runtime code
342
+ */
343
+ generate() {
344
+ // name in this context is always the current remote itself.
345
+ // this code below is in each webpack runtime, host and remotes
346
+ // remote entries handle their own loading of chunks, so i have fractal self awareness
347
+ // hosts will likely never run the http chunk loading runtime, they use fs.readFile
348
+ // remotes only use fs.readFile if we were to cache the chunks on disk after fetching - otherwise its always using http
349
+ // so for example, if im in hostA and require(remoteb/module) --> console.log of name in runtime code will return remoteb
350
+
351
+ const { baseURI, promiseBaseURI, remotes, name } = this.options;
352
+ const { webpack } = this.context;
353
+ const chunkHasJs =
354
+ (webpack && webpack.javascript.JavascriptModulesPlugin.chunkHasJs) ||
355
+ require('webpack/lib/javascript/JavascriptModulesPlugin').chunkHasJs;
356
+
357
+ // workaround for next.js
358
+ const getInitialChunkIds = (chunk, chunkGraph) => {
359
+ const initialChunkIds = new Set(chunk.ids);
360
+ for (const c of chunk.getAllInitialChunks()) {
361
+ if (c === chunk || chunkHasJs(c, chunkGraph)) continue;
362
+ for (const id of c.ids) initialChunkIds.add(id);
363
+ }
364
+ return initialChunkIds;
365
+ };
366
+
367
+ const { chunkGraph, chunk } = this;
368
+ const { runtimeTemplate } = this.compilation;
369
+ const fn = RuntimeGlobals$1.ensureChunkHandlers;
370
+ const withBaseURI = this.runtimeRequirements.has(RuntimeGlobals$1.baseURI);
371
+ const withExternalInstallChunk = this.runtimeRequirements.has(
372
+ RuntimeGlobals$1.externalInstallChunk
373
+ );
374
+ const withOnChunkLoad = this.runtimeRequirements.has(
375
+ RuntimeGlobals$1.onChunksLoaded
376
+ );
377
+ const withLoading = this.runtimeRequirements.has(
378
+ RuntimeGlobals$1.ensureChunkHandlers
379
+ );
380
+ const withHmr = this.runtimeRequirements.has(
381
+ RuntimeGlobals$1.hmrDownloadUpdateHandlers
382
+ );
383
+ const withHmrManifest = this.runtimeRequirements.has(
384
+ RuntimeGlobals$1.hmrDownloadManifest
385
+ );
386
+
387
+ const conditionMap = chunkGraph.getChunkConditionMap(chunk, chunkHasJs);
388
+ const hasJsMatcher = compileBooleanMatcher(conditionMap);
389
+ const initialChunkIds = getInitialChunkIds(chunk, chunkGraph);
390
+
391
+ const outputName = this.compilation.getPath(
392
+ (
393
+ (webpack &&
394
+ webpack.javascript.JavascriptModulesPlugin
395
+ .getChunkFilenameTemplate) ||
396
+ require('webpack/lib/javascript/JavascriptModulesPlugin')
397
+ .getChunkFilenameTemplate
398
+ )(chunk, this.compilation.outputOptions),
399
+ {
400
+ chunk,
401
+ contentHashType: 'javascript',
402
+ }
403
+ );
404
+ const rootOutputDir = getUndoPath(
405
+ outputName,
406
+ this.compilation.outputOptions.path,
407
+ false
408
+ );
409
+
410
+ const stateExpression = withHmr
411
+ ? `${RuntimeGlobals$1.hmrRuntimeStatePrefix}_readFileVm`
412
+ : undefined;
413
+
414
+ return Template.asString([
415
+ withBaseURI
416
+ ? this._generateBaseUri(chunk, rootOutputDir)
417
+ : '// no baseURI',
418
+ '',
419
+ '// object to store loaded chunks',
420
+ '// "0" means "already loaded", Promise means loading',
421
+ `var installedChunks = ${
422
+ stateExpression ? `${stateExpression} = ${stateExpression} || ` : ''
423
+ }{`,
424
+ Template.indent(
425
+ Array.from(initialChunkIds, (id) => `${JSON.stringify(id)}: 0`).join(
426
+ ',\n'
427
+ )
428
+ ),
429
+ '};',
430
+ '',
431
+ withOnChunkLoad
432
+ ? `${
433
+ RuntimeGlobals$1.onChunksLoaded
434
+ }.readFileVm = ${runtimeTemplate.returningFunction(
435
+ 'installedChunks[chunkId] === 0',
436
+ 'chunkId'
437
+ )};`
438
+ : '// no on chunks loaded',
439
+ '',
440
+ withLoading || withExternalInstallChunk
441
+ ? `var installChunk = ${runtimeTemplate.basicFunction('chunk', [
442
+ 'var moreModules = chunk.modules, chunkIds = chunk.ids, runtime = chunk.runtime;',
443
+ 'for(var moduleId in moreModules) {',
444
+ Template.indent([
445
+ `if(${RuntimeGlobals$1.hasOwnProperty}(moreModules, moduleId)) {`,
446
+ Template.indent([
447
+ `${RuntimeGlobals$1.moduleFactories}[moduleId] = moreModules[moduleId];`,
448
+ ]),
449
+ '}',
450
+ ]),
451
+ '}',
452
+ `if(runtime) runtime(__webpack_require__);`,
453
+ 'for(var i = 0; i < chunkIds.length; i++) {',
454
+ Template.indent([
455
+ 'if(installedChunks[chunkIds[i]]) {',
456
+ Template.indent(['installedChunks[chunkIds[i]][0]();']),
457
+ '}',
458
+ 'installedChunks[chunkIds[i]] = 0;',
459
+ ]),
460
+ '}',
461
+ withOnChunkLoad ? `${RuntimeGlobals$1.onChunksLoaded}();` : '',
462
+ ])};`
463
+ : '// no chunk install function needed',
464
+ '',
465
+ withLoading
466
+ ? Template.asString([
467
+ '// ReadFile + VM.run chunk loading for javascript',
468
+ `${fn}.readFileVm = function(chunkId, promises) {`,
469
+ hasJsMatcher !== false
470
+ ? Template.indent([
471
+ '',
472
+ 'var installedChunkData = installedChunks[chunkId];',
473
+ 'if(installedChunkData !== 0) { // 0 means "already installed".',
474
+ Template.indent([
475
+ '// array of [resolve, reject, promise] means "currently loading"',
476
+ 'if(installedChunkData) {',
477
+ Template.indent(['promises.push(installedChunkData[2]);']),
478
+ '} else {',
479
+ Template.indent([
480
+ hasJsMatcher === true
481
+ ? 'if(true) { // all chunks have JS'
482
+ : `if(${hasJsMatcher('chunkId')}) {`,
483
+ Template.indent([
484
+ '// load the chunk and return promise to it',
485
+ 'var promise = new Promise(async function(resolve, reject) {',
486
+ Template.indent([
487
+ 'installedChunkData = installedChunks[chunkId] = [resolve, reject];',
488
+ `var filename = require('path').join(__dirname, ${JSON.stringify(
489
+ rootOutputDir
490
+ )} + ${
491
+ RuntimeGlobals$1.getChunkScriptFilename
492
+ }(chunkId));`,
493
+ "var fs = require('fs');",
494
+ 'if(fs.existsSync(filename)) {',
495
+ Template.indent([
496
+ "fs.readFile(filename, 'utf-8', function(err, content) {",
497
+ Template.indent([
498
+ 'if(err) return reject(err);',
499
+ 'var chunk = {};',
500
+ "require('vm').runInThisContext('(function(exports, require, __dirname, __filename) {' + content + '\\n})', filename)" +
501
+ "(chunk, require, require('path').dirname(filename), filename);",
502
+ 'installChunk(chunk);',
503
+ ]),
504
+ '});',
505
+ ]),
506
+ '} else {',
507
+ Template.indent([
508
+ loadScript,
509
+
510
+ `console.log('needs to load remote module from', ${JSON.stringify(
511
+ name
512
+ )});`,
513
+ `console.log('remotes known to', ${JSON.stringify(
514
+ name
515
+ )}, ${JSON.stringify(remotes)})`,
516
+ // keys are mostly useless here, we want to find remote by its global (unique name)
517
+ `var remotes = ${JSON.stringify(
518
+ Object.values(remotes).reduce((acc, remote) => {
519
+ //TODO: need to handle all other cases like when remote is not a @ syntax string
520
+ const [global, url] = remote.split('@');
521
+ acc[global] = url;
522
+ return acc;
523
+ }, {})
524
+ )};`,
525
+ "Object.assign(global.__remote_scope__._config, remotes)",
526
+ "const remoteRegistry = global.__remote_scope__._config",
527
+ /* TODO: keying by global should be ok, but need to verify - need to deal with when user passes promise new promise()
528
+ global will/should still exist - but can only be known at runtime */
529
+ `console.log('remotes keyed by global name',remotes)`,
530
+ `console.log('remote scope configs',global.__remote_scope__._config)`,
531
+
532
+ `console.log('global.__remote_scope__',global.__remote_scope__)`,
533
+ `console.log('global.__remote_scope__[${JSON.stringify(
534
+ name
535
+ )}]',global.__remote_scope__[${JSON.stringify(
536
+ name
537
+ )}])`,
538
+
539
+ /* TODO: this global.REMOTE_CONFIG doesnt work in this v5 core, not sure if i need to keep it or not
540
+ not deleting it yet since i might need this for tracking all the remote entries across systems
541
+ for now, im going to use locally known remote scope from remoteEntry config
542
+ update: We will most likely need this, since remote would not have its own config
543
+ id need to access the host system and find the known url
544
+ basically this is how i create publicPath: auto on servers.
545
+ `var requestedRemote = global.REMOTE_CONFIG[${JSON.stringify(
546
+ name
547
+ )}]`,
548
+ */
549
+ "console.log('about to derive remote making request')",
550
+ `var requestedRemote = remoteRegistry[${JSON.stringify(
551
+ name
552
+ )}]`,
553
+ "console.log('requested remote', requestedRemote)",
554
+ /*TODO: we need to support when user implements own promise new promise function
555
+ for example i have my own promise remotes, not global@remotename
556
+ so there could be cases where remote may be function still - not sure */
557
+
558
+ /*TODO: need to handle if chunk fetch fails/crashes - ensure server still can keep loading
559
+ right now if you throw an error in here, server will stall forever */
560
+
561
+ `if(typeof requestedRemote === 'function'){
562
+ requestedRemote = await requestedRemote()
563
+ }`,
564
+ `console.log('var requestedRemote',requestedRemote);`,
565
+
566
+ // example: uncomment this and server will never reply
567
+ // `var scriptUrl = new URL(requestedRemote.split("@")[1]);`,
568
+ // since im looping over remote and creating global at build time, i dont need to split string at runtime
569
+ // there may still be a use case for that with promise new promise, depending on how we design it.
570
+ `var scriptUrl = new URL(requestedRemote);`,
571
+
572
+ `var chunkName = ${RuntimeGlobals$1.getChunkScriptFilename}(chunkId);`,
573
+
574
+ `console.log('chunkname to request',chunkName);`,
575
+ `var fileToReplace = require('path').basename(scriptUrl.pathname);`,
576
+ `scriptUrl.pathname = scriptUrl.pathname.replace(fileToReplace, chunkName);`,
577
+ `console.log('will load remote chunk', scriptUrl.toString());`,
578
+ `loadScript(scriptUrl.toString(), function(err, content) {`,
579
+ Template.indent([
580
+ "console.log('load script callback fired')",
581
+ "if(err) {console.error('error loading remote chunk', scriptUrl.toString(),'got',content); return reject(err);}",
582
+ 'var chunk = {};',
583
+ 'try {',
584
+ "require('vm').runInThisContext('(function(exports, require, __dirname, __filename) {' + content + '\\n})', filename)" +
585
+ "(chunk, require, require('path').dirname(filename), filename);",
586
+ '} catch (e) {',
587
+ "console.log('runInThisContext thew', e)",
588
+ '}',
589
+ 'installChunk(chunk);',
590
+ ]),
591
+ '});',
592
+ ]),
593
+ '}',
594
+ ]),
595
+ '});',
596
+ 'promises.push(installedChunkData[2] = promise);',
597
+ ]),
598
+ '} else installedChunks[chunkId] = 0;',
599
+ ]),
600
+ '}',
601
+ ]),
602
+ '}',
603
+ ])
604
+ : Template.indent(['installedChunks[chunkId] = 0;']),
605
+ '};',
606
+ ])
607
+ : '// no chunk loading',
608
+ '',
609
+ withExternalInstallChunk
610
+ ? Template.asString([
611
+ 'module.exports = __webpack_require__;',
612
+ `${RuntimeGlobals$1.externalInstallChunk} = installChunk;`,
613
+ ])
614
+ : '// no external install chunk',
615
+ '',
616
+ withHmr
617
+ ? Template.asString([
618
+ 'function loadUpdateChunk(chunkId, updatedModulesList) {',
619
+ Template.indent([
620
+ 'return new Promise(function(resolve, reject) {',
621
+ Template.indent([
622
+ `var filename = require('path').join(__dirname, ${JSON.stringify(
623
+ rootOutputDir
624
+ )} + ${RuntimeGlobals$1.getChunkUpdateScriptFilename}(chunkId));`,
625
+ "require('fs').readFile(filename, 'utf-8', function(err, content) {",
626
+ Template.indent([
627
+ 'if(err) return reject(err);',
628
+ 'var update = {};',
629
+ "require('vm').runInThisContext('(function(exports, require, __dirname, __filename) {' + content + '\\n})', filename)" +
630
+ "(update, require, require('path').dirname(filename), filename);",
631
+ 'var updatedModules = update.modules;',
632
+ 'var runtime = update.runtime;',
633
+ 'for(var moduleId in updatedModules) {',
634
+ Template.indent([
635
+ `if(${RuntimeGlobals$1.hasOwnProperty}(updatedModules, moduleId)) {`,
636
+ Template.indent([
637
+ `currentUpdate[moduleId] = updatedModules[moduleId];`,
638
+ 'if(updatedModulesList) updatedModulesList.push(moduleId);',
639
+ ]),
640
+ '}',
641
+ ]),
642
+ '}',
643
+ 'if(runtime) currentUpdateRuntime.push(runtime);',
644
+ 'resolve();',
645
+ ]),
646
+ '});',
647
+ ]),
648
+ '});',
649
+ ]),
650
+ '}',
651
+ '',
652
+ Template.getFunctionContent(
653
+ require('../hmr/JavascriptHotModuleReplacement.runtime.js')
654
+ )
655
+ .replace(/\$key\$/g, 'readFileVm')
656
+ .replace(/\$installedChunks\$/g, 'installedChunks')
657
+ .replace(/\$loadUpdateChunk\$/g, 'loadUpdateChunk')
658
+ .replace(/\$moduleCache\$/g, RuntimeGlobals$1.moduleCache)
659
+ .replace(/\$moduleFactories\$/g, RuntimeGlobals$1.moduleFactories)
660
+ .replace(
661
+ /\$ensureChunkHandlers\$/g,
662
+ RuntimeGlobals$1.ensureChunkHandlers
663
+ )
664
+ .replace(/\$hasOwnProperty\$/g, RuntimeGlobals$1.hasOwnProperty)
665
+ .replace(/\$hmrModuleData\$/g, RuntimeGlobals$1.hmrModuleData)
666
+ .replace(
667
+ /\$hmrDownloadUpdateHandlers\$/g,
668
+ RuntimeGlobals$1.hmrDownloadUpdateHandlers
669
+ )
670
+ .replace(
671
+ /\$hmrInvalidateModuleHandlers\$/g,
672
+ RuntimeGlobals$1.hmrInvalidateModuleHandlers
673
+ ),
674
+ ])
675
+ : '// no HMR',
676
+ '',
677
+ withHmrManifest
678
+ ? Template.asString([
679
+ `${RuntimeGlobals$1.hmrDownloadManifest} = function() {`,
680
+ Template.indent([
681
+ 'return new Promise(function(resolve, reject) {',
682
+ Template.indent([
683
+ `var filename = require('path').join(__dirname, ${JSON.stringify(
684
+ rootOutputDir
685
+ )} + ${RuntimeGlobals$1.getUpdateManifestFilename}());`,
686
+ "require('fs').readFile(filename, 'utf-8', function(err, content) {",
687
+ Template.indent([
688
+ 'if(err) {',
689
+ Template.indent([
690
+ 'if(err.code === "ENOENT") return resolve();',
691
+ 'return reject(err);',
692
+ ]),
693
+ '}',
694
+ 'try { resolve(JSON.parse(content)); }',
695
+ 'catch(e) { reject(e); }',
696
+ ]),
697
+ '});',
698
+ ]),
699
+ '});',
700
+ ]),
701
+ '}',
702
+ ])
703
+ : '// no HMR manifest',
704
+ ]);
705
+ }
706
+ }
707
+
708
+ const RuntimeGlobals = require('webpack/lib/RuntimeGlobals');
709
+ const StartupChunkDependenciesPlugin = require('webpack/lib/runtime/StartupChunkDependenciesPlugin');
710
+ // const ChunkLoadingRuntimeModule = require('webpack/lib/node/ReadFileChunkLoadingRuntimeModule')
711
+ class CommonJsChunkLoadingPlugin {
712
+ constructor(options) {
713
+ this.options = options || {};
714
+ this._asyncChunkLoading = this.options.asyncChunkLoading;
715
+ }
716
+
717
+ /**
718
+ * Apply the plugin
719
+ * @param {Compiler} compiler the compiler instance
720
+ * @returns {void}
721
+ */
722
+ apply(compiler) {
723
+ const chunkLoadingValue = this._asyncChunkLoading
724
+ ? 'async-node'
725
+ : 'require';
726
+ new StartupChunkDependenciesPlugin({
727
+ chunkLoading: chunkLoadingValue,
728
+ asyncChunkLoading: this._asyncChunkLoading,
729
+ }).apply(compiler);
730
+ compiler.hooks.thisCompilation.tap(
731
+ 'CommonJsChunkLoadingPlugin',
732
+ (compilation) => {
733
+ const onceForChunkSet = new WeakSet();
734
+ const handler = (chunk, set) => {
735
+ if (onceForChunkSet.has(chunk)) return;
736
+ onceForChunkSet.add(chunk);
737
+ set.add(RuntimeGlobals.moduleFactoriesAddOnly);
738
+ set.add(RuntimeGlobals.hasOwnProperty);
739
+ compilation.addRuntimeModule(
740
+ chunk,
741
+ new ReadFileChunkLoadingRuntimeModule(set, this.options, {
742
+ webpack: compiler.webpack,
743
+ })
744
+ );
745
+ };
746
+
747
+ compilation.hooks.runtimeRequirementInTree
748
+ .for(RuntimeGlobals.ensureChunkHandlers)
749
+ .tap('CommonJsChunkLoadingPlugin', handler);
750
+ compilation.hooks.runtimeRequirementInTree
751
+ .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
752
+ .tap('CommonJsChunkLoadingPlugin', handler);
753
+ compilation.hooks.runtimeRequirementInTree
754
+ .for(RuntimeGlobals.hmrDownloadManifest)
755
+ .tap('CommonJsChunkLoadingPlugin', handler);
756
+ compilation.hooks.runtimeRequirementInTree
757
+ .for(RuntimeGlobals.baseURI)
758
+ .tap('CommonJsChunkLoadingPlugin', handler);
759
+ compilation.hooks.runtimeRequirementInTree
760
+ .for(RuntimeGlobals.externalInstallChunk)
761
+ .tap('CommonJsChunkLoadingPlugin', handler);
762
+ compilation.hooks.runtimeRequirementInTree
763
+ .for(RuntimeGlobals.onChunksLoaded)
764
+ .tap('CommonJsChunkLoadingPlugin', handler);
765
+
766
+ compilation.hooks.runtimeRequirementInTree
767
+ .for(RuntimeGlobals.ensureChunkHandlers)
768
+ .tap('CommonJsChunkLoadingPlugin', (chunk, set) => {
769
+ set.add(RuntimeGlobals.getChunkScriptFilename);
770
+ });
771
+ compilation.hooks.runtimeRequirementInTree
772
+ .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
773
+ .tap('CommonJsChunkLoadingPlugin', (chunk, set) => {
774
+ set.add(RuntimeGlobals.getChunkUpdateScriptFilename);
775
+ set.add(RuntimeGlobals.moduleCache);
776
+ set.add(RuntimeGlobals.hmrModuleData);
777
+ set.add(RuntimeGlobals.moduleFactoriesAddOnly);
778
+ });
779
+ compilation.hooks.runtimeRequirementInTree
780
+ .for(RuntimeGlobals.hmrDownloadManifest)
781
+ .tap('CommonJsChunkLoadingPlugin', (chunk, set) => {
782
+ set.add(RuntimeGlobals.getUpdateManifestFilename);
783
+ });
784
+ }
785
+ );
786
+ }
787
+ }
788
+
789
+ class NodeSoftwareStreamRuntime {
790
+ constructor(options, context) {
791
+ this.options = options || {};
792
+ this.context = context || {};
793
+ }
794
+
795
+ apply(compiler) {
796
+ if (compiler.options.target) {
797
+ console.warn(
798
+ `target should be set to false while using NodeSoftwareStreamRuntime plugin, actual target: ${compiler.options.target}`
799
+ );
800
+ }
801
+
802
+ // When used with Next.js, context is needed to use Next.js webpack
803
+ const { webpack } = compiler;
804
+
805
+ // This will enable CommonJsChunkFormatPlugin
806
+ compiler.options.output.chunkFormat = 'commonjs';
807
+ // This will force async chunk loading
808
+ compiler.options.output.chunkLoading = 'async-node';
809
+ // Disable default config
810
+ compiler.options.output.enabledChunkLoadingTypes = false;
811
+
812
+ new ((webpack && webpack.node && webpack.node.NodeEnvironmentPlugin) ||
813
+ require('webpack/lib/node/NodeEnvironmentPlugin'))({
814
+ infrastructureLogging: compiler.options.infrastructureLogging,
815
+ }).apply(compiler);
816
+ new ((webpack && webpack.node && webpack.node.NodeTargetPlugin) ||
817
+ require('webpack/lib/node/NodeTargetPlugin'))().apply(compiler);
818
+ new CommonJsChunkLoadingPlugin({
819
+ asyncChunkLoading: true,
820
+ name: this.options.name,
821
+ remotes: this.options.remotes,
822
+ baseURI: compiler.options.output.publicPath,
823
+ promiseBaseURI: this.options.promiseBaseURI,
824
+ }).apply(compiler);
825
+ }
826
+ }
827
+
828
+ // possible remote evaluators
829
+ // this depends on the chunk format selected.
830
+ // commonjs2 - it think, is lazily evaluated - beware
831
+ // const remote = eval(scriptContent + '\n try{' + moduleName + '}catch(e) { null; };');
832
+ // commonjs - fine to use but exports marker doesnt exist
833
+ // const remote = eval('let exports = {};' + scriptContent + 'exports');
834
+ // commonjs-module, ideal since it returns a commonjs module format
835
+ // const remote = eval(scriptContent + 'module.exports')
836
+
837
+ // Note on ESM.
838
+ // Its possible to use ESM import, but its impossible to invalidate the module cache
839
+ // So once something is imported, its stuck. This is problematic with at-runtime since we want to hot reload node
840
+ // if ESM were used, youd be forced to restart the process to re-import modules or use a worker pool
841
+ // Workaround is possible with query string on end of request, but this leaks memory badly
842
+ // with commonjs, we can at least reset the require cache to "reboot" webpack runtime
843
+ // It *can* leak memory, but ive not been able to replicate this to an extent that would be concerning.
844
+ // ESM WILL leak memory, big difference.
845
+ // Im talking with TC39 about a proposal around "virtual module trees" which would solve many problems.
846
+ // VMT is like Realms but better - easiest analogy would be like forking the main thread, without going off main thread
847
+ // VMT allows for scope isolation, but still allows reflection and non-primitive memory pointers to be shared - perfect for MFP
848
+
849
+ //TODO: should use extractUrlAndGlobal from internal.js
850
+ //TODO: should use Template system like LoadFileChunk runtime does.
851
+ //TODO: should use vm.runInThisContext instead of eval
852
+ //TODO: global.webpackChunkLoad could use a better convention? I have to use a special http client to get out of my infra firewall
853
+ const executeLoadTemplate = `
854
+ function executeLoad(remoteUrl) {
855
+ console.log('remoteUrl',remoteUrl)
856
+ const scriptUrl = remoteUrl.split("@")[1];
857
+ const moduleName = remoteUrl.split("@")[0];
858
+ console.log("executing remote load", scriptUrl);
859
+ return new Promise(function (resolve, reject) {
860
+
861
+ (global.webpackChunkLoad || fetch)(scriptUrl).then(function(res){
862
+ return res.text();
863
+ }).then(function(scriptContent){
864
+ try {
865
+ const remote = eval(scriptContent + 'module.exports');
866
+ /* TODO: need something like a chunk loading queue, this can lead to async issues
867
+ if two containers load the same remote, they can overwrite global scope
868
+ should check someone is already loading remote and await that */
869
+ global.__remote_scope__[moduleName] = remote[moduleName] || remote
870
+ resolve(global.__remote_scope__[moduleName])
871
+ } catch(e) {
872
+ console.error('problem executing remote module', moduleName);
873
+ reject(e);
874
+ }
875
+ }).catch((e)=>{
876
+ console.error('failed to fetch remote', moduleName, scriptUrl);
877
+ console.error(e);
878
+ reject(null)
879
+ })
880
+ }).catch((e)=>{
881
+ console.error('error',e);
882
+ console.warn(moduleName,'is offline, returning fake remote')
883
+ return {
884
+ fake: true,
885
+ get:(arg)=>{
886
+ console.log('faking', arg,'module on', moduleName);
887
+
888
+ return ()=> Promise.resolve();
889
+ },
890
+ init:()=>{}
891
+ }
892
+ })
893
+ }
894
+ `;
895
+
896
+ function buildRemotes(mfConf, webpack) {
897
+ return Object.entries(mfConf.remotes || {}).reduce(
898
+ (acc, [name, config]) => {
899
+ // if its already been converted into promise, dont do it again
900
+ if(config.startsWith('promise ') || config.startsWith('external ')){
901
+ acc.buildTime[name] = config;
902
+ return acc;
903
+ }
904
+ /*
905
+ TODO: global remote scope object should go into webpack runtime as a runtime requirement
906
+ this can be done by referencing my LoadFile, CommonJs plugins in this directory.
907
+ */
908
+ const [global, url] = config.split('@');
909
+ const loadTemplate = `promise new Promise((resolve)=>{
910
+ if(!global.__remote_scope__) {
911
+ // create a global scope for container, similar to how remotes are set on window in the browser
912
+ global.__remote_scope__ = {
913
+ _config: {},
914
+ }
915
+ }
916
+
917
+ global.__remote_scope__._config[${JSON.stringify(global)}] = ${JSON.stringify(url)};
918
+
919
+ ${executeLoadTemplate}
920
+ resolve(executeLoad(${JSON.stringify(config)}))
921
+ }).then(remote=>{
922
+ console.log(remote);
923
+
924
+ return {
925
+ get: remote.get,
926
+ init: (args)=> {
927
+ console.log(args)
928
+ return remote.init(args)
929
+ }
930
+ }
931
+ });
932
+ `;
933
+ acc.buildTime[name] = loadTemplate;
934
+ return acc;
935
+ },
936
+ { runtime: {}, buildTime: {}, hot: {} }
937
+ );
938
+ }
939
+
940
+ class StreamingFederation {
941
+ constructor({ experiments, ...options }, context) {
942
+ this.options = options || {};
943
+ this.context = context || {};
944
+ this.experiments = experiments || {};
945
+ }
946
+
947
+ apply(compiler) {
948
+ // When used with Next.js, context is needed to use Next.js webpack
949
+ const { webpack } = compiler;
950
+
951
+ const { buildTime, runtime, hot } = buildRemotes(
952
+ this.options,
953
+ webpack || require('webpack')
954
+ );
955
+
956
+ // new ((webpack && webpack.DefinePlugin) || require("webpack").DefinePlugin)(
957
+ // defs
958
+ // ).apply(compiler);
959
+
960
+ const pluginOptions = {
961
+ ...this.options,
962
+ remotes: buildTime,
963
+ };
964
+
965
+ new (this.context.ModuleFederationPlugin || (webpack && webpack.container.ModuleFederationPlugin) ||
966
+ require('webpack/lib/container/ModuleFederationPlugin'))(
967
+ pluginOptions
968
+ ).apply(compiler);
969
+ }
970
+ }
971
+
972
+ /*
973
+ MIT License http://www.opensource.org/licenses/mit-license.php
974
+ Author Tobias Koppers @sokra and Zackary Jackson @ScriptedAlchemy
975
+ */
976
+ /*
977
+ plugin was copied over because ContainerPlugin is called in compiler.hooks.afterPlugins which doest seem to work in child compiler
978
+ */
979
+ /** @typedef {import("../../declarations/plugins/container/ModuleFederationPlugin").ExternalsType} ExternalsType */
980
+ /** @typedef {import("../../declarations/plugins/container/ModuleFederationPlugin").ModuleFederationPluginOptions} ModuleFederationPluginOptions */
981
+
982
+ /** @typedef {import("webpack").Shared} Shared */
983
+ /** @typedef {import("webpack").Compiler} Compiler */
984
+
985
+ class ModuleFederationPlugin {
986
+ /**
987
+ * @param {ModuleFederationPluginOptions} options options
988
+ */
989
+ constructor(options) {
990
+ this._options = options;
991
+ }
992
+
993
+ /**
994
+ * Apply the plugin
995
+ * @param {Compiler} compiler the compiler instance
996
+ * @returns {void}
997
+ */
998
+ apply(compiler) {
999
+ const { _options: options } = this;
1000
+ const webpack = compiler.webpack;
1001
+ const { ContainerPlugin, ContainerReferencePlugin } = webpack.container;
1002
+ const { SharePlugin } = webpack.sharing;
1003
+ const library = options.library || { type: 'var', name: options.name };
1004
+ const remoteType =
1005
+ options.remoteType ||
1006
+ (options.library && /** @type {ExternalsType} */ options.library.type) ||
1007
+ 'script';
1008
+ if (
1009
+ library &&
1010
+ !compiler.options.output.enabledLibraryTypes.includes(library.type)
1011
+ ) {
1012
+ compiler.options.output.enabledLibraryTypes.push(library.type);
1013
+ }
1014
+
1015
+ if (
1016
+ options.exposes &&
1017
+ (Array.isArray(options.exposes)
1018
+ ? options.exposes.length > 0
1019
+ : Object.keys(options.exposes).length > 0)
1020
+ ) {
1021
+ new ContainerPlugin({
1022
+ name: options.name,
1023
+ library,
1024
+ filename: options.filename,
1025
+ runtime: options.runtime,
1026
+ exposes: options.exposes,
1027
+ }).apply(compiler);
1028
+ }
1029
+ if (
1030
+ options.remotes &&
1031
+ (Array.isArray(options.remotes)
1032
+ ? options.remotes.length > 0
1033
+ : Object.keys(options.remotes).length > 0)
1034
+ ) {
1035
+ new ContainerReferencePlugin({
1036
+ remoteType,
1037
+ remotes: options.remotes,
1038
+ }).apply(compiler);
1039
+ }
1040
+ if (options.shared) {
1041
+ new SharePlugin({
1042
+ shared: options.shared,
1043
+ shareScope: options.shareScope,
1044
+ }).apply(compiler);
1045
+ }
1046
+ }
1047
+ }
1048
+
1049
+ /*
1050
+ MIT License http://www.opensource.org/licenses/mit-license.php
1051
+ Author Zackary Jackson @ScriptedAlchemy
1052
+ */
1053
+ const {
1054
+ injectRuleLoader,
1055
+ hasLoader,
1056
+ toDisplayErrors,
1057
+ } = require('./loaders/helpers');
1058
+ const { exposeNextjsPages } = require('./loaders/nextPageMapLoader');
1059
+ const DevHmrFixInvalidPongPlugin = require('./plugins/DevHmrFixInvalidPongPlugin');
1060
+
1061
+ const CHILD_PLUGIN_NAME = 'ChildFederationPlugin';
1062
+
1063
+ class RemoveRRRuntimePlugin {
1064
+ /**
1065
+ * Apply the plugin
1066
+ * @param {Compiler} compiler the compiler instance
1067
+ * @returns {void}
1068
+ */
1069
+ apply(compiler) {
1070
+ const webpack = compiler.webpack;
1071
+ // only impacts dev mode - dont waste the memory during prod build
1072
+ if (compiler.options.mode === 'development') {
1073
+ compiler.hooks.thisCompilation.tap(
1074
+ 'RemoveRRRuntimePlugin',
1075
+ (compilation) => {
1076
+ compilation.hooks.processAssets.tap(
1077
+ {
1078
+ name: 'RemoveRRRuntimePlugin',
1079
+ state:
1080
+ compilation.constructor.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE,
1081
+ },
1082
+ (assets) => {
1083
+ //can this be improved? I need react refresh not to cause global collision in dev mode
1084
+ Object.keys(assets).forEach((filename) => {
1085
+ if (filename.endsWith('.js') || filename.endsWith('.mjs')) {
1086
+ const asset = compilation.getAsset(filename);
1087
+ // easiest way to solve it is to prevent react refresh helpers from running when its a federated module chunk
1088
+ const newSource = asset.source
1089
+ .source()
1090
+ .replace(/RefreshHelpers/g, 'NoExist');
1091
+ const updatedAsset = new webpack.sources.RawSource(newSource);
1092
+
1093
+ if (asset) {
1094
+ compilation.updateAsset(filename, updatedAsset);
1095
+ } else {
1096
+ compilation.emitAsset(filename, updatedAsset);
1097
+ }
1098
+ }
1099
+ });
1100
+ }
1101
+ );
1102
+ }
1103
+ );
1104
+ }
1105
+ }
1106
+ }
1107
+
1108
+ const computeRemoteFilename = (isServer, filename) => {
1109
+ if (isServer && filename) {
1110
+ return path__default["default"].basename(filename);
1111
+ }
1112
+ return filename;
1113
+ };
1114
+ const childCompilers = {};
1115
+ class ChildFederationPlugin {
1116
+ constructor(options, extraOptions = {}) {
1117
+ this._options = options;
1118
+ this._extraOptions = extraOptions;
1119
+ }
1120
+ /**
1121
+ * Apply the plugin
1122
+ * @param {Compiler} compiler the compiler instance
1123
+ * @returns {void}
1124
+ */
1125
+ apply(compiler) {
1126
+ const webpack = compiler.webpack;
1127
+ const LibraryPlugin = webpack.library.EnableLibraryPlugin;
1128
+ const LoaderTargetPlugin = webpack.LoaderTargetPlugin;
1129
+ const library = compiler.options.output.library;
1130
+ const isServer = compiler.options.name === 'server';
1131
+ const isDev = compiler.options.mode === 'development';
1132
+ let outputPath;
1133
+ if (isDev && isServer) {
1134
+ outputPath = path__default["default"].join(getOutputPath(compiler), 'static/ssr');
1135
+ } else {
1136
+ if (isServer) {
1137
+ outputPath = path__default["default"].join(getOutputPath(compiler), 'static/ssr');
1138
+ } else {
1139
+ outputPath = compiler.options.output.path;
1140
+ }
1141
+ }
1142
+
1143
+ compiler.hooks.thisCompilation.tap(CHILD_PLUGIN_NAME, (compilation) => {
1144
+ const buildName = this._options.name;
1145
+ const childOutput = {
1146
+ ...compiler.options.output,
1147
+ path: outputPath,
1148
+ // path: deriveOutputPath(isServer, compiler.options.output.path),
1149
+ publicPath: 'auto',
1150
+ chunkLoadingGlobal: buildName + 'chunkLoader',
1151
+ uniqueName: buildName,
1152
+ library: {
1153
+ name: buildName,
1154
+ type: library.type,
1155
+ },
1156
+ chunkFilename: compiler.options.output.chunkFilename.replace(
1157
+ '.js',
1158
+ '-fed.js'
1159
+ ),
1160
+ filename: compiler.options.output.filename.replace('.js', '-fed.js'),
1161
+ };
1162
+
1163
+ // using ModuleFederationPlugin does not work, i had to fork because of afterPlugins hook on containerPlugin.
1164
+ const FederationPlugin = ModuleFederationPlugin;
1165
+
1166
+ const federationPluginOptions = {
1167
+ // library: {type: 'var', name: buildName},
1168
+ ...this._options,
1169
+ filename: computeRemoteFilename(isServer, this._options.filename),
1170
+ exposes: {
1171
+ ...this._options.exposes,
1172
+ ...(this._extraOptions.exposePages
1173
+ ? exposeNextjsPages(compiler.options.context)
1174
+ : {}),
1175
+ },
1176
+ runtime: false,
1177
+ shared: {
1178
+ ...(this._extraOptions.skipSharingNextInternals
1179
+ ? {}
1180
+ : externalizedShares),
1181
+ ...this._options.shared,
1182
+ },
1183
+ };
1184
+
1185
+ let plugins;
1186
+ if (compiler.options.name === 'client') {
1187
+ plugins = [
1188
+ new FederationPlugin(federationPluginOptions),
1189
+ new webpack.web.JsonpTemplatePlugin(childOutput),
1190
+ new LoaderTargetPlugin('web'),
1191
+ new LibraryPlugin(this._options.library.type),
1192
+ new webpack.DefinePlugin({
1193
+ 'process.env.REMOTES': createRuntimeVariables(
1194
+ this._options.remotes
1195
+ ),
1196
+ 'process.env.CURRENT_HOST': JSON.stringify(this._options.name),
1197
+ }),
1198
+ new AddRuntimeRequirementToPromiseExternal(),
1199
+ ];
1200
+ } else if (compiler.options.name === 'server') {
1201
+ plugins = [
1202
+ new StreamingFederation(federationPluginOptions, {ModuleFederationPlugin: FederationPlugin}),
1203
+ new webpack.node.NodeTemplatePlugin(childOutput),
1204
+ //TODO: Externals function needs to internalize any shared module for host and remote build
1205
+ new webpack.ExternalsPlugin(compiler.options.externalsType, [
1206
+ // next dynamic needs to be within webpack, cannot be externalized
1207
+ ...Object.keys(DEFAULT_SHARE_SCOPE).filter(
1208
+ (k) => k !== 'next/dynamic'
1209
+ ),
1210
+ 'react/jsx-runtime',
1211
+ 'react/jsx-dev-runtime',
1212
+ ]),
1213
+ // new LoaderTargetPlugin('async-node'),
1214
+ new NodeSoftwareStreamRuntime(federationPluginOptions, webpack),
1215
+ new LibraryPlugin(federationPluginOptions.library.type),
1216
+ // new webpack.DefinePlugin({
1217
+ // 'process.env.REMOTES': JSON.stringify(this._options.remotes),
1218
+ // 'process.env.CURRENT_HOST': JSON.stringify(this._options.name),
1219
+ // }),
1220
+ new AddRuntimeRequirementToPromiseExternal(),
1221
+ ];
1222
+ }
1223
+ const childCompiler = compilation.createChildCompiler(
1224
+ CHILD_PLUGIN_NAME,
1225
+ childOutput,
1226
+ plugins
1227
+ );
1228
+
1229
+ childCompiler.outputPath = outputPath;
1230
+ childCompiler.options.module.rules.forEach((rule) => {
1231
+ // next-image-loader fix which adds remote's hostname to the assets url
1232
+ if (
1233
+ this._extraOptions.enableImageLoaderFix &&
1234
+ hasLoader(rule, 'next-image-loader')
1235
+ ) {
1236
+ injectRuleLoader(rule, {
1237
+ loader: path__default["default"].resolve(__dirname, './loaders/fixImageLoader.js'),
1238
+ });
1239
+ }
1240
+
1241
+ // url-loader fix for which adds remote's hostname to the assets url
1242
+ if (
1243
+ this._extraOptions.enableUrlLoaderFix &&
1244
+ hasLoader(rule, 'url-loader')
1245
+ ) {
1246
+ injectRuleLoader({
1247
+ loader: path__default["default"].resolve(__dirname, './loaders/fixUrlLoader.js'),
1248
+ });
1249
+ }
1250
+ });
1251
+ childCompiler.options.experiments.lazyCompilation = false;
1252
+ childCompiler.options.optimization.runtimeChunk = false;
1253
+ // no custom chunk splitting should be derived from host (next)
1254
+ delete childCompiler.options.optimization.splitChunks;
1255
+ childCompiler.outputFileSystem = fs__default["default"];
1256
+
1257
+ if (compiler.options.optimization.minimize) {
1258
+ for (const minimizer of compiler.options.optimization.minimizer) {
1259
+ if (typeof minimizer === "function") {
1260
+ minimizer.call(childCompiler, childCompiler);
1261
+ } else if (minimizer !== "...") {
1262
+ minimizer.apply(childCompiler);
1263
+ }
1264
+ }
1265
+ }
1266
+
1267
+ new RemoveRRRuntimePlugin().apply(childCompiler);
1268
+
1269
+ const MiniCss = childCompiler.options.plugins.find((p) => {
1270
+ return p.constructor.name === 'NextMiniCssExtractPlugin';
1271
+ });
1272
+
1273
+ childCompiler.options.plugins = childCompiler.options.plugins.filter(
1274
+ (plugin) => !removePlugins.includes(plugin.constructor.name)
1275
+ );
1276
+
1277
+ if (MiniCss) {
1278
+ // grab mini-css and reconfigure it to avoid conflicts with host
1279
+ new MiniCss.constructor({
1280
+ ...MiniCss.options,
1281
+ filename: MiniCss.options.filename.replace('.css', '-fed.css'),
1282
+ chunkFilename: MiniCss.options.chunkFilename.replace(
1283
+ '.css',
1284
+ '-fed.css'
1285
+ ),
1286
+ }).apply(childCompiler);
1287
+ }
1288
+
1289
+
1290
+ // TODO: this can likely be deleted now, if running server child compiler under client is the best way to go
1291
+ // help wanted for all asset pipeline stuff below
1292
+ // let childAssets
1293
+ // if (isServer) {
1294
+ // childAssets = new Promise((resolve) => {
1295
+ // childCompiler.hooks.afterEmit.tap(
1296
+ // CHILD_PLUGIN_NAME,
1297
+ // (childCompilation) => {
1298
+ // console.log('after emit assets server');
1299
+ // resolve(childCompilation.assets);
1300
+ // }
1301
+ // );
1302
+ // });
1303
+ // } else {
1304
+ // if(isDev) {
1305
+ // childAssets = new Promise((resolve) => {
1306
+ // childCompiler.hooks.afterEmit.tap(
1307
+ // CHILD_PLUGIN_NAME,
1308
+ // (childCompilation) => {
1309
+ // resolve(childCompilation.assets);
1310
+ // }
1311
+ // );
1312
+ // });
1313
+ //
1314
+ // } else {
1315
+ //
1316
+ // TODO: improve this
1317
+ // childAssets = new Promise((resolve, reject) => {
1318
+ // fs.readdir(
1319
+ // path.join(childCompiler.context, '.next/ssr'),
1320
+ // function (err, files) {
1321
+ // if (err) {
1322
+ // reject('Unable to scan directory: ' + err);
1323
+ // return;
1324
+ // }
1325
+ //
1326
+ // const allFiles = files.map(function (file) {
1327
+ // return new Promise((res, rej) => {
1328
+ // fs.readFile(
1329
+ // path.join(childCompiler.context, '.next/ssr', file),
1330
+ // (err, data) => {
1331
+ // if (err) rej(err);
1332
+ // compilation.assets[path.join('static/ssr', file)] = new compiler.webpack.sources.RawSource(data)
1333
+ // res();
1334
+ // }
1335
+ // );
1336
+ // });
1337
+ // });
1338
+ // Promise.all(allFiles).then(resolve).catch(reject)
1339
+ // }
1340
+ // );
1341
+ // });
1342
+ // }
1343
+ // }
1344
+ // on main compiler add extra assets from server output to browser build
1345
+ // compilation.hooks.additionalAssets.tapPromise(CHILD_PLUGIN_NAME, () => {
1346
+ // console.log('additional hooks', compiler.options.name);
1347
+ // console.log('in additional assets hook for main build');
1348
+ // return childAssets
1349
+ // });
1350
+
1351
+ // cache the serer compiler instance, we will run the server child compiler during the client main compilation
1352
+ // we need to do this because i need access to data from the client build to inject into the server build
1353
+ // in prod builds, server build runs first, followed by client build
1354
+ // in dev, client build runs first, followed by server build
1355
+ childCompilers[compiler.options.name] = childCompiler;
1356
+
1357
+ if (isDev) {
1358
+ // in dev, run the compilers in the order they are created (client, server)
1359
+ childCompiler.run((err, stats) => {
1360
+ if (err) {
1361
+ compilation.errors.push(err);
1362
+ }
1363
+ if (stats && stats.hasErrors()) {
1364
+ compilation.errors.push(
1365
+ new Error(toDisplayErrors(stats.compilation.errors))
1366
+ );
1367
+ }
1368
+ });
1369
+ // in prod, if client
1370
+ } else if (!isServer) {
1371
+ // if ssr enabled and server in compiler cache
1372
+ if(childCompilers['server']) {
1373
+ //wrong hook for this
1374
+ // add hook for additional assets to prevent compile from sealing.
1375
+ compilation.hooks.additionalAssets.tapPromise(CHILD_PLUGIN_NAME, () => {
1376
+ return new Promise((res, rej) => {
1377
+ // run server child compilation during client main compilation
1378
+ childCompilers['server'].run((err, stats) => {
1379
+ if (err) {
1380
+ compilation.errors.push(err);
1381
+ rej();
1382
+ }
1383
+ if (stats && stats.hasErrors()) {
1384
+ compilation.errors.push(
1385
+ new Error(toDisplayErrors(stats.compilation.errors))
1386
+ );
1387
+ rej();
1388
+ }
1389
+ res();
1390
+ });
1391
+ });
1392
+ });
1393
+ }
1394
+ // run client child compiler like normal
1395
+ childCompiler.run((err, stats) => {
1396
+ if (err) {
1397
+ compilation.errors.push(err);
1398
+ }
1399
+ if (stats && stats.hasErrors()) {
1400
+ compilation.errors.push(
1401
+ new Error(toDisplayErrors(stats.compilation.errors))
1402
+ );
1403
+ }
1404
+ });
1405
+ }
1406
+ });
1407
+ }
1408
+ }
1409
+
1410
+ class AddRuntimeRequirementToPromiseExternal {
1411
+ /**
1412
+ * Apply the plugin
1413
+ * @param {Compiler} compiler the compiler instance
1414
+ * @returns {void}
1415
+ */
1416
+ apply(compiler) {
1417
+ compiler.hooks.compilation.tap(
1418
+ 'AddRuntimeRequirementToPromiseExternal',
1419
+ (compilation) => {
1420
+ const RuntimeGlobals = compiler.webpack.RuntimeGlobals;
1421
+ // if (compilation.outputOptions.trustedTypes) {
1422
+ compilation.hooks.additionalModuleRuntimeRequirements.tap(
1423
+ 'AddRuntimeRequirementToPromiseExternal',
1424
+ (module, set, context) => {
1425
+ if (module.externalType === 'promise') {
1426
+ set.add(RuntimeGlobals.loadScript);
1427
+ set.add(RuntimeGlobals.require);
1428
+ }
1429
+ }
1430
+ );
1431
+ // }
1432
+ }
1433
+ );
1434
+ }
1435
+ }
1436
+
1437
+ function createRuntimeVariables(remotes) {
1438
+ return Object.entries(remotes).reduce((acc, remote) => {
1439
+ // handle promise new promise and external new promise
1440
+ if (remote[1].startsWith('promise ') || remote[1].startsWith('external ')) {
1441
+ const promiseCall = remote[1]
1442
+ .replace('promise ', '')
1443
+ .replace('external ', '');
1444
+ acc[remote[0]] = `function() {
1445
+ return ${promiseCall}
1446
+ }`;
1447
+ return acc;
1448
+ }
1449
+ // if somehow its just the @ syntax or something else, pass it through
1450
+ acc[remote[0]] = JSON.stringify(remote[1]);
1451
+ return acc;
1452
+ }, {});
1453
+ }
1454
+
1455
+ class NextFederationPlugin {
1456
+ constructor(options) {
1457
+ const {extraOptions, ...mainOpts} = options;
1458
+ this._options = mainOpts;
1459
+ this._extraOptions = extraOptions || {};
1460
+ }
1461
+
1462
+ /**
1463
+ * Apply the plugin
1464
+ * @param {Compiler} compiler the compiler instance
1465
+ * @returns {void}
1466
+ */
1467
+ apply(compiler) {
1468
+ const isServer = compiler.options.name === 'server';
1469
+ const webpack = compiler.webpack;
1470
+ if (isServer) {
1471
+ console.error('[nextjs-mf] WARNING: SSR IS NOT FULLY SUPPORTED YET, Only use pluign on client builds');
1472
+ // target false because we use our own target for node env
1473
+ compiler.options.target = false;
1474
+ new NodeSoftwareStreamRuntime(this._options, webpack).apply(compiler);
1475
+ this._options.library = {};
1476
+ this._options.library.type = 'commonjs-module';
1477
+ this._options.library.name = this._options.name;
1478
+ // output remote to ssr if server
1479
+ this._options.filename = this._options.filename.replace(
1480
+ '/chunks',
1481
+ '/ssr'
1482
+ );
1483
+
1484
+ // should this be a plugin that we apply to the compiler?
1485
+ internalizeSharedPackages(this._options, compiler);
1486
+ } else {
1487
+ if (this._extraOptions.automaticPageStitching) {
1488
+ compiler.options.module.rules.push({
1489
+ test: /next[\\/]dist[\\/]client[\\/]page-loader\.js$/,
1490
+ loader: path__default["default"].resolve(__dirname, './loaders/patchNextClientPageLoader'),
1491
+ });
1492
+ }
1493
+ if (this._options.remotes) {
1494
+ const parsedRemotes = Object.entries(this._options.remotes).reduce(
1495
+ (acc, remote) => {
1496
+ if (remote[1].includes('@')) {
1497
+ const [url, global] = extractUrlAndGlobal(remote[1]);
1498
+ acc[remote[0]] = generateRemoteTemplate(url, global);
1499
+ return acc;
1500
+ }
1501
+ acc[remote[0]] = remote[1];
1502
+ return acc;
1503
+ },
1504
+ {}
1505
+ );
1506
+ this._options.remotes = parsedRemotes;
1507
+ }
1508
+ if (this._options.library) {
1509
+ console.error('[mf] you cannot set custom library');
1510
+ }
1511
+ this._options.library = {
1512
+ // assign remote name to object to avoid SWC mangling top level variable
1513
+ type: 'window',
1514
+ name: this._options.name,
1515
+ };
1516
+ }
1517
+ //todo runtime variable creation needs to be applied for server as well. this is just for client
1518
+ // todo: this needs to be refactored into something more comprehensive. this is just a quick fix
1519
+ new webpack.DefinePlugin({
1520
+ 'process.env.REMOTES': createRuntimeVariables(this._options.remotes),
1521
+ 'process.env.CURRENT_HOST': JSON.stringify(this._options.name),
1522
+ }).apply(compiler);
1523
+
1524
+
1525
+ const ModuleFederationPlugin = {
1526
+ client: webpack.container.ModuleFederationPlugin,
1527
+ server: StreamingFederation,
1528
+ }[compiler.options.name];
1529
+ // ignore edge runtime and middleware builds
1530
+ if (ModuleFederationPlugin) {
1531
+ const internalShare = reKeyHostShared(this._options.shared);
1532
+ const hostFederationPluginOptions = {
1533
+ ...this._options,
1534
+ exposes: {},
1535
+ shared: {
1536
+ noop: {
1537
+ import: 'data:text/javascript,module.exports = {};',
1538
+ requiredVersion: false,
1539
+ eager: true,
1540
+ version: '0',
1541
+ },
1542
+ ...internalShare,
1543
+ },
1544
+ };
1545
+
1546
+ compiler.options.optimization.chunkIds = 'named';
1547
+
1548
+ new ModuleFederationPlugin(hostFederationPluginOptions, {
1549
+ ModuleFederationPlugin,
1550
+ }).apply(compiler);
1551
+ new ChildFederationPlugin(this._options, this._extraOptions).apply(compiler);
1552
+ new AddRuntimeRequirementToPromiseExternal().apply(compiler);
1553
+ if (compiler.options.mode === 'development') {
1554
+ new DevHmrFixInvalidPongPlugin().apply(compiler);
1555
+ }
1556
+ }
1557
+ }
1558
+ }
6
1559
 
7
1560
  module.exports = NextFederationPlugin;