AwaitDependenciesInitFragment.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. * Represents AwaitDependenciesInitFragment.
  14. * @extends {InitFragment<GenerateContext>}
  15. */
  16. class AwaitDependenciesInitFragment extends InitFragment {
  17. /**
  18. * Creates an instance of AwaitDependenciesInitFragment.
  19. * @param {Dependencies} dependencies maps an import var to an async module that needs to be awaited
  20. */
  21. constructor(dependencies) {
  22. super(
  23. undefined,
  24. InitFragment.STAGE_ASYNC_DEPENDENCIES,
  25. 0,
  26. "await-dependencies"
  27. );
  28. /** @type {Dependencies} */
  29. this.dependencies = dependencies;
  30. }
  31. /**
  32. * Merges another await-dependencies fragment into this fragment.
  33. * @param {AwaitDependenciesInitFragment} other other AwaitDependenciesInitFragment
  34. * @returns {AwaitDependenciesInitFragment} AwaitDependenciesInitFragment
  35. */
  36. merge(other) {
  37. const dependencies = new Map(other.dependencies);
  38. for (const [key, value] of this.dependencies) {
  39. dependencies.set(key, value);
  40. }
  41. return new AwaitDependenciesInitFragment(dependencies);
  42. }
  43. /**
  44. * Returns the source code that will be included as initialization code.
  45. * @param {GenerateContext} context context
  46. * @returns {string | Source | undefined} the source code that will be included as initialization code
  47. */
  48. getContent({ runtimeRequirements, runtimeTemplate }) {
  49. runtimeRequirements.add(RuntimeGlobals.module);
  50. if (this.dependencies.size === 0) {
  51. return "";
  52. }
  53. const importVars = [...this.dependencies.keys()];
  54. const asyncModuleValues = [...this.dependencies.values()].join(", ");
  55. const templateInput = [
  56. `var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([${asyncModuleValues}]);`
  57. ];
  58. if (
  59. this.dependencies.size === 1 ||
  60. !runtimeTemplate.supportsDestructuring()
  61. ) {
  62. templateInput.push(
  63. "var __webpack_async_dependencies_result__ = (__webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__);"
  64. );
  65. for (const [index, importVar] of importVars.entries()) {
  66. templateInput.push(
  67. `${importVar} = __webpack_async_dependencies_result__[${index}];`
  68. );
  69. }
  70. } else {
  71. const importVarsStr = importVars.join(", ");
  72. templateInput.push(
  73. `([${importVarsStr}] = __webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__);`
  74. );
  75. }
  76. templateInput.push("");
  77. return Template.asString(templateInput);
  78. }
  79. }
  80. module.exports = AwaitDependenciesInitFragment;