@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.
- package/lib/ModuleFederationPlugin.js +80 -0
- package/lib/NextFederationPlugin.js +1554 -1
- package/lib/include-defaults.js +1 -3
- package/lib/internal.js +241 -0
- package/lib/loaders/UrlNode.js +1 -7
- package/lib/loaders/helpers.js +3 -10
- package/lib/loaders/nextPageMapLoader.js +5 -19
- package/lib/plugins/DevHmrFixInvalidPongPlugin.js +1 -6
- package/lib/utils.js +7 -0
- package/node-plugin/README.md +27 -0
- package/node-plugin/package.json +4 -0
- package/node-plugin/streaming/CommonJsChunkLoadingPlugin.js +89 -0
- package/node-plugin/streaming/LoadFileChunkLoadingRuntimeModule.js +410 -0
- package/node-plugin/streaming/NodeRuntime.js +245 -0
- package/node-plugin/streaming/index.js +42 -0
- package/node-plugin/streaming/loadScript.js +51 -0
- package/package.json +3 -3
- package/lib/NextFederationPlugin2.js +0 -536
- package/lib/_virtual/UrlNode.js +0 -8
- package/lib/_virtual/_commonjsHelpers.js +0 -26
- package/lib/_virtual/helpers.js +0 -7
- package/lib/_virtual/nextPageMapLoader.js +0 -7
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import CommonJsChunkLoadingPlugin from './CommonJsChunkLoadingPlugin';
|
|
2
|
+
|
|
3
|
+
class NodeSoftwareStreamRuntime {
|
|
4
|
+
constructor(options, context) {
|
|
5
|
+
this.options = options || {};
|
|
6
|
+
this.context = context || {};
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
apply(compiler) {
|
|
10
|
+
if (compiler.options.target) {
|
|
11
|
+
console.warn(
|
|
12
|
+
`target should be set to false while using NodeSoftwareStreamRuntime plugin, actual target: ${compiler.options.target}`
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// When used with Next.js, context is needed to use Next.js webpack
|
|
17
|
+
const { webpack } = compiler;
|
|
18
|
+
|
|
19
|
+
// This will enable CommonJsChunkFormatPlugin
|
|
20
|
+
compiler.options.output.chunkFormat = 'commonjs';
|
|
21
|
+
// This will force async chunk loading
|
|
22
|
+
compiler.options.output.chunkLoading = 'async-node';
|
|
23
|
+
// Disable default config
|
|
24
|
+
compiler.options.output.enabledChunkLoadingTypes = false;
|
|
25
|
+
|
|
26
|
+
new ((webpack && webpack.node && webpack.node.NodeEnvironmentPlugin) ||
|
|
27
|
+
require('webpack/lib/node/NodeEnvironmentPlugin'))({
|
|
28
|
+
infrastructureLogging: compiler.options.infrastructureLogging,
|
|
29
|
+
}).apply(compiler);
|
|
30
|
+
new ((webpack && webpack.node && webpack.node.NodeTargetPlugin) ||
|
|
31
|
+
require('webpack/lib/node/NodeTargetPlugin'))().apply(compiler);
|
|
32
|
+
new CommonJsChunkLoadingPlugin({
|
|
33
|
+
asyncChunkLoading: true,
|
|
34
|
+
name: this.options.name,
|
|
35
|
+
remotes: this.options.remotes,
|
|
36
|
+
baseURI: compiler.options.output.publicPath,
|
|
37
|
+
promiseBaseURI: this.options.promiseBaseURI,
|
|
38
|
+
}).apply(compiler);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export default NodeSoftwareStreamRuntime;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* loadScript(baseURI, fileName, cb)
|
|
3
|
+
* loadScript(scriptUrl, cb)
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
module.exports = `
|
|
7
|
+
function loadScript(url,cb,chunkID) {
|
|
8
|
+
var url;
|
|
9
|
+
var cb = arguments[arguments.length - 1];
|
|
10
|
+
if (typeof cb !== "function") {
|
|
11
|
+
throw new Error("last argument should be a function");
|
|
12
|
+
}
|
|
13
|
+
if (arguments.length === 2) {
|
|
14
|
+
url = arguments[0];
|
|
15
|
+
} else if (arguments.length === 3) {
|
|
16
|
+
url = new URL(arguments[1], arguments[0]).toString();
|
|
17
|
+
} else {
|
|
18
|
+
throw new Error("invalid number of arguments");
|
|
19
|
+
}
|
|
20
|
+
if(global.webpackChunkLoad){
|
|
21
|
+
global.webpackChunkLoad(url).then(function(resp){
|
|
22
|
+
return resp.text();
|
|
23
|
+
}).then(function(rawData){
|
|
24
|
+
cb(null, rawData);
|
|
25
|
+
}).catch(function(err){
|
|
26
|
+
console.error('Federated Chunk load failed', error);
|
|
27
|
+
return cb(error)
|
|
28
|
+
});
|
|
29
|
+
} else {
|
|
30
|
+
//TODO https support
|
|
31
|
+
let request = (url.startsWith('https') ? require('https') : require('http')).get(url, function (resp) {
|
|
32
|
+
if (resp.statusCode === 200) {
|
|
33
|
+
let rawData = '';
|
|
34
|
+
resp.setEncoding('utf8');
|
|
35
|
+
resp.on('data', chunk => {
|
|
36
|
+
rawData += chunk;
|
|
37
|
+
});
|
|
38
|
+
resp.on('end', () => {
|
|
39
|
+
cb(null, rawData);
|
|
40
|
+
});
|
|
41
|
+
} else {
|
|
42
|
+
cb(resp);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
request.on('error', error => {
|
|
46
|
+
console.error('Federated Chunk load failed', error);
|
|
47
|
+
return cb(error)
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
`;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"public": true,
|
|
3
3
|
"name": "@module-federation/nextjs-mf",
|
|
4
|
-
"version": "5.3.
|
|
4
|
+
"version": "5.3.2",
|
|
5
5
|
"description": "Module Federation helper for NextJS",
|
|
6
6
|
"main": "lib/index.js",
|
|
7
7
|
"types": "lib/index.d.ts",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"@types/react": "^18.0.19",
|
|
33
33
|
"concurrently": "^7.3.0",
|
|
34
34
|
"cpx": "^1.5.0",
|
|
35
|
-
"next": "12.
|
|
35
|
+
"next": "12.3.2",
|
|
36
36
|
"prettier": "2.3.2",
|
|
37
37
|
"react": "^18.2.0",
|
|
38
38
|
"rollup": "^2.78.1",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"rollup-plugin-typescript2": "^0.33.0",
|
|
43
43
|
"tslib": "^2.4.0",
|
|
44
44
|
"typescript": "^4.8.2",
|
|
45
|
-
"webpack": "5.
|
|
45
|
+
"webpack": "5.64.4"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
48
|
"react": "^17.0.0 || ^18.0.0"
|
|
@@ -1,536 +0,0 @@
|
|
|
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;
|
package/lib/_virtual/UrlNode.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
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;
|