RemoteRuntimeModule.js 4.6 KB

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