@module-federation/nextjs-mf 5.3.2 → 5.4.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/lib/ModuleFederationPlugin.js +3 -1
- package/lib/NextFederationPlugin.js +18 -1051
- package/lib/_virtual/_commonjsHelpers.js +44 -0
- package/lib/_virtual/_path.js +11 -0
- package/lib/_virtual/_tslib.js +176 -0
- package/lib/_virtual/fs.js +7 -0
- package/lib/_virtual/fs2.js +7 -0
- package/lib/_virtual/options.js +7 -0
- package/lib/_virtual/utils.js +7 -0
- package/lib/client/MFClient.d.ts +4 -1
- package/lib/client/MFClient.d.ts.map +1 -1
- package/lib/client/MFClient.js +35 -19
- package/lib/client/RemoteContainer.js +1 -0
- package/lib/client/RemotePages.d.ts +2 -1
- package/lib/client/RemotePages.d.ts.map +1 -1
- package/lib/client/RemotePages.js +38 -12
- package/lib/dependencies/webpack/lib/container/options.js +102 -0
- package/lib/dependencies/webpack/lib/sharing/utils.js +104 -0
- package/lib/dependencies/webpack/lib/util/fs.js +359 -0
- package/lib/internal.js +35 -18
- package/lib/loaders/UrlNode.js +3 -1
- package/lib/loaders/patchNextClientPageLoader.js +1 -1
- package/lib/node-plugin/streaming/CommonJsChunkLoadingPlugin.js +92 -0
- package/lib/node-plugin/streaming/LoadFileChunkLoadingRuntimeModule.js +412 -0
- package/lib/node-plugin/streaming/NodeRuntime.js +247 -0
- package/lib/node-plugin/streaming/index.js +44 -0
- package/lib/node-plugin/streaming/loadScript.js +57 -0
- package/lib/plugins/DevHmrFixInvalidPongPlugin.js +14 -0
- package/lib/utils.js +16 -7
- package/package.json +4 -3
|
@@ -2,1054 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
var fs = require('fs');
|
|
4
4
|
var path = require('path');
|
|
5
|
-
var
|
|
6
|
-
var
|
|
5
|
+
var internal = require('./internal.js');
|
|
6
|
+
var index = require('./node-plugin/streaming/index.js');
|
|
7
|
+
var NodeRuntime = require('./node-plugin/streaming/NodeRuntime.js');
|
|
8
|
+
var ModuleFederationPlugin = require('./ModuleFederationPlugin.js');
|
|
7
9
|
|
|
8
10
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
9
11
|
|
|
10
12
|
var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
|
|
11
13
|
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
|
|
12
14
|
|
|
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
15
|
/*
|
|
1050
16
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
1051
17
|
Author Zackary Jackson @ScriptedAlchemy
|
|
1052
18
|
*/
|
|
19
|
+
'use strict';
|
|
1053
20
|
const {
|
|
1054
21
|
injectRuleLoader,
|
|
1055
22
|
hasLoader,
|
|
@@ -1131,10 +98,10 @@ class ChildFederationPlugin {
|
|
|
1131
98
|
const isDev = compiler.options.mode === 'development';
|
|
1132
99
|
let outputPath;
|
|
1133
100
|
if (isDev && isServer) {
|
|
1134
|
-
outputPath = path__default["default"].join(getOutputPath(compiler), 'static/ssr');
|
|
101
|
+
outputPath = path__default["default"].join(internal.getOutputPath(compiler), 'static/ssr');
|
|
1135
102
|
} else {
|
|
1136
103
|
if (isServer) {
|
|
1137
|
-
outputPath = path__default["default"].join(getOutputPath(compiler), 'static/ssr');
|
|
104
|
+
outputPath = path__default["default"].join(internal.getOutputPath(compiler), 'static/ssr');
|
|
1138
105
|
} else {
|
|
1139
106
|
outputPath = compiler.options.output.path;
|
|
1140
107
|
}
|
|
@@ -1177,7 +144,7 @@ class ChildFederationPlugin {
|
|
|
1177
144
|
shared: {
|
|
1178
145
|
...(this._extraOptions.skipSharingNextInternals
|
|
1179
146
|
? {}
|
|
1180
|
-
: externalizedShares),
|
|
147
|
+
: internal.externalizedShares),
|
|
1181
148
|
...this._options.shared,
|
|
1182
149
|
},
|
|
1183
150
|
};
|
|
@@ -1199,19 +166,19 @@ class ChildFederationPlugin {
|
|
|
1199
166
|
];
|
|
1200
167
|
} else if (compiler.options.name === 'server') {
|
|
1201
168
|
plugins = [
|
|
1202
|
-
new
|
|
169
|
+
new NodeRuntime(federationPluginOptions, {ModuleFederationPlugin: FederationPlugin}),
|
|
1203
170
|
new webpack.node.NodeTemplatePlugin(childOutput),
|
|
1204
171
|
//TODO: Externals function needs to internalize any shared module for host and remote build
|
|
1205
172
|
new webpack.ExternalsPlugin(compiler.options.externalsType, [
|
|
1206
173
|
// next dynamic needs to be within webpack, cannot be externalized
|
|
1207
|
-
...Object.keys(DEFAULT_SHARE_SCOPE).filter(
|
|
174
|
+
...Object.keys(internal.DEFAULT_SHARE_SCOPE).filter(
|
|
1208
175
|
(k) => k !== 'next/dynamic'
|
|
1209
176
|
),
|
|
1210
177
|
'react/jsx-runtime',
|
|
1211
178
|
'react/jsx-dev-runtime',
|
|
1212
179
|
]),
|
|
1213
180
|
// new LoaderTargetPlugin('async-node'),
|
|
1214
|
-
new
|
|
181
|
+
new index(federationPluginOptions, webpack),
|
|
1215
182
|
new LibraryPlugin(federationPluginOptions.library.type),
|
|
1216
183
|
// new webpack.DefinePlugin({
|
|
1217
184
|
// 'process.env.REMOTES': JSON.stringify(this._options.remotes),
|
|
@@ -1271,7 +238,7 @@ class ChildFederationPlugin {
|
|
|
1271
238
|
});
|
|
1272
239
|
|
|
1273
240
|
childCompiler.options.plugins = childCompiler.options.plugins.filter(
|
|
1274
|
-
(plugin) => !removePlugins.includes(plugin.constructor.name)
|
|
241
|
+
(plugin) => !internal.removePlugins.includes(plugin.constructor.name)
|
|
1275
242
|
);
|
|
1276
243
|
|
|
1277
244
|
if (MiniCss) {
|
|
@@ -1471,7 +438,7 @@ class NextFederationPlugin {
|
|
|
1471
438
|
console.error('[nextjs-mf] WARNING: SSR IS NOT FULLY SUPPORTED YET, Only use pluign on client builds');
|
|
1472
439
|
// target false because we use our own target for node env
|
|
1473
440
|
compiler.options.target = false;
|
|
1474
|
-
new
|
|
441
|
+
new index(this._options, webpack).apply(compiler);
|
|
1475
442
|
this._options.library = {};
|
|
1476
443
|
this._options.library.type = 'commonjs-module';
|
|
1477
444
|
this._options.library.name = this._options.name;
|
|
@@ -1482,7 +449,7 @@ class NextFederationPlugin {
|
|
|
1482
449
|
);
|
|
1483
450
|
|
|
1484
451
|
// should this be a plugin that we apply to the compiler?
|
|
1485
|
-
internalizeSharedPackages(this._options, compiler);
|
|
452
|
+
internal.internalizeSharedPackages(this._options, compiler);
|
|
1486
453
|
} else {
|
|
1487
454
|
if (this._extraOptions.automaticPageStitching) {
|
|
1488
455
|
compiler.options.module.rules.push({
|
|
@@ -1494,8 +461,8 @@ class NextFederationPlugin {
|
|
|
1494
461
|
const parsedRemotes = Object.entries(this._options.remotes).reduce(
|
|
1495
462
|
(acc, remote) => {
|
|
1496
463
|
if (remote[1].includes('@')) {
|
|
1497
|
-
const [url, global] = extractUrlAndGlobal(remote[1]);
|
|
1498
|
-
acc[remote[0]] = generateRemoteTemplate(url, global);
|
|
464
|
+
const [url, global] = internal.extractUrlAndGlobal(remote[1]);
|
|
465
|
+
acc[remote[0]] = internal.generateRemoteTemplate(url, global);
|
|
1499
466
|
return acc;
|
|
1500
467
|
}
|
|
1501
468
|
acc[remote[0]] = remote[1];
|
|
@@ -1524,11 +491,11 @@ class NextFederationPlugin {
|
|
|
1524
491
|
|
|
1525
492
|
const ModuleFederationPlugin = {
|
|
1526
493
|
client: webpack.container.ModuleFederationPlugin,
|
|
1527
|
-
server:
|
|
494
|
+
server: NodeRuntime,
|
|
1528
495
|
}[compiler.options.name];
|
|
1529
496
|
// ignore edge runtime and middleware builds
|
|
1530
497
|
if (ModuleFederationPlugin) {
|
|
1531
|
-
const internalShare = reKeyHostShared(this._options.shared);
|
|
498
|
+
const internalShare = internal.reKeyHostShared(this._options.shared);
|
|
1532
499
|
const hostFederationPluginOptions = {
|
|
1533
500
|
...this._options,
|
|
1534
501
|
exposes: {},
|