@module-federation/nextjs-mf 5.2.2 → 5.3.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.
Files changed (49) hide show
  1. package/lib/ModuleFederationPlugin.js +80 -0
  2. package/lib/NextFederationPlugin.js +524 -1
  3. package/lib/_virtual/_tslib.js +101 -0
  4. package/lib/client/CombinedPages.d.ts +28 -0
  5. package/lib/client/CombinedPages.d.ts.map +1 -0
  6. package/lib/client/CombinedPages.js +60 -0
  7. package/lib/client/MFClient.d.ts +70 -0
  8. package/lib/client/MFClient.d.ts.map +1 -0
  9. package/lib/client/MFClient.js +197 -0
  10. package/lib/client/RemoteContainer.d.ts +58 -0
  11. package/lib/client/RemoteContainer.d.ts.map +1 -0
  12. package/lib/client/RemoteContainer.js +161 -0
  13. package/lib/client/RemotePages.d.ts +48 -0
  14. package/lib/client/RemotePages.d.ts.map +1 -0
  15. package/lib/client/RemotePages.js +168 -0
  16. package/lib/client/UrlNode.d.ts +18 -0
  17. package/lib/client/UrlNode.d.ts.map +1 -0
  18. package/lib/client/UrlNode.js +162 -0
  19. package/lib/client/helpers.d.ts +17 -0
  20. package/lib/client/helpers.d.ts.map +1 -0
  21. package/lib/client/helpers.js +108 -0
  22. package/lib/client/useMFClient.d.ts +25 -0
  23. package/lib/client/useMFClient.d.ts.map +1 -0
  24. package/lib/client/useMFClient.js +79 -0
  25. package/lib/client/useMFRemote.d.ts +17 -0
  26. package/lib/client/useMFRemote.d.ts.map +1 -0
  27. package/lib/client/useMFRemote.js +72 -0
  28. package/lib/include-defaults.js +1 -3
  29. package/lib/internal.js +241 -0
  30. package/lib/loaders/UrlNode.js +209 -0
  31. package/lib/loaders/nextPageMapLoader.js +63 -13
  32. package/lib/loaders/patchNextClientPageLoader.js +53 -0
  33. package/lib/node-plugin/streaming/CommonJsChunkLoadingPlugin.js +86 -0
  34. package/lib/node-plugin/streaming/LoadFileChunkLoadingRuntimeModule.js +410 -0
  35. package/lib/node-plugin/streaming/NodeRuntime.js +147 -0
  36. package/lib/node-plugin/streaming/index.js +44 -0
  37. package/lib/node-plugin/streaming/loadScript.js +55 -0
  38. package/lib/plugins/DevHmrFixInvalidPongPlugin.js +60 -0
  39. package/lib/utils.js +14 -3
  40. package/node-plugin/README.md +27 -0
  41. package/node-plugin/package.json +4 -0
  42. package/node-plugin/streaming/CommonJsChunkLoadingPlugin.js +89 -0
  43. package/node-plugin/streaming/LoadFileChunkLoadingRuntimeModule.js +410 -0
  44. package/node-plugin/streaming/NodeRuntime.js +245 -0
  45. package/node-plugin/streaming/index.js +42 -0
  46. package/node-plugin/streaming/loadScript.js +51 -0
  47. package/package.json +22 -5
  48. package/tsconfig.json +33 -0
  49. package/lib/NextFederationPlugin2.js +0 -0
