@module-federation/node 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 ScriptedAlchemy LLC (Zack Jackson)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # Module Federation Support for Node Environments
2
+
3
+ This package exposes two Webpack Plugins to bring the concept and power of Module Federation to NodeJS. This will allow your server to fetch chunks across the network allowing for distributed deployments of federated applications.
4
+
5
+ ## Installation
6
+
7
+ To install the plugin run one of the following commands in your terminal for your application.
8
+
9
+ ```bash
10
+ # npm
11
+ npm install @module-federation/node
12
+
13
+ # yarn
14
+ yarn add @module-federation/node
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ To then use the plugin, modify your `webpack.config.js` to include and use the two plugins.
20
+
21
+ At build time, you need to be aware if you're building for the `server` or for the `browser`.
22
+ If it's building for server, we need to set `target: false` to allow the plugins to function correctly.
23
+
24
+ The `NodeFederationPlugin` follows the same API as the [Module Federation Plugin](https://webpack.js.org/plugins/module-federation-plugin) and therefore should be a drop-in replacement if you already have it set up in your `webpack.config.js`.
25
+
26
+ An example configuration is presented below:
27
+ ```js
28
+
29
+ const {NodeFederationPlugin, StreamingTargetPlugin} = require("@module-federation/node");
30
+
31
+ const config = {
32
+ target: isServer ? false : "web",
33
+ plugins: [
34
+ new NodeFederationPlugin({
35
+ name: 'website2',
36
+ library: {type: 'commonjs-module'},
37
+ remotes: {},
38
+ filename: 'remoteEntry.js',
39
+ exposes: {
40
+ './SharedComponent': './remoteServer/SharedComponent',
41
+ },
42
+ }),
43
+ new StreamingTargetPlugin({
44
+ name: 'website2',
45
+ library: {type: 'commonjs-module'},
46
+ remotes: {},
47
+ }),
48
+ ]
49
+ }
50
+ ```
package/index.js ADDED
@@ -0,0 +1,5 @@
1
+ const NodeFederationPlugin = require('./src/NodeFederationPlugin');
2
+ const StreamingTargetPlugin = require('./src/StreamingTargetPlugin');
3
+
4
+ module.exports.NodeFederationPlugin = NodeFederationPlugin;
5
+ module.exports.StreamingTargetPlugin = StreamingTargetPlugin;
package/package.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "name": "@module-federation/node",
3
+ "version": "0.0.1",
4
+ "description": "Module Federation for Node.js",
5
+ "main": "index.js",
6
+ "peerDependencies": {
7
+ "webpack": "5.40.0",
8
+ "node-fetch": "^3.2.10"
9
+ }
10
+ }
@@ -0,0 +1,89 @@
1
+ const RuntimeGlobals = require('webpack/lib/RuntimeGlobals');
2
+ const StartupChunkDependenciesPlugin = require('webpack/lib/runtime/StartupChunkDependenciesPlugin');
3
+ const ChunkLoadingRuntimeModule = require('./LoadFileChunkLoadingRuntimeModule');
4
+
5
+ class CommonJsChunkLoadingPlugin {
6
+ constructor(options) {
7
+ this.options = options || {};
8
+ this._asyncChunkLoading = this.options.asyncChunkLoading;
9
+ }
10
+
11
+ /**
12
+ * Apply the plugin
13
+ * @param {Compiler} compiler the compiler instance
14
+ * @returns {void}
15
+ */
16
+ apply(compiler) {
17
+ const chunkLoadingValue = this._asyncChunkLoading
18
+ ? 'async-node'
19
+ : 'require';
20
+ new StartupChunkDependenciesPlugin({
21
+ chunkLoading: chunkLoadingValue,
22
+ asyncChunkLoading: this._asyncChunkLoading,
23
+ }).apply(compiler);
24
+ compiler.hooks.thisCompilation.tap(
25
+ 'CommonJsChunkLoadingPlugin',
26
+ (compilation) => {
27
+ // Always enabled
28
+ const isEnabledForChunk = () => true;
29
+ const onceForChunkSet = new WeakSet();
30
+ const handler = (chunk, set) => {
31
+ if (onceForChunkSet.has(chunk)) return;
32
+ onceForChunkSet.add(chunk);
33
+ if (!isEnabledForChunk(chunk)) return;
34
+ set.add(RuntimeGlobals.moduleFactoriesAddOnly);
35
+ set.add(RuntimeGlobals.hasOwnProperty);
36
+ compilation.addRuntimeModule(
37
+ chunk,
38
+ new ChunkLoadingRuntimeModule(set, this.options, {
39
+ webpack: compiler.webpack,
40
+ })
41
+ );
42
+ };
43
+
44
+ compilation.hooks.runtimeRequirementInTree
45
+ .for(RuntimeGlobals.ensureChunkHandlers)
46
+ .tap('CommonJsChunkLoadingPlugin', handler);
47
+ compilation.hooks.runtimeRequirementInTree
48
+ .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
49
+ .tap('CommonJsChunkLoadingPlugin', handler);
50
+ compilation.hooks.runtimeRequirementInTree
51
+ .for(RuntimeGlobals.hmrDownloadManifest)
52
+ .tap('CommonJsChunkLoadingPlugin', handler);
53
+ compilation.hooks.runtimeRequirementInTree
54
+ .for(RuntimeGlobals.baseURI)
55
+ .tap('CommonJsChunkLoadingPlugin', handler);
56
+ compilation.hooks.runtimeRequirementInTree
57
+ .for(RuntimeGlobals.externalInstallChunk)
58
+ .tap('CommonJsChunkLoadingPlugin', handler);
59
+ compilation.hooks.runtimeRequirementInTree
60
+ .for(RuntimeGlobals.onChunksLoaded)
61
+ .tap('CommonJsChunkLoadingPlugin', handler);
62
+
63
+ compilation.hooks.runtimeRequirementInTree
64
+ .for(RuntimeGlobals.ensureChunkHandlers)
65
+ .tap('CommonJsChunkLoadingPlugin', (chunk, set) => {
66
+ if (!isEnabledForChunk(chunk)) return;
67
+ set.add(RuntimeGlobals.getChunkScriptFilename);
68
+ });
69
+ compilation.hooks.runtimeRequirementInTree
70
+ .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
71
+ .tap('CommonJsChunkLoadingPlugin', (chunk, set) => {
72
+ if (!isEnabledForChunk(chunk)) return;
73
+ set.add(RuntimeGlobals.getChunkUpdateScriptFilename);
74
+ set.add(RuntimeGlobals.moduleCache);
75
+ set.add(RuntimeGlobals.hmrModuleData);
76
+ set.add(RuntimeGlobals.moduleFactoriesAddOnly);
77
+ });
78
+ compilation.hooks.runtimeRequirementInTree
79
+ .for(RuntimeGlobals.hmrDownloadManifest)
80
+ .tap('CommonJsChunkLoadingPlugin', (chunk, set) => {
81
+ if (!isEnabledForChunk(chunk)) return;
82
+ set.add(RuntimeGlobals.getUpdateManifestFilename);
83
+ });
84
+ }
85
+ );
86
+ }
87
+ }
88
+
89
+ module.exports = CommonJsChunkLoadingPlugin;
@@ -0,0 +1,410 @@
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ */
4
+
5
+ 'use strict';
6
+
7
+ const RuntimeGlobals = require('webpack/lib/RuntimeGlobals');
8
+ const RuntimeModule = require('webpack/lib/RuntimeModule');
9
+ const Template = require('webpack/lib/Template');
10
+ const compileBooleanMatcher = require('webpack/lib/util/compileBooleanMatcher');
11
+ const { getUndoPath } = require('webpack/lib/util/identifier');
12
+
13
+ const loadScriptTemplate = require('./loadScript')
14
+
15
+ class ReadFileChunkLoadingRuntimeModule extends RuntimeModule {
16
+ constructor(runtimeRequirements, options, context) {
17
+ super('readFile chunk loading', RuntimeModule.STAGE_ATTACH);
18
+ this.runtimeRequirements = runtimeRequirements;
19
+ this.options = options;
20
+ this.context = context;
21
+ }
22
+
23
+ /**
24
+ * @private
25
+ * @param {Chunk} chunk chunk
26
+ * @param {string} rootOutputDir root output directory
27
+ * @returns {string} generated code
28
+ */
29
+ _generateBaseUri(chunk, rootOutputDir) {
30
+ const options = chunk.getEntryOptions();
31
+ if (options && options.baseUri) {
32
+ return `${RuntimeGlobals.baseURI} = ${JSON.stringify(options.baseUri)};`;
33
+ }
34
+
35
+ return `${RuntimeGlobals.baseURI} = require("url").pathToFileURL(${
36
+ rootOutputDir
37
+ ? `__dirname + ${JSON.stringify('/' + rootOutputDir)}`
38
+ : '__filename'
39
+ });`;
40
+ }
41
+
42
+ /**
43
+ * @returns {string} runtime code
44
+ */
45
+ generate() {
46
+ // name in this context is always the current remote itself.
47
+ // this code below is in each webpack runtime, host and remotes
48
+ // remote entries handle their own loading of chunks, so i have fractal self awareness
49
+ // hosts will likely never run the http chunk loading runtime, they use fs.readFile
50
+ // remotes only use fs.readFile if we were to cache the chunks on disk after fetching - otherwise its always using http
51
+ // so for example, if im in hostA and require(remoteb/module) --> console.log of name in runtime code will return remoteb
52
+
53
+ const { baseURI, promiseBaseURI, remotes, name } = this.options;
54
+ const { webpack } = this.context;
55
+ const chunkHasJs =
56
+ (webpack && webpack.javascript.JavascriptModulesPlugin.chunkHasJs) ||
57
+ require('webpack/lib/javascript/JavascriptModulesPlugin').chunkHasJs;
58
+
59
+ // workaround for next.js
60
+ const getInitialChunkIds = (chunk, chunkGraph) => {
61
+ const initialChunkIds = new Set(chunk.ids);
62
+ for (const c of chunk.getAllInitialChunks()) {
63
+ if (c === chunk || chunkHasJs(c, chunkGraph)) continue;
64
+ for (const id of c.ids) initialChunkIds.add(id);
65
+ }
66
+ return initialChunkIds;
67
+ };
68
+
69
+ const { chunkGraph, chunk } = this;
70
+ const { runtimeTemplate } = this.compilation;
71
+ const fn = RuntimeGlobals.ensureChunkHandlers;
72
+ const withBaseURI = this.runtimeRequirements.has(RuntimeGlobals.baseURI);
73
+ const withExternalInstallChunk = this.runtimeRequirements.has(
74
+ RuntimeGlobals.externalInstallChunk
75
+ );
76
+ const withOnChunkLoad = this.runtimeRequirements.has(
77
+ RuntimeGlobals.onChunksLoaded
78
+ );
79
+ const withLoading = this.runtimeRequirements.has(
80
+ RuntimeGlobals.ensureChunkHandlers
81
+ );
82
+ const withHmr = this.runtimeRequirements.has(
83
+ RuntimeGlobals.hmrDownloadUpdateHandlers
84
+ );
85
+ const withHmrManifest = this.runtimeRequirements.has(
86
+ RuntimeGlobals.hmrDownloadManifest
87
+ );
88
+
89
+ const conditionMap = chunkGraph.getChunkConditionMap(chunk, chunkHasJs);
90
+ const hasJsMatcher = compileBooleanMatcher(conditionMap);
91
+ const initialChunkIds = getInitialChunkIds(chunk, chunkGraph, chunkHasJs);
92
+
93
+ const outputName = this.compilation.getPath(
94
+ (
95
+ (webpack &&
96
+ webpack.javascript.JavascriptModulesPlugin
97
+ .getChunkFilenameTemplate) ||
98
+ require('webpack/lib/javascript/JavascriptModulesPlugin')
99
+ .getChunkFilenameTemplate
100
+ )(chunk, this.compilation.outputOptions),
101
+ {
102
+ chunk,
103
+ contentHashType: 'javascript',
104
+ }
105
+ );
106
+ const rootOutputDir = getUndoPath(
107
+ outputName,
108
+ this.compilation.outputOptions.path,
109
+ false
110
+ );
111
+
112
+ const stateExpression = withHmr
113
+ ? `${RuntimeGlobals.hmrRuntimeStatePrefix}_readFileVm`
114
+ : undefined;
115
+
116
+ return Template.asString([
117
+ withBaseURI
118
+ ? this._generateBaseUri(chunk, rootOutputDir)
119
+ : '// no baseURI',
120
+ '',
121
+ '// object to store loaded chunks',
122
+ '// "0" means "already loaded", Promise means loading',
123
+ `var installedChunks = ${
124
+ stateExpression ? `${stateExpression} = ${stateExpression} || ` : ''
125
+ }{`,
126
+ Template.indent(
127
+ Array.from(initialChunkIds, (id) => `${JSON.stringify(id)}: 0`).join(
128
+ ',\n'
129
+ )
130
+ ),
131
+ '};',
132
+ '',
133
+ withOnChunkLoad
134
+ ? `${
135
+ RuntimeGlobals.onChunksLoaded
136
+ }.readFileVm = ${runtimeTemplate.returningFunction(
137
+ 'installedChunks[chunkId] === 0',
138
+ 'chunkId'
139
+ )};`
140
+ : '// no on chunks loaded',
141
+ '',
142
+ withLoading || withExternalInstallChunk
143
+ ? `var installChunk = ${runtimeTemplate.basicFunction('chunk', [
144
+ 'var moreModules = chunk.modules, chunkIds = chunk.ids, runtime = chunk.runtime;',
145
+ 'for(var moduleId in moreModules) {',
146
+ Template.indent([
147
+ `if(${RuntimeGlobals.hasOwnProperty}(moreModules, moduleId)) {`,
148
+ Template.indent([
149
+ `${RuntimeGlobals.moduleFactories}[moduleId] = moreModules[moduleId];`,
150
+ ]),
151
+ '}',
152
+ ]),
153
+ '}',
154
+ `if(runtime) runtime(__webpack_require__);`,
155
+ 'for(var i = 0; i < chunkIds.length; i++) {',
156
+ Template.indent([
157
+ 'if(installedChunks[chunkIds[i]]) {',
158
+ Template.indent(['installedChunks[chunkIds[i]][0]();']),
159
+ '}',
160
+ 'installedChunks[chunkIds[i]] = 0;',
161
+ ]),
162
+ '}',
163
+ withOnChunkLoad ? `${RuntimeGlobals.onChunksLoaded}();` : '',
164
+ ])};`
165
+ : '// no chunk install function needed',
166
+ '',
167
+ withLoading
168
+ ? Template.asString([
169
+ '// ReadFile + VM.run chunk loading for javascript',
170
+ `${fn}.readFileVm = function(chunkId, promises) {`,
171
+ hasJsMatcher !== false
172
+ ? Template.indent([
173
+ '',
174
+ 'var installedChunkData = installedChunks[chunkId];',
175
+ 'if(installedChunkData !== 0) { // 0 means "already installed".',
176
+ Template.indent([
177
+ '// array of [resolve, reject, promise] means "currently loading"',
178
+ 'if(installedChunkData) {',
179
+ Template.indent(['promises.push(installedChunkData[2]);']),
180
+ '} else {',
181
+ Template.indent([
182
+ hasJsMatcher === true
183
+ ? 'if(true) { // all chunks have JS'
184
+ : `if(${hasJsMatcher('chunkId')}) {`,
185
+ Template.indent([
186
+ '// load the chunk and return promise to it',
187
+ 'var promise = new Promise(async function(resolve, reject) {',
188
+ Template.indent([
189
+ 'installedChunkData = installedChunks[chunkId] = [resolve, reject];',
190
+ `var filename = require('path').join(__dirname, ${JSON.stringify(
191
+ rootOutputDir
192
+ )} + ${
193
+ RuntimeGlobals.getChunkScriptFilename
194
+ }(chunkId));`,
195
+ "var fs = require('fs');",
196
+ 'if(fs.existsSync(filename)) {',
197
+ Template.indent([
198
+ "fs.readFile(filename, 'utf-8', function(err, content) {",
199
+ Template.indent([
200
+ 'if(err) return reject(err);',
201
+ 'var chunk = {};',
202
+ "require('vm').runInThisContext('(function(exports, require, __dirname, __filename) {' + content + '\\n})', filename)" +
203
+ "(chunk, require, require('path').dirname(filename), filename);",
204
+ 'installChunk(chunk);',
205
+ ]),
206
+ '});',
207
+ ]),
208
+ '} else {',
209
+ Template.indent([
210
+ loadScriptTemplate,
211
+
212
+ `console.log('needs to load remote module from', ${JSON.stringify(
213
+ name
214
+ )});`,
215
+ `console.log('remotes known to', ${JSON.stringify(
216
+ name
217
+ )}, ${JSON.stringify(remotes)})`,
218
+ // keys are mostly useless here, we want to find remote by its global (unique name)
219
+ `var remotes = ${JSON.stringify(
220
+ Object.values(remotes).reduce((acc, remote) => {
221
+ //TODO: need to handle all other cases like when remote is not a @ syntax string
222
+ const [global, url] = remote.split('@');
223
+ acc[global] = url;
224
+ return acc;
225
+ }, {})
226
+ )};`,
227
+ "Object.assign(global.__remote_scope__._config, remotes)",
228
+ "const remoteRegistry = global.__remote_scope__._config",
229
+ /* TODO: keying by global should be ok, but need to verify - need to deal with when user passes promise new promise()
230
+ global will/should still exist - but can only be known at runtime */
231
+ `console.log('remotes keyed by global name',remotes)`,
232
+ `console.log('remote scope configs',global.__remote_scope__._config)`,
233
+
234
+ `console.log('global.__remote_scope__',global.__remote_scope__)`,
235
+ `console.log('global.__remote_scope__[${JSON.stringify(
236
+ name
237
+ )}]',global.__remote_scope__[${JSON.stringify(
238
+ name
239
+ )}])`,
240
+
241
+ /* TODO: this global.REMOTE_CONFIG doesnt work in this v5 core, not sure if i need to keep it or not
242
+ not deleting it yet since i might need this for tracking all the remote entries across systems
243
+ for now, im going to use locally known remote scope from remoteEntry config
244
+ update: We will most likely need this, since remote would not have its own config
245
+ id need to access the host system and find the known url
246
+ basically this is how i create publicPath: auto on servers.
247
+ `var requestedRemote = global.REMOTE_CONFIG[${JSON.stringify(
248
+ name
249
+ )}]`,
250
+ */
251
+ "console.log('about to derive remote making request')",
252
+ `var requestedRemote = remoteRegistry[${JSON.stringify(
253
+ name
254
+ )}]`,
255
+ "console.log('requested remote', requestedRemote)",
256
+ /*TODO: we need to support when user implements own promise new promise function
257
+ for example i have my own promise remotes, not global@remotename
258
+ so there could be cases where remote may be function still - not sure */
259
+
260
+ /*TODO: need to handle if chunk fetch fails/crashes - ensure server still can keep loading
261
+ right now if you throw an error in here, server will stall forever */
262
+
263
+ `if(typeof requestedRemote === 'function'){
264
+ requestedRemote = await requestedRemote()
265
+ }`,
266
+ `console.log('var requestedRemote',requestedRemote);`,
267
+
268
+ // example: uncomment this and server will never reply
269
+ // `var scriptUrl = new URL(requestedRemote.split("@")[1]);`,
270
+ // since im looping over remote and creating global at build time, i dont need to split string at runtime
271
+ // there may still be a use case for that with promise new promise, depending on how we design it.
272
+ `var scriptUrl = new URL(requestedRemote);`,
273
+
274
+ `var chunkName = ${RuntimeGlobals.getChunkScriptFilename}(chunkId);`,
275
+
276
+ `console.log('chunkname to request',chunkName);`,
277
+ `var fileToReplace = require('path').basename(scriptUrl.pathname);`,
278
+ `scriptUrl.pathname = scriptUrl.pathname.replace(fileToReplace, chunkName);`,
279
+ `console.log('will load remote chunk', scriptUrl.toString());`,
280
+ `loadScript(scriptUrl.toString(), function(err, content) {`,
281
+ Template.indent([
282
+ "console.log('load script callback fired')",
283
+ "if(err) {console.error('error loading remote chunk', scriptUrl.toString(),'got',content); return reject(err);}",
284
+ 'var chunk = {};',
285
+ 'try {',
286
+ "require('vm').runInThisContext('(function(exports, require, __dirname, __filename) {' + content + '\\n})', filename)" +
287
+ "(chunk, require, require('path').dirname(filename), filename);",
288
+ '} catch (e) {',
289
+ "console.log('runInThisContext thew', e)",
290
+ '}',
291
+ 'installChunk(chunk);',
292
+ ]),
293
+ '});',
294
+ ]),
295
+ '}',
296
+ ]),
297
+ '});',
298
+ 'promises.push(installedChunkData[2] = promise);',
299
+ ]),
300
+ '} else installedChunks[chunkId] = 0;',
301
+ ]),
302
+ '}',
303
+ ]),
304
+ '}',
305
+ ])
306
+ : Template.indent(['installedChunks[chunkId] = 0;']),
307
+ '};',
308
+ ])
309
+ : '// no chunk loading',
310
+ '',
311
+ withExternalInstallChunk
312
+ ? Template.asString([
313
+ 'module.exports = __webpack_require__;',
314
+ `${RuntimeGlobals.externalInstallChunk} = installChunk;`,
315
+ ])
316
+ : '// no external install chunk',
317
+ '',
318
+ withHmr
319
+ ? Template.asString([
320
+ 'function loadUpdateChunk(chunkId, updatedModulesList) {',
321
+ Template.indent([
322
+ 'return new Promise(function(resolve, reject) {',
323
+ Template.indent([
324
+ `var filename = require('path').join(__dirname, ${JSON.stringify(
325
+ rootOutputDir
326
+ )} + ${RuntimeGlobals.getChunkUpdateScriptFilename}(chunkId));`,
327
+ "require('fs').readFile(filename, 'utf-8', function(err, content) {",
328
+ Template.indent([
329
+ 'if(err) return reject(err);',
330
+ 'var update = {};',
331
+ "require('vm').runInThisContext('(function(exports, require, __dirname, __filename) {' + content + '\\n})', filename)" +
332
+ "(update, require, require('path').dirname(filename), filename);",
333
+ 'var updatedModules = update.modules;',
334
+ 'var runtime = update.runtime;',
335
+ 'for(var moduleId in updatedModules) {',
336
+ Template.indent([
337
+ `if(${RuntimeGlobals.hasOwnProperty}(updatedModules, moduleId)) {`,
338
+ Template.indent([
339
+ `currentUpdate[moduleId] = updatedModules[moduleId];`,
340
+ 'if(updatedModulesList) updatedModulesList.push(moduleId);',
341
+ ]),
342
+ '}',
343
+ ]),
344
+ '}',
345
+ 'if(runtime) currentUpdateRuntime.push(runtime);',
346
+ 'resolve();',
347
+ ]),
348
+ '});',
349
+ ]),
350
+ '});',
351
+ ]),
352
+ '}',
353
+ '',
354
+ Template.getFunctionContent(
355
+ require('../hmr/JavascriptHotModuleReplacement.runtime.js')
356
+ )
357
+ .replace(/\$key\$/g, 'readFileVm')
358
+ .replace(/\$installedChunks\$/g, 'installedChunks')
359
+ .replace(/\$loadUpdateChunk\$/g, 'loadUpdateChunk')
360
+ .replace(/\$moduleCache\$/g, RuntimeGlobals.moduleCache)
361
+ .replace(/\$moduleFactories\$/g, RuntimeGlobals.moduleFactories)
362
+ .replace(
363
+ /\$ensureChunkHandlers\$/g,
364
+ RuntimeGlobals.ensureChunkHandlers
365
+ )
366
+ .replace(/\$hasOwnProperty\$/g, RuntimeGlobals.hasOwnProperty)
367
+ .replace(/\$hmrModuleData\$/g, RuntimeGlobals.hmrModuleData)
368
+ .replace(
369
+ /\$hmrDownloadUpdateHandlers\$/g,
370
+ RuntimeGlobals.hmrDownloadUpdateHandlers
371
+ )
372
+ .replace(
373
+ /\$hmrInvalidateModuleHandlers\$/g,
374
+ RuntimeGlobals.hmrInvalidateModuleHandlers
375
+ ),
376
+ ])
377
+ : '// no HMR',
378
+ '',
379
+ withHmrManifest
380
+ ? Template.asString([
381
+ `${RuntimeGlobals.hmrDownloadManifest} = function() {`,
382
+ Template.indent([
383
+ 'return new Promise(function(resolve, reject) {',
384
+ Template.indent([
385
+ `var filename = require('path').join(__dirname, ${JSON.stringify(
386
+ rootOutputDir
387
+ )} + ${RuntimeGlobals.getUpdateManifestFilename}());`,
388
+ "require('fs').readFile(filename, 'utf-8', function(err, content) {",
389
+ Template.indent([
390
+ 'if(err) {',
391
+ Template.indent([
392
+ 'if(err.code === "ENOENT") return resolve();',
393
+ 'return reject(err);',
394
+ ]),
395
+ '}',
396
+ 'try { resolve(JSON.parse(content)); }',
397
+ 'catch(e) { reject(e); }',
398
+ ]),
399
+ '});',
400
+ ]),
401
+ '});',
402
+ ]),
403
+ '}',
404
+ ])
405
+ : '// no HMR manifest',
406
+ ]);
407
+ }
408
+ }
409
+
410
+ module.exports = ReadFileChunkLoadingRuntimeModule;
@@ -0,0 +1,245 @@
1
+ 'use strict';
2
+ // possible remote evaluators
3
+ // this depends on the chunk format selected.
4
+ // commonjs2 - it think, is lazily evaluated - beware
5
+ // const remote = eval(scriptContent + '\n try{' + moduleName + '}catch(e) { null; };');
6
+ // commonjs - fine to use but exports marker doesnt exist
7
+ // const remote = eval('let exports = {};' + scriptContent + 'exports');
8
+ // commonjs-module, ideal since it returns a commonjs module format
9
+ // const remote = eval(scriptContent + 'module.exports')
10
+
11
+ // Note on ESM.
12
+ // Its possible to use ESM import, but its impossible to invalidate the module cache
13
+ // So once something is imported, its stuck. This is problematic with at-runtime since we want to hot reload node
14
+ // if ESM were used, youd be forced to restart the process to re-import modules or use a worker pool
15
+ // Workaround is possible with query string on end of request, but this leaks memory badly
16
+ // with commonjs, we can at least reset the require cache to "reboot" webpack runtime
17
+ // It *can* leak memory, but ive not been able to replicate this to an extent that would be concerning.
18
+ // ESM WILL leak memory, big difference.
19
+ // Im talking with TC39 about a proposal around "virtual module trees" which would solve many problems.
20
+ // VMT is like Realms but better - easiest analogy would be like forking the main thread, without going off main thread
21
+ // VMT allows for scope isolation, but still allows reflection and non-primitive memory pointers to be shared - perfect for MFP
22
+
23
+ //TODO: should use extractUrlAndGlobal from internal.js
24
+ //TODO: should use Template system like LoadFileChunk runtime does.
25
+ //TODO: should use vm.runInThisContext instead of eval
26
+ //TODO: global.webpackChunkLoad could use a better convention? I have to use a special http client to get out of my infra firewall
27
+ const executeLoadTemplate = `
28
+ function executeLoad(remoteUrl) {
29
+ console.log('remoteUrl',remoteUrl)
30
+ const scriptUrl = remoteUrl.split("@")[1];
31
+ const moduleName = remoteUrl.split("@")[0];
32
+ console.log("executing remote load", scriptUrl);
33
+ return new Promise(function (resolve, reject) {
34
+
35
+ (global.webpackChunkLoad || global.fetch || require("node-fetch"))(scriptUrl).then(function(res){
36
+ return res.text();
37
+ }).then(function(scriptContent){
38
+ try {
39
+ const remote = eval(scriptContent + 'module.exports');
40
+ /* TODO: need something like a chunk loading queue, this can lead to async issues
41
+ if two containers load the same remote, they can overwrite global scope
42
+ should check someone is already loading remote and await that */
43
+ global.__remote_scope__[moduleName] = remote[moduleName] || remote
44
+ resolve(global.__remote_scope__[moduleName])
45
+ } catch(e) {
46
+ console.error('problem executing remote module', moduleName);
47
+ reject(e);
48
+ }
49
+ }).catch((e)=>{
50
+ console.error('failed to fetch remote', moduleName, scriptUrl);
51
+ console.error(e);
52
+ reject(null)
53
+ })
54
+ }).catch((e)=>{
55
+ console.error('error',e);
56
+ console.warn(moduleName,'is offline, returning fake remote')
57
+ return {
58
+ fake: true,
59
+ get:(arg)=>{
60
+ console.log('faking', arg,'module on', moduleName);
61
+
62
+ return ()=> Promise.resolve();
63
+ },
64
+ init:()=>{}
65
+ }
66
+ })
67
+ }
68
+ `;
69
+
70
+ function buildRemotes(mfConf, webpack) {
71
+ return Object.entries(mfConf.remotes || {}).reduce(
72
+ (acc, [name, config]) => {
73
+ // if its already been converted into promise, dont do it again
74
+ if(config.startsWith('promise ') || config.startsWith('external ')){
75
+ acc.buildTime[name] = config;
76
+ return acc;
77
+ }
78
+ /*
79
+ TODO: global remote scope object should go into webpack runtime as a runtime requirement
80
+ this can be done by referencing my LoadFile, CommonJs plugins in this directory.
81
+ */
82
+ const [global, url] = config.split('@');
83
+ const loadTemplate = `promise new Promise((resolve)=>{
84
+ if(!global.__remote_scope__) {
85
+ // create a global scope for container, similar to how remotes are set on window in the browser
86
+ global.__remote_scope__ = {
87
+ _config: {},
88
+ }
89
+ }
90
+
91
+ global.__remote_scope__._config[${JSON.stringify(global)}] = ${JSON.stringify(url)};
92
+
93
+ ${executeLoadTemplate}
94
+ resolve(executeLoad(${JSON.stringify(config)}))
95
+ }).then(remote=>{
96
+ console.log(remote);
97
+
98
+ return {
99
+ get: remote.get,
100
+ init: (args)=> {
101
+ console.log(args)
102
+ return remote.init(args)
103
+ }
104
+ }
105
+ });
106
+ `;
107
+ acc.buildTime[name] = loadTemplate;
108
+ return acc;
109
+ },
110
+ { runtime: {}, buildTime: {}, hot: {} }
111
+ );
112
+ // return Object.entries(mfConf.remotes || {}).reduce(
113
+ // (acc, [name, config]) => {
114
+ // const hasMiddleware = config.startsWith('middleware ');
115
+ // let middleware;
116
+ // if (hasMiddleware) {
117
+ // middleware = config.split('middleware ')[1];
118
+ // } else {
119
+ // middleware = `Promise.resolve(${JSON.stringify(config)})`;
120
+ // }
121
+ //
122
+ // const templateStart = `
123
+ // var ${webpack.RuntimeGlobals.require} = ${
124
+ // webpack.RuntimeGlobals.require
125
+ // } ? ${
126
+ // webpack.RuntimeGlobals.require
127
+ // } : typeof arguments !== 'undefined' ? arguments[2] : false;
128
+ // ${executeLoadTemplate}
129
+ // global.loadedRemotes = global.loadedRemotes || {};
130
+ // if (global.loadedRemotes[${JSON.stringify(name)}]) {
131
+ // return global.loadedRemotes[${JSON.stringify(name)}]
132
+ // }
133
+ // // if using modern output, then there are no arguments on the parent function scope, thus we need to get it via a window global.
134
+ //
135
+ // var shareScope = (${webpack.RuntimeGlobals.require} && ${
136
+ // webpack.RuntimeGlobals.shareScopeMap
137
+ // }) ? ${
138
+ // webpack.RuntimeGlobals.shareScopeMap
139
+ // } : global.__webpack_share_scopes__
140
+ // var name = ${JSON.stringify(name)}
141
+ // `;
142
+ // const template = `(remotesConfig) => new Promise((res) => {
143
+ // console.log('in template promise',JSON.stringify(remotesConfig))
144
+ // executeLoad(remotesConfig).then((remote) => {
145
+ //
146
+ // return Promise.resolve(remote.init(shareScope.default)).then(() => {
147
+ // return remote
148
+ // })
149
+ // })
150
+ // .then(function (remote) {
151
+ // const proxy = {
152
+ // get: remote.get,
153
+ // chunkMap: remote.chunkMap,
154
+ // path: remotesConfig.toString(),
155
+ // init: (arg) => {
156
+ // try {
157
+ // return remote.init(shareScope.default)
158
+ // } catch (e) {
159
+ // console.log('remote container already initialized')
160
+ // }
161
+ // }
162
+ // }
163
+ // if (remote.fake) {
164
+ // res(proxy);
165
+ // return null
166
+ // }
167
+ //
168
+ // Object.assign(global.loadedRemotes, {
169
+ // [name]: proxy
170
+ // });
171
+ //
172
+ // res(global.loadedRemotes[name])
173
+ // })
174
+ //
175
+ //
176
+ // })`;
177
+ //
178
+ // acc.runtime[name] = `()=> ${middleware}.then((remoteConfig)=>{
179
+ // console.log('remoteConfig runtime',remoteConfig);
180
+ // if(!global.REMOTE_CONFIG) {
181
+ // global.REMOTE_CONFIG = {};
182
+ // }
183
+ // global.REMOTE_CONFIG[${JSON.stringify(name)}] = remoteConfig;
184
+ // ${templateStart}
185
+ // const loadTemplate = ${template};
186
+ // return loadTemplate(remoteConfig)
187
+ // })`;
188
+ //
189
+ // acc.buildTime[name] = `promise ${middleware}.then((remoteConfig)=>{
190
+ // if(!global.REMOTE_CONFIG) {
191
+ // global.REMOTE_CONFIG = {};
192
+ // }
193
+ // console.log('remoteConfig buildtime',remoteConfig);
194
+ // global.REMOTE_CONFIG[${JSON.stringify(name)}] = remoteConfig;
195
+ // ${templateStart};
196
+ // const loadTemplate = ${template};
197
+ // return loadTemplate(remoteConfig)
198
+ // })`;
199
+ //
200
+ // acc.hot[name] = `()=> ${middleware}`;
201
+ //
202
+ // return acc;
203
+ // },
204
+ // { runtime: {}, buildTime: {}, hot: {} }
205
+ // );
206
+ }
207
+
208
+ class NodeFederationPlugin {
209
+ constructor({ experiments, ...options }, context) {
210
+ this.options = options || {};
211
+ this.context = context || {};
212
+ this.experiments = experiments || {};
213
+ }
214
+
215
+ apply(compiler) {
216
+ // When used with Next.js, context is needed to use Next.js webpack
217
+ const { webpack } = compiler;
218
+
219
+ const { buildTime, runtime, hot } = buildRemotes(
220
+ this.options,
221
+ webpack || require('webpack')
222
+ );
223
+ const defs = {
224
+ 'process.env.REMOTES': runtime,
225
+ 'process.env.REMOTE_CONFIG': hot,
226
+ };
227
+
228
+ // need to add this back to enabele runtime remotes, have commented out old promise new promise template
229
+ // new ((webpack && webpack.DefinePlugin) || require("webpack").DefinePlugin)(
230
+ // defs
231
+ // ).apply(compiler);
232
+
233
+ const pluginOptions = {
234
+ ...this.options,
235
+ remotes: buildTime,
236
+ };
237
+
238
+ new (this.context.ModuleFederationPlugin || (webpack && webpack.container.ModuleFederationPlugin) ||
239
+ require('webpack/lib/container/ModuleFederationPlugin'))(
240
+ pluginOptions
241
+ ).apply(compiler);
242
+ }
243
+ }
244
+
245
+ module.exports = NodeFederationPlugin;
@@ -0,0 +1,42 @@
1
+ const CommonJsChunkLoadingPlugin = require('./CommonJsChunkLoadingPlugin');
2
+
3
+ class StreamingTargetPlugin {
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, //TODO: should remove?
37
+ promiseBaseURI: this.options.promiseBaseURI, //TODO: should remove?
38
+ }).apply(compiler);
39
+ }
40
+ }
41
+
42
+ module.exports = StreamingTargetPlugin;
@@ -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
+ `;