HarmonyImportSideEffectDependency.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const makeSerializable = require("../util/makeSerializable");
  7. const HarmonyImportDependency = require("./HarmonyImportDependency");
  8. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  9. /** @typedef {import("../Dependency")} Dependency */
  10. /** @typedef {import("../Dependency").GetConditionFn} GetConditionFn */
  11. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  12. /** @typedef {import("../Module")} Module */
  13. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  14. /** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
  15. /** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
  16. /** @typedef {import("../javascript/JavascriptParser").ImportAttributes} ImportAttributes */
  17. /** @typedef {import("../util/Hash")} Hash */
  18. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  19. class HarmonyImportSideEffectDependency extends HarmonyImportDependency {
  20. /**
  21. * @param {string} request the request string
  22. * @param {number} sourceOrder source order
  23. * @param {ImportAttributes=} attributes import attributes
  24. */
  25. constructor(request, sourceOrder, attributes) {
  26. super(request, sourceOrder, attributes);
  27. }
  28. get type() {
  29. return "harmony side effect evaluation";
  30. }
  31. /**
  32. * @param {ModuleGraph} moduleGraph module graph
  33. * @returns {null | false | GetConditionFn} function to determine if the connection is active
  34. */
  35. getCondition(moduleGraph) {
  36. return connection => {
  37. const refModule = connection.resolvedModule;
  38. if (!refModule) return true;
  39. return refModule.getSideEffectsConnectionState(moduleGraph);
  40. };
  41. }
  42. /**
  43. * @param {ModuleGraph} moduleGraph the module graph
  44. * @returns {ConnectionState} how this dependency connects the module to referencing modules
  45. */
  46. getModuleEvaluationSideEffectsState(moduleGraph) {
  47. const refModule = moduleGraph.getModule(this);
  48. if (!refModule) return true;
  49. return refModule.getSideEffectsConnectionState(moduleGraph);
  50. }
  51. }
  52. makeSerializable(
  53. HarmonyImportSideEffectDependency,
  54. "webpack/lib/dependencies/HarmonyImportSideEffectDependency"
  55. );
  56. HarmonyImportSideEffectDependency.Template = class HarmonyImportSideEffectDependencyTemplate extends (
  57. HarmonyImportDependency.Template
  58. ) {
  59. /**
  60. * @param {Dependency} dependency the dependency for which the template should be applied
  61. * @param {ReplaceSource} source the current replace source which can be modified
  62. * @param {DependencyTemplateContext} templateContext the context object
  63. * @returns {void}
  64. */
  65. apply(dependency, source, templateContext) {
  66. const { moduleGraph, concatenationScope } = templateContext;
  67. if (concatenationScope) {
  68. const module = /** @type {Module} */ (moduleGraph.getModule(dependency));
  69. if (concatenationScope.isModuleInScope(module)) {
  70. return;
  71. }
  72. }
  73. super.apply(dependency, source, templateContext);
  74. }
  75. };
  76. module.exports = HarmonyImportSideEffectDependency;