RemoteRuntimeModule.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const RuntimeGlobals = require("../RuntimeGlobals");
  7. const RuntimeModule = require("../RuntimeModule");
  8. const Template = require("../Template");
  9. /** @typedef {import("../Chunk")} Chunk */
  10. /** @typedef {import("../Chunk").ChunkId} ChunkId */
  11. /** @typedef {import("../ChunkGraph")} ChunkGraph */
  12. /** @typedef {import("../ChunkGraph").ModuleId} ModuleId */
  13. /** @typedef {import("../Compilation")} Compilation */
  14. /** @typedef {import("./RemoteModule")} RemoteModule */
  15. class RemoteRuntimeModule extends RuntimeModule {
  16. constructor() {
  17. super("remotes loading");
  18. }
  19. /**
  20. * Generates runtime code for this runtime module.
  21. * @returns {string | null} runtime code
  22. */
  23. generate() {
  24. const compilation = /** @type {Compilation} */ (this.compilation);
  25. const chunkGraph = /** @type {ChunkGraph} */ (this.chunkGraph);
  26. const { runtimeTemplate, moduleGraph } = compilation;
  27. /** @type {Record<ChunkId, ModuleId[]>} */
  28. const chunkToRemotesMapping = {};
  29. /** @type {Record<ModuleId, [string, string, ModuleId]>} */
  30. const idToExternalAndNameMapping = {};
  31. for (const chunk of /** @type {Chunk} */ (
  32. this.chunk
  33. ).getAllReferencedChunks()) {
  34. const modules = chunkGraph.getChunkModulesIterableBySourceType(
  35. chunk,
  36. "remote"
  37. );
  38. if (!modules) continue;
  39. /** @type {ModuleId[]} */
  40. const remotes = (chunkToRemotesMapping[
  41. /** @type {ChunkId} */
  42. (chunk.id)
  43. ] = []);
  44. for (const m of modules) {
  45. const module = /** @type {RemoteModule} */ (m);
  46. const name = module.internalRequest;
  47. const id = /** @type {ModuleId} */ (chunkGraph.getModuleId(module));
  48. const shareScope = module.shareScope;
  49. const dep = module.dependencies[0];
  50. const externalModule = moduleGraph.getModule(dep);
  51. const externalModuleId =
  52. /** @type {ModuleId} */
  53. (externalModule && chunkGraph.getModuleId(externalModule));
  54. remotes.push(id);
  55. idToExternalAndNameMapping[id] = [shareScope, name, externalModuleId];
  56. }
  57. }
  58. return Template.asString([
  59. `var chunkMapping = ${JSON.stringify(
  60. chunkToRemotesMapping,
  61. null,
  62. "\t"
  63. )};`,
  64. `var idToExternalAndNameMapping = ${JSON.stringify(
  65. idToExternalAndNameMapping,
  66. null,
  67. "\t"
  68. )};`,
  69. `${
  70. RuntimeGlobals.ensureChunkHandlers
  71. }.remotes = ${runtimeTemplate.basicFunction("chunkId, promises", [
  72. `if(${RuntimeGlobals.hasOwnProperty}(chunkMapping, chunkId)) {`,
  73. Template.indent([
  74. `chunkMapping[chunkId].forEach(${runtimeTemplate.basicFunction("id", [
  75. `var getScope = ${RuntimeGlobals.currentRemoteGetScope};`,
  76. "if(!getScope) getScope = [];",
  77. "var data = idToExternalAndNameMapping[id];",
  78. "if(getScope.indexOf(data) >= 0) return;",
  79. "getScope.push(data);",
  80. "if(data.p) return promises.push(data.p);",
  81. `var onError = ${runtimeTemplate.basicFunction("error", [
  82. 'if(!error) error = new Error("Container missing");',
  83. 'if(typeof error.message === "string")',
  84. Template.indent(
  85. "error.message += '\\nwhile loading \"' + data[1] + '\" from ' + data[2];"
  86. ),
  87. `${
  88. RuntimeGlobals.moduleFactories
  89. }[id] = ${runtimeTemplate.basicFunction("", ["throw error;"])}`,
  90. "data.p = 0;"
  91. ])};`,
  92. `var handleFunction = ${runtimeTemplate.basicFunction(
  93. "fn, arg1, arg2, d, next, first",
  94. [
  95. "try {",
  96. Template.indent([
  97. "var promise = fn(arg1, arg2);",
  98. "if(promise && promise.then) {",
  99. Template.indent([
  100. `var p = promise.then(${runtimeTemplate.returningFunction(
  101. "next(result, d)",
  102. "result"
  103. )}, onError);`,
  104. "if(first) promises.push(data.p = p); else return p;"
  105. ]),
  106. "} else {",
  107. Template.indent(["return next(promise, d, first);"]),
  108. "}"
  109. ]),
  110. "} catch(error) {",
  111. Template.indent(["onError(error);"]),
  112. "}"
  113. ]
  114. )}`,
  115. `var onExternal = ${runtimeTemplate.returningFunction(
  116. `external ? handleFunction(${RuntimeGlobals.initializeSharing}, data[0], 0, external, onInitialized, first) : onError()`,
  117. "external, _, first"
  118. )};`,
  119. `var onInitialized = ${runtimeTemplate.returningFunction(
  120. "handleFunction(external.get, data[1], getScope, 0, onFactory, first)",
  121. "_, external, first"
  122. )};`,
  123. `var onFactory = ${runtimeTemplate.basicFunction("factory", [
  124. "data.p = 1;",
  125. `${
  126. RuntimeGlobals.moduleFactories
  127. }[id] = ${runtimeTemplate.basicFunction("module", [
  128. "module.exports = factory();"
  129. ])}`
  130. ])};`,
  131. `handleFunction(${RuntimeGlobals.require}, data[2], 0, 0, onExternal, 1);`
  132. ])});`
  133. ]),
  134. "}"
  135. ])}`
  136. ]);
  137. }
  138. }
  139. module.exports = RemoteRuntimeModule;