CssImportDependency.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Ivan Kopeykin @vankop
  4. */
  5. "use strict";
  6. const makeSerializable = require("../util/makeSerializable");
  7. const ModuleDependency = require("./ModuleDependency");
  8. /** @import { ReplaceSource } from "webpack-sources" */
  9. /** @import { CssParserExportType } from "../../declarations/WebpackOptions" */
  10. /** @import Dependency from "../Dependency" */
  11. /**
  12. * @import {
  13. * CssDependencyTemplateContext as DependencyTemplateContext
  14. * } from "../DependencyTemplate"
  15. */
  16. /** @import { Inheritance } from "../css/CssModule" */
  17. /** @import { Range } from "../css/CssParser" */
  18. /**
  19. * @import {
  20. * ObjectDeserializerContext,
  21. * ObjectSerializerContext
  22. * } from "../serialization/ObjectMiddleware"
  23. */
  24. class CssImportDependency extends ModuleDependency {
  25. /**
  26. * Example of dependency:
  27. * \@import url("landscape.css") layer(forms) screen and (orientation: landscape) screen and (orientation: landscape);
  28. * @param {string} request request
  29. * @param {Range} range range of the argument
  30. * @param {"local" | "global"=} mode mode of the parsed CSS
  31. * @param {string=} layer layer
  32. * @param {string=} supports list of supports conditions
  33. * @param {string=} media list of media conditions
  34. * @param {Inheritance=} inheritance layer/supports/media chain inherited from the importing module
  35. * @param {CssParserExportType=} exportType exportType inherited from the importing module
  36. */
  37. constructor(
  38. request,
  39. range,
  40. mode,
  41. layer,
  42. supports,
  43. media,
  44. inheritance,
  45. exportType
  46. ) {
  47. super(request);
  48. this.range = range;
  49. this.mode = mode;
  50. /** @type {string | undefined} */
  51. this.layer = layer;
  52. /** @type {string | undefined} */
  53. this.supports = supports;
  54. /** @type {string | undefined} */
  55. this.media = media;
  56. /** @type {Inheritance | undefined} */
  57. this.inheritance = inheritance;
  58. /** @type {CssParserExportType | undefined} */
  59. this.exportType = exportType;
  60. }
  61. get type() {
  62. return "css @import";
  63. }
  64. get category() {
  65. return `css-import${this.mode ? `-${this.mode}-module` : ""}`;
  66. }
  67. /**
  68. * Returns true if this dependency can be concatenated
  69. * @param {boolean} concatenateCommonJsModules whether optimization.concatenateModules.commonjs is enabled
  70. * @returns {boolean} true if this dependency can be concatenated
  71. */
  72. canConcatenate(concatenateCommonJsModules) {
  73. return true;
  74. }
  75. /**
  76. * Returns an identifier to merge equal requests.
  77. * @returns {string | null} an identifier to merge equal requests
  78. */
  79. getResourceIdentifier() {
  80. let str = `context${this._context || ""}|module${this.request}`;
  81. if (this.mode) {
  82. str += `|mode${this.mode}`;
  83. }
  84. if (this.layer) {
  85. str += `|layer${this.layer}`;
  86. }
  87. if (this.supports) {
  88. str += `|supports${this.supports}`;
  89. }
  90. if (this.media) {
  91. str += `|media${this.media}`;
  92. }
  93. return str;
  94. }
  95. /**
  96. * Serializes this instance into the provided serializer context.
  97. * @param {ObjectSerializerContext} context context
  98. */
  99. serialize(context) {
  100. const { write } = context;
  101. write(this.range);
  102. write(this.mode);
  103. write(this.layer);
  104. write(this.supports);
  105. write(this.media);
  106. write(this.inheritance);
  107. write(this.exportType);
  108. super.serialize(context);
  109. }
  110. /**
  111. * Restores this instance from the provided deserializer context.
  112. * @param {ObjectDeserializerContext} context context
  113. */
  114. deserialize(context) {
  115. const { read } = context;
  116. this.range = read();
  117. this.mode = read();
  118. this.layer = read();
  119. this.supports = read();
  120. this.media = read();
  121. this.inheritance = read();
  122. this.exportType = read();
  123. super.deserialize(context);
  124. }
  125. }
  126. CssImportDependency.Template = class CssImportDependencyTemplate extends (
  127. ModuleDependency.Template
  128. ) {
  129. /**
  130. * Applies the plugin by registering its hooks on the compiler.
  131. * @param {Dependency} dependency the dependency for which the template should be applied
  132. * @param {ReplaceSource} source the current replace source which can be modified
  133. * @param {DependencyTemplateContext} templateContext the context object
  134. * @returns {void}
  135. */
  136. apply(dependency, source, templateContext) {
  137. if (templateContext.type === "javascript") return;
  138. const dep = /** @type {CssImportDependency} */ (dependency);
  139. source.replace(dep.range[0], dep.range[1] - 1, "");
  140. }
  141. };
  142. makeSerializable(
  143. CssImportDependency,
  144. "webpack/lib/dependencies/CssImportDependency"
  145. );
  146. module.exports = CssImportDependency;