HarmonyAcceptDependency.js 4.1 KB

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