@@ -0,0 +1,60 @@
1
+ /**
2
+ * If HMR through websocket received {"invalid":true, "event":"pong"} event
3
+ * then pages reloads. But for federated page this is unwanted behavior.
4
+ *
5
+ * So this plugin in DEV mode disables page.reload() in HMR for federated pages.
6
+ */
7
+ class DevHmrFixInvalidPongPlugin {
8
+ /**
9
+ * Apply the plugin
10
+ * @param {import("webpack").Compiler} compiler the compiler instance
11
+ * @returns {void}
12
+ */
13
+ apply(compiler) {
14
+ const webpack = compiler.webpack;
15
+
16
+ compiler.hooks.thisCompilation.tap(
17
+ 'DevHmrFixInvalidPongPlugin',
18
+ (compilation) => {
19
+ compilation.hooks.processAssets.tap(
20
+ {
21
+ name: 'DevHmrFixInvalidPongPlugin',
22
+ state: compilation.constructor.PROCESS_ASSETS_STAGE_PRE_PROCESS,
23
+ },
24
+ (assets) => {
25
+ Object.keys(assets).forEach((filename) => {
26
+ if (filename.endsWith('/main.js')) {
27
+ const asset = compilation.getAsset(filename);
28
+ const newSource = asset.source.source().replace(
29
+ new RegExp(
30
+ escapeRegExp(
31
+ 'if (payload.event === \\"pong\\" && payload.invalid && !self.__NEXT_DATA__.err) {'
32
+ ),
33
+ 'g'
34
+ ),
35
+ `if (payload.event === \\"pong\\" && payload.invalid && !self.__NEXT_DATA__.err) {
36
+ if (window.mf_client && window.mf_client.isFederatedPathname(window.location.pathname)) return;
37
+ `.replaceAll('\n', '\\n')
38
+ );
39
+
40
+ const updatedAsset = new webpack.sources.RawSource(newSource);
41
+
42
+ if (asset) {
43
+ compilation.updateAsset(filename, updatedAsset);
44
+ } else {
45
+ compilation.emitAsset(filename, updatedAsset);
46
+ }
47
+ }
48
+ });
49
+ }
50
+ );
51
+ }
52
+ );
53
+ }
54
+ }
55
+
56
+ function escapeRegExp(string) {
57
+ return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
58
+ }
59
+
60
+ module.exports = DevHmrFixInvalidPongPlugin;
package/lib/utils.js CHANGED
@@ -1,20 +1,29 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
1
5
  const remoteVars = process.env.REMOTES || {};
2
6
 
3
7
  const runtimeRemotes = Object.entries(remoteVars).reduce(function (acc, item) {
4
8
  const [key, value] = item;
5
9
  if (typeof value === 'object' && typeof value.then === 'function') {
10
+ // if its an object with a thennable (eagerly executing function)
6
11
  acc[key] = { asyncContainer: value };
12
+ } else if (typeof value === 'function') {
13
+ // if its a function that must be called (lazily executing function)
14
+ acc[key] = { asyncContainer: value() };
7
15
  } else if (typeof value === 'string') {
16
+ // if its just a string (global@url)
8
17
  const [global, url] = value.split('@');
9
18
  acc[key] = { global, url };
10
19
  } else {
20
+ // we dont know or currently support this type
21
+ console.log('remotes process', process.env.REMOTES);
11
22
  throw new Error(`[mf] Invalid value received for runtime_remote "${key}"`);
12
23
  }
13
24
  return acc;
14
25
  }, {});
15
26
 
16
- module.exports.remotes = runtimeRemotes;
17
-
18
27
  /**
19
28
  * Return initialized remote container by remote's key or its runtime remote item data.
20
29
  *
@@ -94,4 +103,6 @@ function injectScript(keyOrRuntimeRemoteItem) {
94
103
  });
95
104
  }
96
105
 
97
- module.exports.injectScript = injectScript;
106
+ var injectScript_1 = injectScript;
107
+
108
+ exports.injectScript = injectScript_1;
@@ -0,0 +1,27 @@
1
+ # node
2
+
3
+ Software streaming to enable node.js support for browser-like chunk loading
4
+
5
+ ```js
6
+ const StreamingRuntime = require('../node/streaming');
7
+ const NodeFederation = require('../node/streaming/NodeRuntime');
8
+
9
+ plugins: [
10
+ new StreamingRuntime({
11
+ name: 'website2',
12
+ library: { type: 'commonjs' },
13
+ filename: 'remoteEntry.js',
14
+ exposes: {
15
+ './SharedComponent': './remoteServer/SharedComponent',
16
+ },
17
+ }),
18
+ new NodeFederation({
19
+ name: 'website2',
20
+ library: { type: 'commonjs' },
21
+ filename: 'remoteEntry.js',
22
+ exposes: {
23
+ './SharedComponent': './remoteServer/SharedComponent',
24
+ },
25
+ }),
26
+ ];
27
+ ```
@@ -0,0 +1,4 @@
1
+ {
2
+ "name": "@module-federation/node",
3
+ "version": "1.0.0"
4
+ }
@@ -0,0 +1,89 @@
1
+ const RuntimeGlobals = require('webpack/lib/RuntimeGlobals');
2
+ const StartupChunkDependenciesPlugin = require('webpack/lib/runtime/StartupChunkDependenciesPlugin');
3
+ import ChunkLoadingRuntimeModule from './LoadFileChunkLoadingRuntimeModule';
4
+ // const ChunkLoadingRuntimeModule = require('webpack/lib/node/ReadFileChunkLoadingRuntimeModule')
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
+ export default 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
+ import loadScriptTemplate from './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
+ export default ReadFileChunkLoadingRuntimeModule;