CssSelfLocalIdentifierDependency.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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("../util/runtime").RuntimeSpec} RuntimeSpec */
  15. class CssSelfLocalIdentifierDependency extends CssLocalIdentifierDependency {
  16. /**
  17. * @param {string} name name
  18. * @param {[number, number]} range range
  19. * @param {string=} prefix prefix
  20. * @param {Set<string>=} declaredSet set of declared names (will only be active when in declared set)
  21. */
  22. constructor(name, range, prefix = "", declaredSet = undefined) {
  23. super(name, range, prefix);
  24. this.declaredSet = declaredSet;
  25. }
  26. get type() {
  27. return "css self local identifier";
  28. }
  29. get category() {
  30. return "self";
  31. }
  32. /**
  33. * @returns {string | null} an identifier to merge equal requests
  34. */
  35. getResourceIdentifier() {
  36. return `self`;
  37. }
  38. /**
  39. * Returns the exported names
  40. * @param {ModuleGraph} moduleGraph module graph
  41. * @returns {ExportsSpec | undefined} export names
  42. */
  43. getExports(moduleGraph) {
  44. if (this.declaredSet && !this.declaredSet.has(this.name)) return;
  45. return super.getExports(moduleGraph);
  46. }
  47. /**
  48. * Returns list of exports referenced by this dependency
  49. * @param {ModuleGraph} moduleGraph module graph
  50. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  51. * @returns {(string[] | ReferencedExport)[]} referenced exports
  52. */
  53. getReferencedExports(moduleGraph, runtime) {
  54. if (this.declaredSet && !this.declaredSet.has(this.name))
  55. return Dependency.NO_EXPORTS_REFERENCED;
  56. return [[this.name]];
  57. }
  58. serialize(context) {
  59. const { write } = context;
  60. write(this.declaredSet);
  61. super.serialize(context);
  62. }
  63. deserialize(context) {
  64. const { read } = context;
  65. this.declaredSet = read();
  66. super.deserialize(context);
  67. }
  68. }
  69. CssSelfLocalIdentifierDependency.Template = class CssSelfLocalIdentifierDependencyTemplate extends (
  70. CssLocalIdentifierDependency.Template
  71. ) {
  72. /**
  73. * @param {Dependency} dependency the dependency for which the template should be applied
  74. * @param {ReplaceSource} source the current replace source which can be modified
  75. * @param {DependencyTemplateContext} templateContext the context object
  76. * @returns {void}
  77. */
  78. apply(dependency, source, templateContext) {
  79. const dep = /** @type {CssSelfLocalIdentifierDependency} */ (dependency);
  80. if (dep.declaredSet && !dep.declaredSet.has(dep.name)) return;
  81. super.apply(dependency, source, templateContext);
  82. }
  83. };
  84. makeSerializable(
  85. CssSelfLocalIdentifierDependency,
  86. "webpack/lib/dependencies/CssSelfLocalIdentifierDependency"
  87. );
  88. module.exports = CssSelfLocalIdentifierDependency;