CssIcssSymbolDependency.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. * Creates an instance of CssIcssSymbolDependency.
  25. * @param {string} localName local name
  26. * @param {Range} range range
  27. * @param {string=} value value when it was defined in this module
  28. * @param {string=} importName import name when it was imported from other module
  29. */
  30. constructor(localName, range, value, importName) {
  31. super();
  32. this.localName = localName;
  33. this.range = range;
  34. this.value = value;
  35. this.importName = importName;
  36. /** @type {undefined | string} */
  37. this._hashUpdate = undefined;
  38. }
  39. get type() {
  40. return "css symbol identifier";
  41. }
  42. /**
  43. * Updates the hash with the data contributed by this instance.
  44. * @param {Hash} hash hash to be updated
  45. * @param {UpdateHashContext} context context
  46. * @returns {void}
  47. */
  48. updateHash(hash, context) {
  49. if (this._hashUpdate === undefined) {
  50. this._hashUpdate = `${this.range}${this.localName}${this.value || this.importName}`;
  51. }
  52. hash.update(this._hashUpdate);
  53. }
  54. /**
  55. * Serializes this instance into the provided serializer context.
  56. * @param {ObjectSerializerContext} context context
  57. */
  58. serialize(context) {
  59. const { write } = context;
  60. write(this.localName);
  61. write(this.range);
  62. write(this.value);
  63. write(this.importName);
  64. super.serialize(context);
  65. }
  66. /**
  67. * Restores this instance from the provided deserializer context.
  68. * @param {ObjectDeserializerContext} context context
  69. */
  70. deserialize(context) {
  71. const { read } = context;
  72. this.localName = read();
  73. this.range = read();
  74. this.value = read();
  75. this.importName = read();
  76. super.deserialize(context);
  77. }
  78. }
  79. CssIcssSymbolDependency.Template = class CssIcssSymbolDependencyTemplate extends (
  80. NullDependency.Template
  81. ) {
  82. /**
  83. * Applies the plugin by registering its hooks on the compiler.
  84. * @param {Dependency} dependency the dependency for which the template should be applied
  85. * @param {ReplaceSource} source the current replace source which can be modified
  86. * @param {DependencyTemplateContext} templateContext the context object
  87. * @returns {void}
  88. */
  89. apply(dependency, source, templateContext) {
  90. if (templateContext.type === CSS_TYPE) {
  91. const dep = /** @type {CssIcssSymbolDependency} */ (dependency);
  92. /** @type {string | undefined} */
  93. const value = dep.importName
  94. ? CssIcssExportDependency.Template.resolve(
  95. dep.localName,
  96. dep.importName,
  97. templateContext
  98. )
  99. : dep.value;
  100. if (!value) {
  101. return;
  102. }
  103. source.replace(dep.range[0], dep.range[1] - 1, value);
  104. }
  105. }
  106. };
  107. makeSerializable(
  108. CssIcssSymbolDependency,
  109. "webpack/lib/dependencies/CssIcssSymbolDependency"
  110. );
  111. module.exports = CssIcssSymbolDependency;