HarmonyImportSideEffectDependency.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  11. /** @typedef {import("../InitFragment")} InitFragment */
  12. /** @typedef {import("../Module")} Module */
  13. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  14. /** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
  15. /** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
  16. /** @typedef {import("../util/Hash")} Hash */
  17. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  18. class HarmonyImportSideEffectDependency extends HarmonyImportDependency {
  19. constructor(request, sourceOrder, assertions) {
  20. super(request, sourceOrder, assertions);
  21. }
  22. get type() {
  23. return "harmony side effect evaluation";
  24. }
  25. /**
  26. * @param {ModuleGraph} moduleGraph module graph
  27. * @returns {null | false | function(ModuleGraphConnection, RuntimeSpec): ConnectionState} function to determine if the connection is active
  28. */
  29. getCondition(moduleGraph) {
  30. return connection => {
  31. const refModule = connection.resolvedModule;
  32. if (!refModule) return true;
  33. return refModule.getSideEffectsConnectionState(moduleGraph);
  34. };
  35. }
  36. /**
  37. * @param {ModuleGraph} moduleGraph the module graph
  38. * @returns {ConnectionState} how this dependency connects the module to referencing modules
  39. */
  40. getModuleEvaluationSideEffectsState(moduleGraph) {
  41. const refModule = moduleGraph.getModule(this);
  42. if (!refModule) return true;
  43. return refModule.getSideEffectsConnectionState(moduleGraph);
  44. }
  45. }
  46. makeSerializable(
  47. HarmonyImportSideEffectDependency,
  48. "webpack/lib/dependencies/HarmonyImportSideEffectDependency"
  49. );
  50. HarmonyImportSideEffectDependency.Template = class HarmonyImportSideEffectDependencyTemplate extends (
  51. HarmonyImportDependency.Template
  52. ) {
  53. /**
  54. * @param {Dependency} dependency the dependency for which the template should be applied
  55. * @param {ReplaceSource} source the current replace source which can be modified
  56. * @param {DependencyTemplateContext} templateContext the context object
  57. * @returns {void}
  58. */
  59. apply(dependency, source, templateContext) {
  60. const { moduleGraph, concatenationScope } = templateContext;
  61. if (concatenationScope) {
  62. const module = moduleGraph.getModule(dependency);
  63. if (concatenationScope.isModuleInScope(module)) {
  64. return;
  65. }
  66. }
  67. super.apply(dependency, source, templateContext);
  68. }
  69. };
  70. module.exports = HarmonyImportSideEffectDependency;