RemoteRuntimeModule.js 4.1 KB

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