CssModule.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Alexander Akait @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, supports: Supports, media: Media, inheritance: Inheritance }} 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. // We generate extra code for HMR, so we need to invalidate the module
  55. if (this.hot) {
  56. identifier += `|${this.hot}`;
  57. }
  58. return identifier;
  59. }
  60. /**
  61. * @param {RequestShortener} requestShortener the request shortener
  62. * @returns {string} a user readable identifier of the module
  63. */
  64. readableIdentifier(requestShortener) {
  65. const readableIdentifier = super.readableIdentifier(requestShortener);
  66. let identifier = `css ${readableIdentifier}`;
  67. if (this.cssLayer) {
  68. identifier += ` (layer: ${this.cssLayer})`;
  69. }
  70. if (this.supports) {
  71. identifier += ` (supports: ${this.supports})`;
  72. }
  73. if (this.media) {
  74. identifier += ` (media: ${this.media})`;
  75. }
  76. return identifier;
  77. }
  78. /**
  79. * Assuming this module is in the cache. Update the (cached) module with
  80. * the fresh module from the factory. Usually updates internal references
  81. * and properties.
  82. * @param {Module} module fresh module
  83. * @returns {void}
  84. */
  85. updateCacheModule(module) {
  86. super.updateCacheModule(module);
  87. const m = /** @type {CssModule} */ (module);
  88. this.cssLayer = m.cssLayer;
  89. this.supports = m.supports;
  90. this.media = m.media;
  91. this.inheritance = m.inheritance;
  92. }
  93. /**
  94. * @param {ObjectSerializerContext} context context
  95. */
  96. serialize(context) {
  97. const { write } = context;
  98. write(this.cssLayer);
  99. write(this.supports);
  100. write(this.media);
  101. write(this.inheritance);
  102. super.serialize(context);
  103. }
  104. /**
  105. * @param {ObjectDeserializerContext} context context
  106. * @returns {CssModule} the deserialized object
  107. */
  108. static deserialize(context) {
  109. const obj = new CssModule({
  110. // will be deserialized by Module
  111. layer: /** @type {EXPECTED_ANY} */ (null),
  112. type: "",
  113. // will be filled by updateCacheModule
  114. resource: "",
  115. context: "",
  116. request: /** @type {EXPECTED_ANY} */ (null),
  117. userRequest: /** @type {EXPECTED_ANY} */ (null),
  118. rawRequest: /** @type {EXPECTED_ANY} */ (null),
  119. loaders: /** @type {EXPECTED_ANY} */ (null),
  120. matchResource: /** @type {EXPECTED_ANY} */ (null),
  121. parser: /** @type {EXPECTED_ANY} */ (null),
  122. parserOptions: /** @type {EXPECTED_ANY} */ (null),
  123. generator: /** @type {EXPECTED_ANY} */ (null),
  124. generatorOptions: /** @type {EXPECTED_ANY} */ (null),
  125. resolveOptions: /** @type {EXPECTED_ANY} */ (null),
  126. cssLayer: /** @type {EXPECTED_ANY} */ (null),
  127. supports: /** @type {EXPECTED_ANY} */ (null),
  128. media: /** @type {EXPECTED_ANY} */ (null),
  129. inheritance: /** @type {EXPECTED_ANY} */ (null)
  130. });
  131. obj.deserialize(context);
  132. return obj;
  133. }
  134. /**
  135. * @param {ObjectDeserializerContext} context context
  136. * @returns {TODO} Module
  137. */
  138. deserialize(context) {
  139. const { read } = context;
  140. this.cssLayer = read();
  141. this.supports = read();
  142. this.media = read();
  143. this.inheritance = read();
  144. super.deserialize(context);
  145. }
  146. }
  147. makeSerializable(CssModule, "webpack/lib/CssModule");
  148. module.exports = CssModule;