CssModule.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Alexander Krasnoyarov @alexander-akait
  4. */
  5. "use strict";
  6. const NormalModule = require("./NormalModule");
  7. const makeSerializable = require("./util/makeSerializable");
  8. /** @typedef {import("./Module")} Module */
  9. /** @typedef {import("./NormalModule").NormalModuleCreateData} NormalModuleCreateData */
  10. /** @typedef {import("./RequestShortener")} RequestShortener */
  11. /** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  12. /** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  13. /** @typedef {string|undefined} CssLayer */
  14. /** @typedef {string|undefined} Supports */
  15. /** @typedef {string|undefined} Media */
  16. /** @typedef {[CssLayer?, Supports?, Media?]} InheritanceItem */
  17. /** @typedef {Array<InheritanceItem>} Inheritance */
  18. /** @typedef {NormalModuleCreateData & { cssLayer: CssLayer|null, supports: Supports|null, media: Media|null, inheritance: Inheritance|null }} CSSModuleCreateData */
  19. class CssModule extends NormalModule {
  20. /**
  21. * @param {CSSModuleCreateData} options options object
  22. */
  23. constructor(options) {
  24. super(options);
  25. // Avoid override `layer` for `Module` class, because it is a feature to run module in specific layer
  26. this.cssLayer = options.cssLayer;
  27. this.supports = options.supports;
  28. this.media = options.media;
  29. this.inheritance = options.inheritance;
  30. }
  31. /**
  32. * @returns {string} a unique identifier of the module
  33. */
  34. identifier() {
  35. let identifier = super.identifier();
  36. if (this.cssLayer) {
  37. identifier += `|${this.cssLayer}`;
  38. }
  39. if (this.supports) {
  40. identifier += `|${this.supports}`;
  41. }
  42. if (this.media) {
  43. identifier += `|${this.media}`;
  44. }
  45. if (this.inheritance) {
  46. const inheritance = this.inheritance.map(
  47. (item, index) =>
  48. `inheritance_${index}|${item[0] || ""}|${item[1] || ""}|${
  49. item[2] || ""
  50. }`
  51. );
  52. identifier += `|${inheritance.join("|")}`;
  53. }
  54. return identifier;
  55. }
  56. /**
  57. * @param {RequestShortener} requestShortener the request shortener
  58. * @returns {string} a user readable identifier of the module
  59. */
  60. readableIdentifier(requestShortener) {
  61. const readableIdentifier = super.readableIdentifier(requestShortener);
  62. let identifier = `css ${readableIdentifier}`;
  63. if (this.cssLayer) {
  64. identifier += ` (layer: ${this.cssLayer})`;
  65. }
  66. if (this.supports) {
  67. identifier += ` (supports: ${this.supports})`;
  68. }
  69. if (this.media) {
  70. identifier += ` (media: ${this.media})`;
  71. }
  72. return identifier;
  73. }
  74. /**
  75. * Assuming this module is in the cache. Update the (cached) module with
  76. * the fresh module from the factory. Usually updates internal references
  77. * and properties.
  78. * @param {Module} module fresh module
  79. * @returns {void}
  80. */
  81. updateCacheModule(module) {
  82. super.updateCacheModule(module);
  83. const m = /** @type {CssModule} */ (module);
  84. this.cssLayer = m.cssLayer;
  85. this.supports = m.supports;
  86. this.media = m.media;
  87. this.inheritance = m.inheritance;
  88. }
  89. /**
  90. * @param {ObjectSerializerContext} context context
  91. */
  92. serialize(context) {
  93. const { write } = context;
  94. write(this.cssLayer);
  95. write(this.supports);
  96. write(this.media);
  97. write(this.inheritance);
  98. super.serialize(context);
  99. }
  100. /**
  101. * @param {ObjectDeserializerContext} context context
  102. * @returns {CssModule} the deserialized object
  103. */
  104. static deserialize(context) {
  105. const obj = new CssModule({
  106. // will be deserialized by Module
  107. layer: null,
  108. type: "",
  109. // will be filled by updateCacheModule
  110. resource: "",
  111. context: "",
  112. request: null,
  113. userRequest: null,
  114. rawRequest: null,
  115. loaders: null,
  116. matchResource: null,
  117. parser: null,
  118. parserOptions: null,
  119. generator: null,
  120. generatorOptions: null,
  121. resolveOptions: null,
  122. cssLayer: null,
  123. supports: null,
  124. media: null,
  125. inheritance: null
  126. });
  127. obj.deserialize(context);
  128. return obj;
  129. }
  130. deserialize(context) {
  131. const { read } = context;
  132. this.cssLayer = read();
  133. this.supports = read();
  134. this.media = read();
  135. this.inheritance = read();
  136. super.deserialize(context);
  137. }
  138. }
  139. makeSerializable(CssModule, "webpack/lib/CssModule");
  140. module.exports = CssModule;