@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.
@@ -0,0 +1,92 @@
1
+ 'use strict';
2
+
3
+ var LoadFileChunkLoadingRuntimeModule = require('./LoadFileChunkLoadingRuntimeModule.js');
4
+
5
+ const RuntimeGlobals = require('webpack/lib/RuntimeGlobals');
6
+ const StartupChunkDependenciesPlugin = require('webpack/lib/runtime/StartupChunkDependenciesPlugin');
7
+ // const ChunkLoadingRuntimeModule = require('webpack/lib/node/ReadFileChunkLoadingRuntimeModule')
8
+ class CommonJsChunkLoadingPlugin {
9
+ constructor(options) {
10
+ this.options = options || {};
11
+ this._asyncChunkLoading = this.options.asyncChunkLoading;
12
+ }
13
+
14
+ /**
15
+ * Apply the plugin
16
+ * @param {Compiler} compiler the compiler instance
17
+ * @returns {void}
18
+ */
19
+ apply(compiler) {
20
+ const chunkLoadingValue = this._asyncChunkLoading
21
+ ? 'async-node'
22
+ : 'require';
23
+ new StartupChunkDependenciesPlugin({
24
+ chunkLoading: chunkLoadingValue,
25
+ asyncChunkLoading: this._asyncChunkLoading,
26
+ }).apply(compiler);
27
+ compiler.hooks.thisCompilation.tap(
28
+ 'CommonJsChunkLoadingPlugin',
29
+ (compilation) => {
30
+ // Always enabled
31
+ const isEnabledForChunk = () => true;
32
+ const onceForChunkSet = new WeakSet();
33
+ const handler = (chunk, set) => {
34
+ if (onceForChunkSet.has(chunk)) return;
35
+ onceForChunkSet.add(chunk);
36
+ if (!isEnabledForChunk(chunk)) return;
37
+ set.add(RuntimeGlobals.moduleFactoriesAddOnly);
38
+ set.add(RuntimeGlobals.hasOwnProperty);
39
+ compilation.addRuntimeModule(
40
+ chunk,
41
+ new LoadFileChunkLoadingRuntimeModule(set, this.options, {
42
+ webpack: compiler.webpack,
43
+ })
44
+ );
45
+ };
46
+
47
+ compilation.hooks.runtimeRequirementInTree
48
+ .for(RuntimeGlobals.ensureChunkHandlers)
49
+ .tap('CommonJsChunkLoadingPlugin', handler);
50
+ compilation.hooks.runtimeRequirementInTree
51
+ .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
52
+ .tap('CommonJsChunkLoadingPlugin', handler);
53
+ compilation.hooks.runtimeRequirementInTree
54
+ .for(RuntimeGlobals.hmrDownloadManifest)
55
+ .tap('CommonJsChunkLoadingPlugin', handler);
56
+ compilation.hooks.runtimeRequirementInTree
57
+ .for(RuntimeGlobals.baseURI)
58
+ .tap('CommonJsChunkLoadingPlugin', handler);
59
+ compilation.hooks.runtimeRequirementInTree
60
+ .for(RuntimeGlobals.externalInstallChunk)
61
+ .tap('CommonJsChunkLoadingPlugin', handler);
62
+ compilation.hooks.runtimeRequirementInTree
63
+ .for(RuntimeGlobals.onChunksLoaded)
64
+ .tap('CommonJsChunkLoadingPlugin', handler);
65
+
66
+ compilation.hooks.runtimeRequirementInTree
67
+ .for(RuntimeGlobals.ensureChunkHandlers)
68
+ .tap('CommonJsChunkLoadingPlugin', (chunk, set) => {
69
+ if (!isEnabledForChunk(chunk)) return;
70
+ set.add(RuntimeGlobals.getChunkScriptFilename);
71
+ });
72
+ compilation.hooks.runtimeRequirementInTree
73
+ .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
74
+ .tap('CommonJsChunkLoadingPlugin', (chunk, set) => {
75
+ if (!isEnabledForChunk(chunk)) return;
76
+ set.add(RuntimeGlobals.getChunkUpdateScriptFilename);
77
+ set.add(RuntimeGlobals.moduleCache);
78
+ set.add(RuntimeGlobals.hmrModuleData);
79
+ set.add(RuntimeGlobals.moduleFactoriesAddOnly);
80
+ });
81
+ compilation.hooks.runtimeRequirementInTree
82
+ .for(RuntimeGlobals.hmrDownloadManifest)
83
+ .tap('CommonJsChunkLoadingPlugin', (chunk, set) => {
84
+ if (!isEnabledForChunk(chunk)) return;
85
+ set.add(RuntimeGlobals.getUpdateManifestFilename);
86
+ });
87
+ }
88
+ );
89
+ }
90
+ }
91
+
92
+ module.exports = CommonJsChunkLoadingPlugin;
@@ -0,0 +1,412 @@
1
+ 'use strict';
2
+
3
+ var loadScript = require('./loadScript.js');
4
+
5
+ /*
6
+ MIT License http://www.opensource.org/licenses/mit-license.php
7
+ */
8
+
9
+ 'use strict';
10
+
11
+ const RuntimeGlobals = require('webpack/lib/RuntimeGlobals');
12
+ const RuntimeModule = require('webpack/lib/RuntimeModule');
13
+ const Template = require('webpack/lib/Template');
14
+ const compileBooleanMatcher = require('webpack/lib/util/compileBooleanMatcher');
15
+ const { getUndoPath } = require('webpack/lib/util/identifier');
16
+
17
+ class ReadFileChunkLoadingRuntimeModule extends RuntimeModule {
18
+ constructor(runtimeRequirements, options, context) {
19
+ super('readFile chunk loading', RuntimeModule.STAGE_ATTACH);
20
+ this.runtimeRequirements = runtimeRequirements;
21
+ this.options = options;
22
+ this.context = context;
23
+ }
24
+
25
+ /**
26
+ * @private
27
+ * @param {Chunk} chunk chunk
28
+ * @param {string} rootOutputDir root output directory
29
+ * @returns {string} generated code
30
+ */
31
+ _generateBaseUri(chunk, rootOutputDir) {
32
+ const options = chunk.getEntryOptions();
33
+ if (options && options.baseUri) {
34
+ return `${RuntimeGlobals.baseURI} = ${JSON.stringify(options.baseUri)};`;
35
+ }
36
+
37
+ return `${RuntimeGlobals.baseURI} = require("url").pathToFileURL(${
38
+ rootOutputDir
39
+ ? `__dirname + ${JSON.stringify('/' + rootOutputDir)}`
40
+ : '__filename'
41
+ });`;
42
+ }
43
+
44
+ /**
45
+ * @returns {string} runtime code
46
+ */
47
+ generate() {
48
+ // name in this context is always the current remote itself.
49
+ // this code below is in each webpack runtime, host and remotes
50
+ // remote entries handle their own loading of chunks, so i have fractal self awareness
51
+ // hosts will likely never run the http chunk loading runtime, they use fs.readFile
52
+ // remotes only use fs.readFile if we were to cache the chunks on disk after fetching - otherwise its always using http
53
+ // so for example, if im in hostA and require(remoteb/module) --> console.log of name in runtime code will return remoteb
54
+
55
+ const { baseURI, promiseBaseURI, remotes, name } = this.options;
56
+ const { webpack } = this.context;
57
+ const chunkHasJs =
58
+ (webpack && webpack.javascript.JavascriptModulesPlugin.chunkHasJs) ||
59
+ require('webpack/lib/javascript/JavascriptModulesPlugin').chunkHasJs;
60
+
61
+ // workaround for next.js
62
+ const getInitialChunkIds = (chunk, chunkGraph) => {
63
+ const initialChunkIds = new Set(chunk.ids);
64
+ for (const c of chunk.getAllInitialChunks()) {
65
+ if (c === chunk || chunkHasJs(c, chunkGraph)) continue;
66
+ for (const id of c.ids) initialChunkIds.add(id);
67
+ }
68
+ return initialChunkIds;
69
+ };
70
+
71
+ const { chunkGraph, chunk } = this;
72
+ const { runtimeTemplate } = this.compilation;
73
+ const fn = RuntimeGlobals.ensureChunkHandlers;
74
+ const withBaseURI = this.runtimeRequirements.has(RuntimeGlobals.baseURI);
75
+ const withExternalInstallChunk = this.runtimeRequirements.has(
76
+ RuntimeGlobals.externalInstallChunk
77
+ );
78
+ const withOnChunkLoad = this.runtimeRequirements.has(
79
+ RuntimeGlobals.onChunksLoaded
80
+ );
81
+ const withLoading = this.runtimeRequirements.has(
82
+ RuntimeGlobals.ensureChunkHandlers
83
+ );
84
+ const withHmr = this.runtimeRequirements.has(
85
+ RuntimeGlobals.hmrDownloadUpdateHandlers
86
+ );
87
+ const withHmrManifest = this.runtimeRequirements.has(
88
+ RuntimeGlobals.hmrDownloadManifest
89
+ );
90
+
91
+ const conditionMap = chunkGraph.getChunkConditionMap(chunk, chunkHasJs);
92
+ const hasJsMatcher = compileBooleanMatcher(conditionMap);
93
+ const initialChunkIds = getInitialChunkIds(chunk, chunkGraph, chunkHasJs);
94
+
95
+ const outputName = this.compilation.getPath(
96
+ (
97
+ (webpack &&
98
+ webpack.javascript.JavascriptModulesPlugin
99
+ .getChunkFilenameTemplate) ||
100
+ require('webpack/lib/javascript/JavascriptModulesPlugin')
101
+ .getChunkFilenameTemplate
102
+ )(chunk, this.compilation.outputOptions),
103
+ {
104
+ chunk,
105
+ contentHashType: 'javascript',
106
+ }
107
+ );
108
+ const rootOutputDir = getUndoPath(
109
+ outputName,
110
+ this.compilation.outputOptions.path,
111
+ false
112
+ );
113
+
114
+ const stateExpression = withHmr
115
+ ? `${RuntimeGlobals.hmrRuntimeStatePrefix}_readFileVm`
116
+ : undefined;
117
+
118
+ return Template.asString([
119
+ withBaseURI
120
+ ? this._generateBaseUri(chunk, rootOutputDir)
121
+ : '// no baseURI',
122
+ '',
123
+ '// object to store loaded chunks',
124
+ '// "0" means "already loaded", Promise means loading',
125
+ `var installedChunks = ${
126
+ stateExpression ? `${stateExpression} = ${stateExpression} || ` : ''
127
+ }{`,
128
+ Template.indent(
129
+ Array.from(initialChunkIds, (id) => `${JSON.stringify(id)}: 0`).join(
130
+ ',\n'
131
+ )
132
+ ),
133
+ '};',
134
+ '',
135
+ withOnChunkLoad
136
+ ? `${
137
+ RuntimeGlobals.onChunksLoaded
138
+ }.readFileVm = ${runtimeTemplate.returningFunction(
139
+ 'installedChunks[chunkId] === 0',
140
+ 'chunkId'
141
+ )};`
142
+ : '// no on chunks loaded',
143
+ '',
144
+ withLoading || withExternalInstallChunk
145
+ ? `var installChunk = ${runtimeTemplate.basicFunction('chunk', [
146
+ 'var moreModules = chunk.modules, chunkIds = chunk.ids, runtime = chunk.runtime;',
147
+ 'for(var moduleId in moreModules) {',
148
+ Template.indent([
149
+ `if(${RuntimeGlobals.hasOwnProperty}(moreModules, moduleId)) {`,
150
+ Template.indent([
151
+ `${RuntimeGlobals.moduleFactories}[moduleId] = moreModules[moduleId];`,
152
+ ]),
153
+ '}',
154
+ ]),
155
+ '}',
156
+ `if(runtime) runtime(__webpack_require__);`,
157
+ 'for(var i = 0; i < chunkIds.length; i++) {',
158
+ Template.indent([
159
+ 'if(installedChunks[chunkIds[i]]) {',
160
+ Template.indent(['installedChunks[chunkIds[i]][0]();']),
161
+ '}',
162
+ 'installedChunks[chunkIds[i]] = 0;',
163
+ ]),
164
+ '}',
165
+ withOnChunkLoad ? `${RuntimeGlobals.onChunksLoaded}();` : '',
166
+ ])};`
167
+ : '// no chunk install function needed',
168
+ '',
169
+ withLoading
170
+ ? Template.asString([
171
+ '// ReadFile + VM.run chunk loading for javascript',
172
+ `${fn}.readFileVm = function(chunkId, promises) {`,
173
+ hasJsMatcher !== false
174
+ ? Template.indent([
175
+ '',
176
+ 'var installedChunkData = installedChunks[chunkId];',
177
+ 'if(installedChunkData !== 0) { // 0 means "already installed".',
178
+ Template.indent([
179
+ '// array of [resolve, reject, promise] means "currently loading"',
180
+ 'if(installedChunkData) {',
181
+ Template.indent(['promises.push(installedChunkData[2]);']),
182
+ '} else {',
183
+ Template.indent([
184
+ hasJsMatcher === true
185
+ ? 'if(true) { // all chunks have JS'
186
+ : `if(${hasJsMatcher('chunkId')}) {`,
187
+ Template.indent([
188
+ '// load the chunk and return promise to it',
189
+ 'var promise = new Promise(async function(resolve, reject) {',
190
+ Template.indent([
191
+ 'installedChunkData = installedChunks[chunkId] = [resolve, reject];',
192
+ `var filename = require('path').join(__dirname, ${JSON.stringify(
193
+ rootOutputDir
194
+ )} + ${
195
+ RuntimeGlobals.getChunkScriptFilename
196
+ }(chunkId));`,
197
+ "var fs = require('fs');",
198
+ 'if(fs.existsSync(filename)) {',
199
+ Template.indent([
200
+ "fs.readFile(filename, 'utf-8', function(err, content) {",
201
+ Template.indent([
202
+ 'if(err) return reject(err);',
203
+ 'var chunk = {};',
204
+ "require('vm').runInThisContext('(function(exports, require, __dirname, __filename) {' + content + '\\n})', filename)" +
205
+ "(chunk, require, require('path').dirname(filename), filename);",
206
+ 'installChunk(chunk);',
207
+ ]),
208
+ '});',
209
+ ]),
210
+ '} else {',
211
+ Template.indent([
212
+ loadScript,
213
+
214
+ `console.log('needs to load remote module from', ${JSON.stringify(
215
+ name
216
+ )});`,
217
+ `console.log('remotes known to', ${JSON.stringify(
218
+ name
219
+ )}, ${JSON.stringify(remotes)})`,
220
+ // keys are mostly useless here, we want to find remote by its global (unique name)
221
+ `var remotes = ${JSON.stringify(
222
+ Object.values(remotes).reduce((acc, remote) => {
223
+ //TODO: need to handle all other cases like when remote is not a @ syntax string
224
+ const [global, url] = remote.split('@');
225
+ acc[global] = url;
226
+ return acc;
227
+ }, {})
228
+ )};`,
229
+ "Object.assign(global.__remote_scope__._config, remotes)",
230
+ "const remoteRegistry = global.__remote_scope__._config",
231
+ /* TODO: keying by global should be ok, but need to verify - need to deal with when user passes promise new promise()
232
+ global will/should still exist - but can only be known at runtime */
233
+ `console.log('remotes keyed by global name',remotes)`,
234
+ `console.log('remote scope configs',global.__remote_scope__._config)`,
235
+
236
+ `console.log('global.__remote_scope__',global.__remote_scope__)`,
237
+ `console.log('global.__remote_scope__[${JSON.stringify(
238
+ name
239
+ )}]',global.__remote_scope__[${JSON.stringify(
240
+ name
241
+ )}])`,
242
+
243
+ /* TODO: this global.REMOTE_CONFIG doesnt work in this v5 core, not sure if i need to keep it or not
244
+ not deleting it yet since i might need this for tracking all the remote entries across systems
245
+ for now, im going to use locally known remote scope from remoteEntry config
246
+ update: We will most likely need this, since remote would not have its own config
247
+ id need to access the host system and find the known url
248
+ basically this is how i create publicPath: auto on servers.
249
+ `var requestedRemote = global.REMOTE_CONFIG[${JSON.stringify(
250
+ name
251
+ )}]`,
252
+ */
253
+ "console.log('about to derive remote making request')",
254
+ `var requestedRemote = remoteRegistry[${JSON.stringify(
255
+ name
256
+ )}]`,
257
+ "console.log('requested remote', requestedRemote)",
258
+ /*TODO: we need to support when user implements own promise new promise function
259
+ for example i have my own promise remotes, not global@remotename
260
+ so there could be cases where remote may be function still - not sure */
261
+
262
+ /*TODO: need to handle if chunk fetch fails/crashes - ensure server still can keep loading
263
+ right now if you throw an error in here, server will stall forever */
264
+
265
+ `if(typeof requestedRemote === 'function'){
266
+ requestedRemote = await requestedRemote()
267
+ }`,
268
+ `console.log('var requestedRemote',requestedRemote);`,
269
+
270
+ // example: uncomment this and server will never reply
271
+ // `var scriptUrl = new URL(requestedRemote.split("@")[1]);`,
272
+ // since im looping over remote and creating global at build time, i dont need to split string at runtime
273
+ // there may still be a use case for that with promise new promise, depending on how we design it.
274
+ `var scriptUrl = new URL(requestedRemote);`,
275
+
276
+ `var chunkName = ${RuntimeGlobals.getChunkScriptFilename}(chunkId);`,
277
+
278
+ `console.log('chunkname to request',chunkName);`,
279
+ `var fileToReplace = require('path').basename(scriptUrl.pathname);`,
280
+ `scriptUrl.pathname = scriptUrl.pathname.replace(fileToReplace, chunkName);`,
281
+ `console.log('will load remote chunk', scriptUrl.toString());`,
282
+ `loadScript(scriptUrl.toString(), function(err, content) {`,
283
+ Template.indent([
284
+ "console.log('load script callback fired')",
285
+ "if(err) {console.error('error loading remote chunk', scriptUrl.toString(),'got',content); return reject(err);}",
286
+ 'var chunk = {};',
287
+ 'try {',
288
+ "require('vm').runInThisContext('(function(exports, require, __dirname, __filename) {' + content + '\\n})', filename)" +
289
+ "(chunk, require, require('path').dirname(filename), filename);",
290
+ '} catch (e) {',
291
+ "console.log('runInThisContext thew', e)",
292
+ '}',
293
+ 'installChunk(chunk);',
294
+ ]),
295
+ '});',
296
+ ]),
297
+ '}',
298
+ ]),
299
+ '});',
300
+ 'promises.push(installedChunkData[2] = promise);',
301
+ ]),
302
+ '} else installedChunks[chunkId] = 0;',
303
+ ]),
304
+ '}',
305
+ ]),
306
+ '}',
307
+ ])
308
+ : Template.indent(['installedChunks[chunkId] = 0;']),
309
+ '};',
310
+ ])
311
+ : '// no chunk loading',
312
+ '',
313
+ withExternalInstallChunk
314
+ ? Template.asString([
315
+ 'module.exports = __webpack_require__;',
316
+ `${RuntimeGlobals.externalInstallChunk} = installChunk;`,
317
+ ])
318
+ : '// no external install chunk',
319
+ '',
320
+ withHmr
321
+ ? Template.asString([
322
+ 'function loadUpdateChunk(chunkId, updatedModulesList) {',
323
+ Template.indent([
324
+ 'return new Promise(function(resolve, reject) {',
325
+ Template.indent([
326
+ `var filename = require('path').join(__dirname, ${JSON.stringify(
327
+ rootOutputDir
328
+ )} + ${RuntimeGlobals.getChunkUpdateScriptFilename}(chunkId));`,
329
+ "require('fs').readFile(filename, 'utf-8', function(err, content) {",
330
+ Template.indent([
331
+ 'if(err) return reject(err);',
332
+ 'var update = {};',
333
+ "require('vm').runInThisContext('(function(exports, require, __dirname, __filename) {' + content + '\\n})', filename)" +
334
+ "(update, require, require('path').dirname(filename), filename);",
335
+ 'var updatedModules = update.modules;',
336
+ 'var runtime = update.runtime;',
337
+ 'for(var moduleId in updatedModules) {',
338
+ Template.indent([
339
+ `if(${RuntimeGlobals.hasOwnProperty}(updatedModules, moduleId)) {`,
340
+ Template.indent([
341
+ `currentUpdate[moduleId] = updatedModules[moduleId];`,
342
+ 'if(updatedModulesList) updatedModulesList.push(moduleId);',
343
+ ]),
344
+ '}',
345
+ ]),
346
+ '}',
347
+ 'if(runtime) currentUpdateRuntime.push(runtime);',
348
+ 'resolve();',
349
+ ]),
350
+ '});',
351
+ ]),
352
+ '});',
353
+ ]),
354
+ '}',
355
+ '',
356
+ Template.getFunctionContent(
357
+ require('../hmr/JavascriptHotModuleReplacement.runtime.js')
358
+ )
359
+ .replace(/\$key\$/g, 'readFileVm')
360
+ .replace(/\$installedChunks\$/g, 'installedChunks')
361
+ .replace(/\$loadUpdateChunk\$/g, 'loadUpdateChunk')
362
+ .replace(/\$moduleCache\$/g, RuntimeGlobals.moduleCache)
363
+ .replace(/\$moduleFactories\$/g, RuntimeGlobals.moduleFactories)
364
+ .replace(
365
+ /\$ensureChunkHandlers\$/g,
366
+ RuntimeGlobals.ensureChunkHandlers
367
+ )
368
+ .replace(/\$hasOwnProperty\$/g, RuntimeGlobals.hasOwnProperty)
369
+ .replace(/\$hmrModuleData\$/g, RuntimeGlobals.hmrModuleData)
370
+ .replace(
371
+ /\$hmrDownloadUpdateHandlers\$/g,
372
+ RuntimeGlobals.hmrDownloadUpdateHandlers
373
+ )
374
+ .replace(
375
+ /\$hmrInvalidateModuleHandlers\$/g,
376
+ RuntimeGlobals.hmrInvalidateModuleHandlers
377
+ ),
378
+ ])
379
+ : '// no HMR',
380
+ '',
381
+ withHmrManifest
382
+ ? Template.asString([
383
+ `${RuntimeGlobals.hmrDownloadManifest} = function() {`,
384
+ Template.indent([
385
+ 'return new Promise(function(resolve, reject) {',
386
+ Template.indent([
387
+ `var filename = require('path').join(__dirname, ${JSON.stringify(
388
+ rootOutputDir
389
+ )} + ${RuntimeGlobals.getUpdateManifestFilename}());`,
390
+ "require('fs').readFile(filename, 'utf-8', function(err, content) {",
391
+ Template.indent([
392
+ 'if(err) {',
393
+ Template.indent([
394
+ 'if(err.code === "ENOENT") return resolve();',
395
+ 'return reject(err);',
396
+ ]),
397
+ '}',
398
+ 'try { resolve(JSON.parse(content)); }',
399
+ 'catch(e) { reject(e); }',
400
+ ]),
401
+ '});',
402
+ ]),
403
+ '});',
404
+ ]),
405
+ '}',
406
+ ])
407
+ : '// no HMR manifest',
408
+ ]);
409
+ }
410
+ }
411
+
412
+ module.exports = ReadFileChunkLoadingRuntimeModule;