HarmonyAcceptDependency.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Template = require("../Template");
  7. const makeSerializable = require("../util/makeSerializable");
  8. const HarmonyImportDependency = require("./HarmonyImportDependency");
  9. const NullDependency = require("./NullDependency");
  10. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  11. /** @typedef {import("../Dependency")} Dependency */
  12. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  13. /** @typedef {import("./HarmonyAcceptImportDependency")} HarmonyAcceptImportDependency */
  14. class HarmonyAcceptDependency extends NullDependency {
  15. /**
  16. * @param {[number, number]} range expression range
  17. * @param {HarmonyAcceptImportDependency[]} dependencies import dependencies
  18. * @param {boolean} hasCallback true, if the range wraps an existing callback
  19. */
  20. constructor(range, dependencies, hasCallback) {
  21. super();
  22. this.range = range;
  23. this.dependencies = dependencies;
  24. this.hasCallback = hasCallback;
  25. }
  26. get type() {
  27. return "accepted harmony modules";
  28. }
  29. serialize(context) {
  30. const { write } = context;
  31. write(this.range);
  32. write(this.dependencies);
  33. write(this.hasCallback);
  34. super.serialize(context);
  35. }
  36. deserialize(context) {
  37. const { read } = context;
  38. this.range = read();
  39. this.dependencies = read();
  40. this.hasCallback = read();
  41. super.deserialize(context);
  42. }
  43. }
  44. makeSerializable(
  45. HarmonyAcceptDependency,
  46. "webpack/lib/dependencies/HarmonyAcceptDependency"
  47. );
  48. HarmonyAcceptDependency.Template = class HarmonyAcceptDependencyTemplate extends (
  49. NullDependency.Template
  50. ) {
  51. /**
  52. * @param {Dependency} dependency the dependency for which the template should be applied
  53. * @param {ReplaceSource} source the current replace source which can be modified
  54. * @param {DependencyTemplateContext} templateContext the context object
  55. * @returns {void}
  56. */
  57. apply(dependency, source, templateContext) {
  58. const dep = /** @type {HarmonyAcceptDependency} */ (dependency);
  59. const {
  60. module,
  61. runtime,
  62. runtimeRequirements,
  63. runtimeTemplate,
  64. moduleGraph,
  65. chunkGraph
  66. } = templateContext;
  67. const content = dep.dependencies
  68. .map(dependency => {
  69. const referencedModule = moduleGraph.getModule(dependency);
  70. return {
  71. dependency,
  72. runtimeCondition: referencedModule
  73. ? HarmonyImportDependency.Template.getImportEmittedRuntime(
  74. module,
  75. referencedModule
  76. )
  77. : false
  78. };
  79. })
  80. .filter(({ runtimeCondition }) => runtimeCondition !== false)
  81. .map(({ dependency, runtimeCondition }) => {
  82. const condition = runtimeTemplate.runtimeConditionExpression({
  83. chunkGraph,
  84. runtime,
  85. runtimeCondition,
  86. runtimeRequirements
  87. });
  88. const s = dependency.getImportStatement(true, templateContext);
  89. const code = s[0] + s[1];
  90. if (condition !== "true") {
  91. return `if (${condition}) {\n${Template.indent(code)}\n}\n`;
  92. }
  93. return code;
  94. })
  95. .join("");
  96. if (dep.hasCallback) {
  97. if (runtimeTemplate.supportsArrowFunction()) {
  98. source.insert(
  99. dep.range[0],
  100. `__WEBPACK_OUTDATED_DEPENDENCIES__ => { ${content}(`
  101. );
  102. source.insert(dep.range[1], ")(__WEBPACK_OUTDATED_DEPENDENCIES__); }");
  103. } else {
  104. source.insert(
  105. dep.range[0],
  106. `function(__WEBPACK_OUTDATED_DEPENDENCIES__) { ${content}(`
  107. );
  108. source.insert(
  109. dep.range[1],
  110. ")(__WEBPACK_OUTDATED_DEPENDENCIES__); }.bind(this)"
  111. );
  112. }
  113. return;
  114. }
  115. const arrow = runtimeTemplate.supportsArrowFunction();
  116. source.insert(
  117. dep.range[1] - 0.5,
  118. `, ${arrow ? "() =>" : "function()"} { ${content} }`
  119. );
  120. }
  121. };
  122. module.exports = HarmonyAcceptDependency;