CssIcssSymbolDependency.js 3.6 KB

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