CssSelfLocalIdentifierDependency.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Ivan Kopeykin @vankop
  4. */
  5. "use strict";
  6. const Dependency = require("../Dependency");
  7. const makeSerializable = require("../util/makeSerializable");
  8. const CssLocalIdentifierDependency = require("./CssLocalIdentifierDependency");
  9. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  10. /** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
  11. /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
  12. /** @typedef {import("../DependencyTemplate").CssDependencyTemplateContext} DependencyTemplateContext */
  13. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  14. /** @typedef {import("../css/CssParser").Range} Range */
  15. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  16. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  17. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  18. class CssSelfLocalIdentifierDependency extends CssLocalIdentifierDependency {
  19. /**
  20. * @param {string} name name
  21. * @param {Range} range range
  22. * @param {string=} prefix prefix
  23. * @param {Set<string>=} declaredSet set of declared names (will only be active when in declared set)
  24. */
  25. constructor(name, range, prefix = "", declaredSet = undefined) {
  26. super(name, range, prefix);
  27. this.declaredSet = declaredSet;
  28. }
  29. get type() {
  30. return "css self local identifier";
  31. }
  32. get category() {
  33. return "self";
  34. }
  35. /**
  36. * @returns {string | null} an identifier to merge equal requests
  37. */
  38. getResourceIdentifier() {
  39. return `self`;
  40. }
  41. /**
  42. * Returns the exported names
  43. * @param {ModuleGraph} moduleGraph module graph
  44. * @returns {ExportsSpec | undefined} export names
  45. */
  46. getExports(moduleGraph) {
  47. if (this.declaredSet && !this.declaredSet.has(this.name)) return;
  48. return super.getExports(moduleGraph);
  49. }
  50. /**
  51. * Returns list of exports referenced by this dependency
  52. * @param {ModuleGraph} moduleGraph module graph
  53. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  54. * @returns {(string[] | ReferencedExport)[]} referenced exports
  55. */
  56. getReferencedExports(moduleGraph, runtime) {
  57. if (this.declaredSet && !this.declaredSet.has(this.name))
  58. return Dependency.NO_EXPORTS_REFERENCED;
  59. return [[this.name]];
  60. }
  61. /**
  62. * @param {ObjectSerializerContext} context context
  63. */
  64. serialize(context) {
  65. const { write } = context;
  66. write(this.declaredSet);
  67. super.serialize(context);
  68. }
  69. /**
  70. * @param {ObjectDeserializerContext} context context
  71. */
  72. deserialize(context) {
  73. const { read } = context;
  74. this.declaredSet = read();
  75. super.deserialize(context);
  76. }
  77. }
  78. CssSelfLocalIdentifierDependency.Template = class CssSelfLocalIdentifierDependencyTemplate extends (
  79. CssLocalIdentifierDependency.Template
  80. ) {
  81. /**
  82. * @param {Dependency} dependency the dependency for which the template should be applied
  83. * @param {ReplaceSource} source the current replace source which can be modified
  84. * @param {DependencyTemplateContext} templateContext the context object
  85. * @returns {void}
  86. */
  87. apply(dependency, source, templateContext) {
  88. const dep = /** @type {CssSelfLocalIdentifierDependency} */ (dependency);
  89. if (dep.declaredSet && !dep.declaredSet.has(dep.name)) return;
  90. super.apply(dependency, source, templateContext);
  91. }
  92. };
  93. makeSerializable(
  94. CssSelfLocalIdentifierDependency,
  95. "webpack/lib/dependencies/CssSelfLocalIdentifierDependency"
  96. );
  97. module.exports = CssSelfLocalIdentifierDependency;