@module-federation/nextjs-mf 5.2.1 → 5.3.0
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.
- package/LICENSE +21 -0
- package/lib/NextFederationPlugin.js +1 -481
- package/lib/NextFederationPlugin2.js +536 -0
- package/lib/_virtual/UrlNode.js +8 -0
- package/lib/_virtual/_commonjsHelpers.js +26 -0
- package/lib/_virtual/_tslib.js +101 -0
- package/lib/_virtual/helpers.js +7 -0
- package/lib/_virtual/nextPageMapLoader.js +7 -0
- package/lib/client/CombinedPages.d.ts +28 -0
- package/lib/client/CombinedPages.d.ts.map +1 -0
- package/lib/client/CombinedPages.js +60 -0
- package/lib/client/MFClient.d.ts +70 -0
- package/lib/client/MFClient.d.ts.map +1 -0
- package/lib/client/MFClient.js +197 -0
- package/lib/client/RemoteContainer.d.ts +58 -0
- package/lib/client/RemoteContainer.d.ts.map +1 -0
- package/lib/client/RemoteContainer.js +161 -0
- package/lib/client/RemotePages.d.ts +48 -0
- package/lib/client/RemotePages.d.ts.map +1 -0
- package/lib/client/RemotePages.js +168 -0
- package/lib/client/UrlNode.d.ts +18 -0
- package/lib/client/UrlNode.d.ts.map +1 -0
- package/lib/client/UrlNode.js +162 -0
- package/lib/client/helpers.d.ts +17 -0
- package/lib/client/helpers.d.ts.map +1 -0
- package/lib/client/helpers.js +108 -0
- package/lib/client/useMFClient.d.ts +25 -0
- package/lib/client/useMFClient.d.ts.map +1 -0
- package/lib/client/useMFClient.js +79 -0
- package/lib/client/useMFRemote.d.ts +17 -0
- package/lib/client/useMFRemote.d.ts.map +1 -0
- package/lib/client/useMFRemote.js +72 -0
- package/lib/loaders/UrlNode.js +215 -0
- package/lib/loaders/fixImageLoader.js +42 -8
- package/lib/loaders/helpers.js +21 -2
- package/lib/loaders/nextPageMapLoader.js +81 -17
- package/lib/loaders/patchNextClientPageLoader.js +53 -0
- package/lib/plugins/DevHmrFixInvalidPongPlugin.js +65 -0
- package/lib/utils.js +7 -3
- package/package.json +21 -4
- package/tsconfig.json +33 -0
|
@@ -0,0 +1,536 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var require$$0 = require('path');
|
|
4
|
+
require('./loaders/helpers.js');
|
|
5
|
+
require('./loaders/nextPageMapLoader.js');
|
|
6
|
+
var DevHmrFixInvalidPongPlugin$1 = require('./plugins/DevHmrFixInvalidPongPlugin.js');
|
|
7
|
+
var helpers = require('./_virtual/helpers.js');
|
|
8
|
+
var nextPageMapLoader = require('./_virtual/nextPageMapLoader.js');
|
|
9
|
+
|
|
10
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
11
|
+
|
|
12
|
+
var require$$0__default = /*#__PURE__*/_interopDefaultLegacy(require$$0);
|
|
13
|
+
|
|
14
|
+
/*
|
|
15
|
+
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
16
|
+
Author Zackary Jackson @ScriptedAlchemy
|
|
17
|
+
*/
|
|
18
|
+
const path = require$$0__default["default"];
|
|
19
|
+
const {
|
|
20
|
+
injectRuleLoader,
|
|
21
|
+
hasLoader,
|
|
22
|
+
toDisplayErrors,
|
|
23
|
+
} = helpers.__exports;
|
|
24
|
+
const { exposeNextjsPages } = nextPageMapLoader.nextPageMapLoader.exports;
|
|
25
|
+
const DevHmrFixInvalidPongPlugin = DevHmrFixInvalidPongPlugin$1;
|
|
26
|
+
|
|
27
|
+
const CHILD_PLUGIN_NAME = 'ChildFederationPlugin';
|
|
28
|
+
|
|
29
|
+
/** @typedef {import("../../declarations/plugins/container/ModuleFederationPlugin").ExternalsType} ExternalsType */
|
|
30
|
+
/** @typedef {import("../../declarations/plugins/container/ModuleFederationPlugin").ModuleFederationPluginOptions} ModuleFederationPluginOptions */
|
|
31
|
+
|
|
32
|
+
/** @typedef {import("webpack").Shared} Shared */
|
|
33
|
+
/** @typedef {import("webpack").Compiler} Compiler */
|
|
34
|
+
|
|
35
|
+
class ModuleFederationPlugin {
|
|
36
|
+
/**
|
|
37
|
+
* @param {ModuleFederationPluginOptions} options options
|
|
38
|
+
*/
|
|
39
|
+
constructor(options) {
|
|
40
|
+
this._options = options;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Apply the plugin
|
|
45
|
+
* @param {Compiler} compiler the compiler instance
|
|
46
|
+
* @returns {void}
|
|
47
|
+
*/
|
|
48
|
+
apply(compiler) {
|
|
49
|
+
const { _options: options } = this;
|
|
50
|
+
const webpack = compiler.webpack;
|
|
51
|
+
const { ContainerPlugin, ContainerReferencePlugin } = webpack.container;
|
|
52
|
+
const { SharePlugin } = webpack.sharing;
|
|
53
|
+
const library = options.library || { type: 'var', name: options.name };
|
|
54
|
+
const remoteType =
|
|
55
|
+
options.remoteType ||
|
|
56
|
+
(options.library && /** @type {ExternalsType} */ options.library.type) ||
|
|
57
|
+
'script';
|
|
58
|
+
if (
|
|
59
|
+
library &&
|
|
60
|
+
!compiler.options.output.enabledLibraryTypes.includes(library.type)
|
|
61
|
+
) {
|
|
62
|
+
compiler.options.output.enabledLibraryTypes.push(library.type);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (
|
|
66
|
+
options.exposes &&
|
|
67
|
+
(Array.isArray(options.exposes)
|
|
68
|
+
? options.exposes.length > 0
|
|
69
|
+
: Object.keys(options.exposes).length > 0)
|
|
70
|
+
) {
|
|
71
|
+
new ContainerPlugin({
|
|
72
|
+
name: options.name,
|
|
73
|
+
library,
|
|
74
|
+
filename: options.filename,
|
|
75
|
+
runtime: options.runtime,
|
|
76
|
+
exposes: options.exposes,
|
|
77
|
+
}).apply(compiler);
|
|
78
|
+
}
|
|
79
|
+
if (
|
|
80
|
+
options.remotes &&
|
|
81
|
+
(Array.isArray(options.remotes)
|
|
82
|
+
? options.remotes.length > 0
|
|
83
|
+
: Object.keys(options.remotes).length > 0)
|
|
84
|
+
) {
|
|
85
|
+
new ContainerReferencePlugin({
|
|
86
|
+
remoteType,
|
|
87
|
+
remotes: options.remotes,
|
|
88
|
+
}).apply(compiler);
|
|
89
|
+
}
|
|
90
|
+
if (options.shared) {
|
|
91
|
+
new SharePlugin({
|
|
92
|
+
shared: options.shared,
|
|
93
|
+
shareScope: options.shareScope,
|
|
94
|
+
}).apply(compiler);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
class RemoveRRRuntimePlugin {
|
|
100
|
+
/**
|
|
101
|
+
* Apply the plugin
|
|
102
|
+
* @param {Compiler} compiler the compiler instance
|
|
103
|
+
* @returns {void}
|
|
104
|
+
*/
|
|
105
|
+
apply(compiler) {
|
|
106
|
+
const webpack = compiler.webpack;
|
|
107
|
+
|
|
108
|
+
compiler.hooks.thisCompilation.tap(
|
|
109
|
+
'RemoveRRRuntimePlugin',
|
|
110
|
+
(compilation) => {
|
|
111
|
+
compilation.hooks.processAssets.tap(
|
|
112
|
+
{
|
|
113
|
+
name: 'RemoveRRRuntimePlugin',
|
|
114
|
+
state: compilation.constructor.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE,
|
|
115
|
+
},
|
|
116
|
+
(assets) => {
|
|
117
|
+
Object.keys(assets).forEach((filename) => {
|
|
118
|
+
if (filename.endsWith('.js') || filename.endsWith('.mjs')) {
|
|
119
|
+
const asset = compilation.getAsset(filename);
|
|
120
|
+
const newSource = asset.source
|
|
121
|
+
.source()
|
|
122
|
+
.replace(/RefreshHelpers/g, 'NoExist');
|
|
123
|
+
const updatedAsset = new webpack.sources.RawSource(newSource);
|
|
124
|
+
|
|
125
|
+
if (asset) {
|
|
126
|
+
compilation.updateAsset(filename, updatedAsset);
|
|
127
|
+
} else {
|
|
128
|
+
compilation.emitAsset(filename, updatedAsset);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const DEFAULT_SHARE_SCOPE = {
|
|
140
|
+
react: {
|
|
141
|
+
singleton: true,
|
|
142
|
+
requiredVersion: false,
|
|
143
|
+
},
|
|
144
|
+
'react/jsx-runtime': {
|
|
145
|
+
singleton: true,
|
|
146
|
+
requiredVersion: false,
|
|
147
|
+
},
|
|
148
|
+
'react-dom': {
|
|
149
|
+
singleton: true,
|
|
150
|
+
requiredVersion: false,
|
|
151
|
+
},
|
|
152
|
+
'next/dynamic': {
|
|
153
|
+
requiredVersion: false,
|
|
154
|
+
singleton: true,
|
|
155
|
+
},
|
|
156
|
+
'styled-jsx': {
|
|
157
|
+
requiredVersion: false,
|
|
158
|
+
singleton: true,
|
|
159
|
+
},
|
|
160
|
+
'next/link': {
|
|
161
|
+
requiredVersion: false,
|
|
162
|
+
singleton: true,
|
|
163
|
+
},
|
|
164
|
+
'next/router': {
|
|
165
|
+
requiredVersion: false,
|
|
166
|
+
singleton: true,
|
|
167
|
+
},
|
|
168
|
+
'next/script': {
|
|
169
|
+
requiredVersion: false,
|
|
170
|
+
singleton: true,
|
|
171
|
+
},
|
|
172
|
+
'next/head': {
|
|
173
|
+
requiredVersion: false,
|
|
174
|
+
singleton: true,
|
|
175
|
+
},
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
class ChildFederationPlugin {
|
|
179
|
+
constructor(options, extraOptions = {}) {
|
|
180
|
+
this._options = options;
|
|
181
|
+
this._extraOptions = extraOptions;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Apply the plugin
|
|
185
|
+
* @param {Compiler} compiler the compiler instance
|
|
186
|
+
* @returns {void}
|
|
187
|
+
*/
|
|
188
|
+
apply(compiler) {
|
|
189
|
+
const webpack = compiler.webpack;
|
|
190
|
+
const LibraryPlugin = webpack.library.EnableLibraryPlugin;
|
|
191
|
+
webpack.container.ContainerPlugin;
|
|
192
|
+
const LoaderTargetPlugin = webpack.LoaderTargetPlugin;
|
|
193
|
+
const library = compiler.options.output.library;
|
|
194
|
+
|
|
195
|
+
compiler.hooks.thisCompilation.tap(CHILD_PLUGIN_NAME, (compilation) => {
|
|
196
|
+
const buildName = this._options.name;
|
|
197
|
+
const childOutput = {
|
|
198
|
+
...compiler.options.output,
|
|
199
|
+
publicPath: 'auto',
|
|
200
|
+
chunkLoadingGlobal: buildName + 'chunkLoader',
|
|
201
|
+
uniqueName: buildName,
|
|
202
|
+
library: {
|
|
203
|
+
name: buildName,
|
|
204
|
+
type: library.type,
|
|
205
|
+
},
|
|
206
|
+
chunkFilename: compiler.options.output.chunkFilename.replace(
|
|
207
|
+
'.js',
|
|
208
|
+
'-fed.js'
|
|
209
|
+
),
|
|
210
|
+
filename: compiler.options.output.chunkFilename.replace(
|
|
211
|
+
'.js',
|
|
212
|
+
'-fed.js'
|
|
213
|
+
),
|
|
214
|
+
};
|
|
215
|
+
const externalizedShares = Object.entries(DEFAULT_SHARE_SCOPE).reduce(
|
|
216
|
+
(acc, item) => {
|
|
217
|
+
const [key, value] = item;
|
|
218
|
+
acc[key] = { ...value, import: false };
|
|
219
|
+
if (key === 'react/jsx-runtime') {
|
|
220
|
+
delete acc[key].import;
|
|
221
|
+
}
|
|
222
|
+
return acc;
|
|
223
|
+
},
|
|
224
|
+
{}
|
|
225
|
+
);
|
|
226
|
+
const childCompiler = compilation.createChildCompiler(
|
|
227
|
+
CHILD_PLUGIN_NAME,
|
|
228
|
+
childOutput,
|
|
229
|
+
[
|
|
230
|
+
new ModuleFederationPlugin({
|
|
231
|
+
// library: {type: 'var', name: buildName},
|
|
232
|
+
...this._options,
|
|
233
|
+
exposes: {
|
|
234
|
+
...this._options.exposes,
|
|
235
|
+
...(this._extraOptions.exposePages
|
|
236
|
+
? exposeNextjsPages(compiler.options.context)
|
|
237
|
+
: {}),
|
|
238
|
+
},
|
|
239
|
+
runtime: false,
|
|
240
|
+
shared: {
|
|
241
|
+
...(this._extraOptions.skipSharingNextInternals
|
|
242
|
+
? {}
|
|
243
|
+
: externalizedShares),
|
|
244
|
+
...this._options.shared,
|
|
245
|
+
},
|
|
246
|
+
}),
|
|
247
|
+
new webpack.web.JsonpTemplatePlugin(childOutput),
|
|
248
|
+
new LoaderTargetPlugin('web'),
|
|
249
|
+
new LibraryPlugin(this._options.library.type),
|
|
250
|
+
new webpack.DefinePlugin({
|
|
251
|
+
'process.env.REMOTES': JSON.stringify(this._options.remotes),
|
|
252
|
+
'process.env.CURRENT_HOST': JSON.stringify(this._options.name),
|
|
253
|
+
}),
|
|
254
|
+
new AddRuntimeRequirementToPromiseExternal(),
|
|
255
|
+
]
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
new RemoveRRRuntimePlugin().apply(childCompiler);
|
|
259
|
+
|
|
260
|
+
childCompiler.options.module.rules.forEach((rule) => {
|
|
261
|
+
// next-image-loader fix which adds remote's hostname to the assets url
|
|
262
|
+
if (
|
|
263
|
+
this._extraOptions.enableImageLoaderFix &&
|
|
264
|
+
hasLoader(rule, 'next-image-loader')
|
|
265
|
+
) {
|
|
266
|
+
injectRuleLoader(rule, {
|
|
267
|
+
loader: path.resolve(__dirname, './loaders/fixImageLoader.js'),
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// url-loader fix for which adds remote's hostname to the assets url
|
|
272
|
+
if (
|
|
273
|
+
this._extraOptions.enableUrlLoaderFix &&
|
|
274
|
+
hasLoader(rule, 'url-loader')
|
|
275
|
+
) {
|
|
276
|
+
injectRuleLoader({
|
|
277
|
+
loader: path.resolve(__dirname, './loaders/fixUrlLoader.js'),
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
const MiniCss = childCompiler.options.plugins.find((p) => {
|
|
283
|
+
return p.constructor.name === 'NextMiniCssExtractPlugin';
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
const removePlugins = [
|
|
287
|
+
'NextJsRequireCacheHotReloader',
|
|
288
|
+
'BuildManifestPlugin',
|
|
289
|
+
'WellKnownErrorsPlugin',
|
|
290
|
+
'WebpackBuildEventsPlugin',
|
|
291
|
+
'HotModuleReplacementPlugin',
|
|
292
|
+
'NextMiniCssExtractPlugin',
|
|
293
|
+
'NextFederationPlugin',
|
|
294
|
+
'CopyFilePlugin',
|
|
295
|
+
'ProfilingPlugin',
|
|
296
|
+
'DropClientPage',
|
|
297
|
+
'ReactFreshWebpackPlugin',
|
|
298
|
+
];
|
|
299
|
+
|
|
300
|
+
childCompiler.options.plugins = childCompiler.options.plugins.filter(
|
|
301
|
+
(plugin) => !removePlugins.includes(plugin.constructor.name)
|
|
302
|
+
);
|
|
303
|
+
|
|
304
|
+
if (MiniCss) {
|
|
305
|
+
new MiniCss.constructor({
|
|
306
|
+
...MiniCss.options,
|
|
307
|
+
filename: MiniCss.options.filename.replace('.css', '-fed.css'),
|
|
308
|
+
chunkFilename: MiniCss.options.chunkFilename.replace(
|
|
309
|
+
'.css',
|
|
310
|
+
'-fed.css'
|
|
311
|
+
),
|
|
312
|
+
}).apply(childCompiler);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
childCompiler.options.experiments.lazyCompilation = false;
|
|
316
|
+
childCompiler.options.optimization.runtimeChunk = false;
|
|
317
|
+
delete childCompiler.options.optimization.splitChunks;
|
|
318
|
+
childCompiler.outputFileSystem = compiler.outputFileSystem;
|
|
319
|
+
if (compiler.options.mode === 'development') {
|
|
320
|
+
childCompiler.run((err, stats) => {
|
|
321
|
+
if (err) {
|
|
322
|
+
compilation.errors.push(err);
|
|
323
|
+
}
|
|
324
|
+
if (stats && stats.hasErrors()) {
|
|
325
|
+
compilation.errors.push(
|
|
326
|
+
new Error(toDisplayErrors(stats.compilation.errors))
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
} else {
|
|
331
|
+
childCompiler.runAsChild((err, entries, childCompilation) => {
|
|
332
|
+
if (childCompilation.getStats().hasErrors()) {
|
|
333
|
+
compilation.errors.push(
|
|
334
|
+
new Error(
|
|
335
|
+
toDisplayErrors(childCompilation.getStats().compilation.errors)
|
|
336
|
+
)
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
class AddRuntimeRequirementToPromiseExternal {
|
|
346
|
+
/**
|
|
347
|
+
* Apply the plugin
|
|
348
|
+
* @param {Compiler} compiler the compiler instance
|
|
349
|
+
* @returns {void}
|
|
350
|
+
*/
|
|
351
|
+
apply(compiler) {
|
|
352
|
+
compiler.hooks.compilation.tap(
|
|
353
|
+
'AddRuntimeRequirementToPromiseExternal',
|
|
354
|
+
(compilation) => {
|
|
355
|
+
const RuntimeGlobals = compiler.webpack.RuntimeGlobals;
|
|
356
|
+
// if (compilation.outputOptions.trustedTypes) {
|
|
357
|
+
compilation.hooks.additionalModuleRuntimeRequirements.tap(
|
|
358
|
+
'AddRuntimeRequirementToPromiseExternal',
|
|
359
|
+
(module, set, context) => {
|
|
360
|
+
if (module.externalType === 'promise') {
|
|
361
|
+
set.add(RuntimeGlobals.loadScript);
|
|
362
|
+
set.add(RuntimeGlobals.require);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
);
|
|
366
|
+
// }
|
|
367
|
+
}
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function extractUrlAndGlobal(urlAndGlobal) {
|
|
373
|
+
const index = urlAndGlobal.indexOf('@');
|
|
374
|
+
if (index <= 0 || index === urlAndGlobal.length - 1) {
|
|
375
|
+
throw new Error(`Invalid request "${urlAndGlobal}"`);
|
|
376
|
+
}
|
|
377
|
+
return [urlAndGlobal.substring(index + 1), urlAndGlobal.substring(0, index)];
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function generateRemoteTemplate(url, global) {
|
|
381
|
+
return `promise new Promise(function (resolve, reject) {
|
|
382
|
+
var __webpack_error__ = new Error();
|
|
383
|
+
if (typeof ${global} !== 'undefined') return resolve();
|
|
384
|
+
__webpack_require__.l(
|
|
385
|
+
${JSON.stringify(url)},
|
|
386
|
+
function (event) {
|
|
387
|
+
if (typeof ${global} !== 'undefined') return resolve();
|
|
388
|
+
var errorType = event && (event.type === 'load' ? 'missing' : event.type);
|
|
389
|
+
var realSrc = event && event.target && event.target.src;
|
|
390
|
+
__webpack_error__.message =
|
|
391
|
+
'Loading script failed.\\n(' + errorType + ': ' + realSrc + ')';
|
|
392
|
+
__webpack_error__.name = 'ScriptExternalLoadError';
|
|
393
|
+
__webpack_error__.type = errorType;
|
|
394
|
+
__webpack_error__.request = realSrc;
|
|
395
|
+
reject(__webpack_error__);
|
|
396
|
+
},
|
|
397
|
+
${JSON.stringify(global)},
|
|
398
|
+
);
|
|
399
|
+
}).then(function () {
|
|
400
|
+
const proxy = {
|
|
401
|
+
get: ${global}.get,
|
|
402
|
+
init: function(shareScope) {
|
|
403
|
+
const handler = {
|
|
404
|
+
get(target, prop) {
|
|
405
|
+
if (target[prop]) {
|
|
406
|
+
Object.values(target[prop]).forEach(function(o) {
|
|
407
|
+
if(o.from === '_N_E') {
|
|
408
|
+
o.loaded = 1
|
|
409
|
+
}
|
|
410
|
+
})
|
|
411
|
+
}
|
|
412
|
+
return target[prop]
|
|
413
|
+
},
|
|
414
|
+
set(target, property, value, receiver) {
|
|
415
|
+
if (target[property]) {
|
|
416
|
+
return target[property]
|
|
417
|
+
}
|
|
418
|
+
target[property] = value
|
|
419
|
+
return true
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
try {
|
|
423
|
+
${global}.init(new Proxy(shareScope, handler))
|
|
424
|
+
} catch (e) {
|
|
425
|
+
|
|
426
|
+
}
|
|
427
|
+
${global}.__initialized = true
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
if (!${global}.__initialized) {
|
|
431
|
+
proxy.init()
|
|
432
|
+
}
|
|
433
|
+
return proxy
|
|
434
|
+
})`;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
function createRuntimeVariables(remotes) {
|
|
438
|
+
return Object.entries(remotes).reduce((acc, remote) => {
|
|
439
|
+
acc[remote[0]] = remote[1].replace('promise ', '');
|
|
440
|
+
return acc;
|
|
441
|
+
}, {});
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
class NextFederationPlugin {
|
|
445
|
+
constructor(options) {
|
|
446
|
+
const { extraOptions, ...mainOpts } = options;
|
|
447
|
+
this._options = mainOpts;
|
|
448
|
+
this._extraOptions = extraOptions;
|
|
449
|
+
if (options.remotes) {
|
|
450
|
+
const parsedRemotes = Object.entries(options.remotes).reduce(
|
|
451
|
+
(acc, remote) => {
|
|
452
|
+
if (remote[1].includes('@')) {
|
|
453
|
+
const [url, global] = extractUrlAndGlobal(remote[1]);
|
|
454
|
+
acc[remote[0]] = generateRemoteTemplate(url, global);
|
|
455
|
+
return acc;
|
|
456
|
+
}
|
|
457
|
+
acc[remote[0]] = remote[1];
|
|
458
|
+
return acc;
|
|
459
|
+
},
|
|
460
|
+
{}
|
|
461
|
+
);
|
|
462
|
+
this._options.remotes = parsedRemotes;
|
|
463
|
+
}
|
|
464
|
+
if (this._options.library) {
|
|
465
|
+
console.error('[mf] you cannot set custom library');
|
|
466
|
+
}
|
|
467
|
+
this._options.library = {
|
|
468
|
+
// assign remote name to object to avoid SWC mangling top level variable
|
|
469
|
+
type: 'window',
|
|
470
|
+
name: this._options.name,
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Apply the plugin
|
|
475
|
+
* @param {Compiler} compiler the compiler instance
|
|
476
|
+
* @returns {void}
|
|
477
|
+
*/
|
|
478
|
+
apply(compiler) {
|
|
479
|
+
if (this._extraOptions.automaticPageStitching) {
|
|
480
|
+
compiler.options.module.rules.push({
|
|
481
|
+
test: /next[\\/]dist[\\/]client[\\/]page-loader\.js$/,
|
|
482
|
+
loader: path.resolve(__dirname, './loaders/patchNextClientPageLoader'),
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
const webpack = compiler.webpack;
|
|
487
|
+
const sharedForHost = Object.entries({
|
|
488
|
+
...(this._options.shared || {}),
|
|
489
|
+
...DEFAULT_SHARE_SCOPE,
|
|
490
|
+
}).reduce((acc, item) => {
|
|
491
|
+
const [itemKey, shareOptions] = item;
|
|
492
|
+
|
|
493
|
+
const shareKey = 'host' + (item.shareKey || itemKey);
|
|
494
|
+
acc[shareKey] = shareOptions;
|
|
495
|
+
if (!shareOptions.import) {
|
|
496
|
+
acc[shareKey].import = itemKey;
|
|
497
|
+
}
|
|
498
|
+
if (!shareOptions.shareKey) {
|
|
499
|
+
acc[shareKey].shareKey = itemKey;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
if (DEFAULT_SHARE_SCOPE[itemKey]) {
|
|
503
|
+
acc[shareKey].packageName = itemKey;
|
|
504
|
+
}
|
|
505
|
+
return acc;
|
|
506
|
+
}, {});
|
|
507
|
+
|
|
508
|
+
new webpack.container.ModuleFederationPlugin({
|
|
509
|
+
...this._options,
|
|
510
|
+
exposes: {},
|
|
511
|
+
shared: {
|
|
512
|
+
noop: {
|
|
513
|
+
import: 'data:text/javascript,module.exports = {};',
|
|
514
|
+
requiredVersion: false,
|
|
515
|
+
version: '0',
|
|
516
|
+
},
|
|
517
|
+
...sharedForHost,
|
|
518
|
+
},
|
|
519
|
+
}).apply(compiler);
|
|
520
|
+
new webpack.DefinePlugin({
|
|
521
|
+
'process.env.REMOTES': createRuntimeVariables(this._options.remotes),
|
|
522
|
+
'process.env.CURRENT_HOST': JSON.stringify(this._options.name),
|
|
523
|
+
}).apply(compiler);
|
|
524
|
+
new ChildFederationPlugin(this._options, this._extraOptions).apply(
|
|
525
|
+
compiler
|
|
526
|
+
);
|
|
527
|
+
new AddRuntimeRequirementToPromiseExternal().apply(compiler);
|
|
528
|
+
if (compiler.options.mode === 'development') {
|
|
529
|
+
new DevHmrFixInvalidPongPlugin().apply(compiler);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
var NextFederationPlugin_1 = NextFederationPlugin;
|
|
535
|
+
|
|
536
|
+
module.exports = NextFederationPlugin_1;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
function getAugmentedNamespace(n) {
|
|
6
|
+
var f = n.default;
|
|
7
|
+
if (typeof f == "function") {
|
|
8
|
+
var a = function () {
|
|
9
|
+
return f.apply(this, arguments);
|
|
10
|
+
};
|
|
11
|
+
a.prototype = f.prototype;
|
|
12
|
+
} else a = {};
|
|
13
|
+
Object.defineProperty(a, '__esModule', {value: true});
|
|
14
|
+
Object.keys(n).forEach(function (k) {
|
|
15
|
+
var d = Object.getOwnPropertyDescriptor(n, k);
|
|
16
|
+
Object.defineProperty(a, k, d.get ? d : {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
get: function () {
|
|
19
|
+
return n[k];
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
return a;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
exports.getAugmentedNamespace = getAugmentedNamespace;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
/******************************************************************************
|
|
6
|
+
Copyright (c) Microsoft Corporation.
|
|
7
|
+
|
|
8
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
9
|
+
purpose with or without fee is hereby granted.
|
|
10
|
+
|
|
11
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
12
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
13
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
14
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
15
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
16
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
17
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
18
|
+
***************************************************************************** */
|
|
19
|
+
|
|
20
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
21
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
22
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
23
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
24
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
25
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
26
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function __generator(thisArg, body) {
|
|
31
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
32
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
33
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
34
|
+
function step(op) {
|
|
35
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
36
|
+
while (_) try {
|
|
37
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
38
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
39
|
+
switch (op[0]) {
|
|
40
|
+
case 0: case 1: t = op; break;
|
|
41
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
42
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
43
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
44
|
+
default:
|
|
45
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
46
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
47
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
48
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
49
|
+
if (t[2]) _.ops.pop();
|
|
50
|
+
_.trys.pop(); continue;
|
|
51
|
+
}
|
|
52
|
+
op = body.call(thisArg, _);
|
|
53
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
54
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function __values(o) {
|
|
59
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
60
|
+
if (m) return m.call(o);
|
|
61
|
+
if (o && typeof o.length === "number") return {
|
|
62
|
+
next: function () {
|
|
63
|
+
if (o && i >= o.length) o = void 0;
|
|
64
|
+
return { value: o && o[i++], done: !o };
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function __read(o, n) {
|
|
71
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
72
|
+
if (!m) return o;
|
|
73
|
+
var i = m.call(o), r, ar = [], e;
|
|
74
|
+
try {
|
|
75
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
76
|
+
}
|
|
77
|
+
catch (error) { e = { error: error }; }
|
|
78
|
+
finally {
|
|
79
|
+
try {
|
|
80
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
81
|
+
}
|
|
82
|
+
finally { if (e) throw e.error; }
|
|
83
|
+
}
|
|
84
|
+
return ar;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function __spreadArray(to, from, pack) {
|
|
88
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
89
|
+
if (ar || !(i in from)) {
|
|
90
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
91
|
+
ar[i] = from[i];
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
exports.__awaiter = __awaiter;
|
|
98
|
+
exports.__generator = __generator;
|
|
99
|
+
exports.__read = __read;
|
|
100
|
+
exports.__spreadArray = __spreadArray;
|
|
101
|
+
exports.__values = __values;
|