CssIcssSymbolDependency.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Alexander Akait @alexander-akait
  4. */
  5. "use strict";
  6. const { CSS_TYPE } = require("../ModuleSourceTypeConstants");
  7. const makeSerializable = require("../util/makeSerializable");
  8. const CssIcssExportDependency = require("./CssIcssExportDependency");
  9. const NullDependency = require("./NullDependency");
  10. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  11. /** @typedef {import("../Dependency")} Dependency */
  12. /** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
  13. /** @typedef {import("../Dependency").ReferencedExports} ReferencedExports */
  14. /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
  15. /** @typedef {import("../DependencyTemplate").CssDependencyTemplateContext} DependencyTemplateContext */
  16. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  17. /** @typedef {import("../css/CssParser").Range} Range */
  18. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  19. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  20. /** @typedef {import("../util/Hash")} Hash */
  21. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  22. class CssIcssSymbolDependency extends NullDependency {
  23. /**
  24. * @param {string} name name
  25. * @param {string} symbol symbol
  26. * @param {Range} range range
  27. * @param {boolean=} isReference true when is reference, otherwise false
  28. */
  29. constructor(name, symbol, range, isReference) {
  30. super();
  31. this.name = name;
  32. this.symbol = symbol;
  33. this.range = range;
  34. this.isReference = isReference;
  35. this._hashUpdate = undefined;
  36. }
  37. get type() {
  38. return "css symbol identifier";
  39. }
  40. /**
  41. * Update the hash
  42. * @param {Hash} hash hash to be updated
  43. * @param {UpdateHashContext} context context
  44. * @returns {void}
  45. */
  46. updateHash(hash, context) {
  47. if (this._hashUpdate === undefined) {
  48. this._hashUpdate = `${this.range}${this.name}${this.value}`;
  49. }
  50. hash.update(this._hashUpdate);
  51. }
  52. /**
  53. * Returns list of exports referenced by this dependency
  54. * @param {ModuleGraph} moduleGraph module graph
  55. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  56. * @returns {ReferencedExports} referenced exports
  57. */
  58. getReferencedExports(moduleGraph, runtime) {
  59. return [[this.symbol]];
  60. }
  61. /**
  62. * @param {ObjectSerializerContext} context context
  63. */
  64. serialize(context) {
  65. const { write } = context;
  66. write(this.name);
  67. write(this.symbol);
  68. write(this.value);
  69. write(this.range);
  70. write(this.isReference);
  71. super.serialize(context);
  72. }
  73. /**
  74. * @param {ObjectDeserializerContext} context context
  75. */
  76. deserialize(context) {
  77. const { read } = context;
  78. this.name = read();
  79. this.symbol = read();
  80. this.value = read();
  81. this.range = read();
  82. this.isReference = read();
  83. super.deserialize(context);
  84. }
  85. }
  86. CssIcssSymbolDependency.Template = class CssIcssSymbolDependencyTemplate extends (
  87. NullDependency.Template
  88. ) {
  89. /**
  90. * @param {Dependency} dependency the dependency for which the template should be applied
  91. * @param {ReplaceSource} source the current replace source which can be modified
  92. * @param {DependencyTemplateContext} templateContext the context object
  93. * @returns {void}
  94. */
  95. apply(dependency, source, templateContext) {
  96. if (templateContext.type === CSS_TYPE) {
  97. const dep = /** @type {CssIcssSymbolDependency} */ (dependency);
  98. /** @type {string | undefined} */
  99. const value = dep.isReference
  100. ? CssIcssExportDependency.Template.findReference(
  101. dep.symbol,
  102. templateContext
  103. )
  104. : dep.symbol;
  105. if (!value) {
  106. return;
  107. }
  108. source.replace(dep.range[0], dep.range[1] - 1, value);
  109. }
  110. }
  111. };
  112. makeSerializable(
  113. CssIcssSymbolDependency,
  114. "webpack/lib/dependencies/CssIcssSymbolDependency"
  115. );
  116. module.exports = CssIcssSymbolDependency;