RemoteRuntimeModule.js 4.7 KB

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