CssIcssSymbolDependency.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. /** @type {undefined | string} */
  36. this._hashUpdate = undefined;
  37. }
  38. get type() {
  39. return "css symbol identifier";
  40. }
  41. /**
  42. * Update the hash
  43. * @param {Hash} hash hash to be updated
  44. * @param {UpdateHashContext} context context
  45. * @returns {void}
  46. */
  47. updateHash(hash, context) {
  48. if (this._hashUpdate === undefined) {
  49. this._hashUpdate = `${this.range}${this.name}${this.value}`;
  50. }
  51. hash.update(this._hashUpdate);
  52. }
  53. /**
  54. * Returns list of exports referenced by this dependency
  55. * @param {ModuleGraph} moduleGraph module graph
  56. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  57. * @returns {ReferencedExports} referenced exports
  58. */
  59. getReferencedExports(moduleGraph, runtime) {
  60. return [[this.symbol]];
  61. }
  62. /**
  63. * @param {ObjectSerializerContext} context context
  64. */
  65. serialize(context) {
  66. const { write } = context;
  67. write(this.name);
  68. write(this.symbol);
  69. write(this.value);
  70. write(this.range);
  71. write(this.isReference);
  72. super.serialize(context);
  73. }
  74. /**
  75. * @param {ObjectDeserializerContext} context context
  76. */
  77. deserialize(context) {
  78. const { read } = context;
  79. this.name = read();
  80. this.symbol = read();
  81. this.value = read();
  82. this.range = read();
  83. this.isReference = read();
  84. super.deserialize(context);
  85. }
  86. }
  87. CssIcssSymbolDependency.Template = class CssIcssSymbolDependencyTemplate extends (
  88. NullDependency.Template
  89. ) {
  90. /**
  91. * @param {Dependency} dependency the dependency for which the template should be applied
  92. * @param {ReplaceSource} source the current replace source which can be modified
  93. * @param {DependencyTemplateContext} templateContext the context object
  94. * @returns {void}
  95. */
  96. apply(dependency, source, templateContext) {
  97. if (templateContext.type === CSS_TYPE) {
  98. const dep = /** @type {CssIcssSymbolDependency} */ (dependency);
  99. /** @type {string | undefined} */
  100. const value = dep.isReference
  101. ? CssIcssExportDependency.Template.findReference(
  102. dep.symbol,
  103. templateContext
  104. )
  105. : dep.symbol;
  106. if (!value) {
  107. return;
  108. }
  109. source.replace(dep.range[0], dep.range[1] - 1, value);
  110. }
  111. }
  112. };
  113. makeSerializable(
  114. CssIcssSymbolDependency,
  115. "webpack/lib/dependencies/CssIcssSymbolDependency"
  116. );
  117. module.exports = CssIcssSymbolDependency;