AwaitDependenciesInitFragment.js 2.5 KB

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