AwaitDependenciesInitFragment.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const InitFragment = require("../InitFragment");
  7. const RuntimeGlobals = require("../RuntimeGlobals");
  8. const Template = require("../Template");
  9. /** @typedef {import("webpack-sources").Source} Source */
  10. /** @typedef {import("../Generator").GenerateContext} GenerateContext */
  11. /** @typedef {Map<string, string>} Dependencies */
  12. /**
  13. * @extends {InitFragment<GenerateContext>}
  14. */
  15. class AwaitDependenciesInitFragment extends InitFragment {
  16. /**
  17. * @param {Dependencies} dependencies maps an import var to an async module that needs to be awaited
  18. */
  19. constructor(dependencies) {
  20. super(
  21. undefined,
  22. InitFragment.STAGE_ASYNC_DEPENDENCIES,
  23. 0,
  24. "await-dependencies"
  25. );
  26. /** @type {Dependencies} */
  27. this.dependencies = dependencies;
  28. }
  29. /**
  30. * @param {AwaitDependenciesInitFragment} other other AwaitDependenciesInitFragment
  31. * @returns {AwaitDependenciesInitFragment} AwaitDependenciesInitFragment
  32. */
  33. merge(other) {
  34. const dependencies = new Map(other.dependencies);
  35. for (const [key, value] of this.dependencies) {
  36. dependencies.set(key, value);
  37. }
  38. return new AwaitDependenciesInitFragment(dependencies);
  39. }
  40. /**
  41. * @param {GenerateContext} context context
  42. * @returns {string | Source | undefined} the source code that will be included as initialization code
  43. */
  44. getContent({ runtimeRequirements, runtimeTemplate }) {
  45. runtimeRequirements.add(RuntimeGlobals.module);
  46. if (this.dependencies.size === 0) {
  47. return "";
  48. }
  49. const importVars = [...this.dependencies.keys()];
  50. const asyncModuleValues = [...this.dependencies.values()].join(", ");
  51. const templateInput = [
  52. `var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([${asyncModuleValues}]);`
  53. ];
  54. if (
  55. this.dependencies.size === 1 ||
  56. !runtimeTemplate.supportsDestructuring()
  57. ) {
  58. templateInput.push(
  59. "var __webpack_async_dependencies_result__ = (__webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__);"
  60. );
  61. for (const [index, importVar] of importVars.entries()) {
  62. templateInput.push(
  63. `${importVar} = __webpack_async_dependencies_result__[${index}];`
  64. );
  65. }
  66. } else {
  67. const importVarsStr = importVars.join(", ");
  68. templateInput.push(
  69. `([${importVarsStr}] = __webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__);`
  70. );
  71. }
  72. templateInput.push("");
  73. return Template.asString(templateInput);
  74. }
  75. }
  76. module.exports = AwaitDependenciesInitFragment;