CssImportDependency.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  9. /** @typedef {import("../ChunkGraph")} ChunkGraph */
  10. /** @typedef {import("../Dependency")} Dependency */
  11. /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
  12. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  13. /** @typedef {import("../Module")} Module */
  14. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  15. /** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
  16. /** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
  17. /** @typedef {import("../css/CssParser").Range} Range */
  18. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  19. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  20. /** @typedef {import("../util/Hash")} Hash */
  21. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  22. class CssImportDependency extends ModuleDependency {
  23. /**
  24. * Example of dependency:
  25. *
  26. * \@import url("landscape.css") layer(forms) screen and (orientation: landscape) screen and (orientation: landscape);
  27. *
  28. * @param {string} request request
  29. * @param {Range} range range of the argument
  30. * @param {string | undefined} layer layer
  31. * @param {string | undefined} supports list of supports conditions
  32. * @param {string | undefined} media list of media conditions
  33. */
  34. constructor(request, range, layer, supports, media) {
  35. super(request);
  36. this.range = range;
  37. this.layer = layer;
  38. this.supports = supports;
  39. this.media = media;
  40. }
  41. get type() {
  42. return "css @import";
  43. }
  44. get category() {
  45. return "css-import";
  46. }
  47. /**
  48. * @returns {string | null} an identifier to merge equal requests
  49. */
  50. getResourceIdentifier() {
  51. let str = `context${this._context || ""}|module${this.request}`;
  52. if (this.layer) {
  53. str += `|layer${this.layer}`;
  54. }
  55. if (this.supports) {
  56. str += `|supports${this.supports}`;
  57. }
  58. if (this.media) {
  59. str += `|media${this.media}`;
  60. }
  61. return str;
  62. }
  63. /**
  64. * @param {string} context context directory
  65. * @returns {Module | null} a module
  66. */
  67. createIgnoredModule(context) {
  68. return null;
  69. }
  70. /**
  71. * @param {ObjectSerializerContext} context context
  72. */
  73. serialize(context) {
  74. const { write } = context;
  75. write(this.layer);
  76. write(this.supports);
  77. write(this.media);
  78. super.serialize(context);
  79. }
  80. /**
  81. * @param {ObjectDeserializerContext} context context
  82. */
  83. deserialize(context) {
  84. const { read } = context;
  85. this.layer = read();
  86. this.supports = read();
  87. this.media = read();
  88. super.deserialize(context);
  89. }
  90. }
  91. CssImportDependency.Template = class CssImportDependencyTemplate extends (
  92. ModuleDependency.Template
  93. ) {
  94. /**
  95. * @param {Dependency} dependency the dependency for which the template should be applied
  96. * @param {ReplaceSource} source the current replace source which can be modified
  97. * @param {DependencyTemplateContext} templateContext the context object
  98. * @returns {void}
  99. */
  100. apply(dependency, source, templateContext) {
  101. const dep = /** @type {CssImportDependency} */ (dependency);
  102. source.replace(dep.range[0], dep.range[1] - 1, "");
  103. }
  104. };
  105. makeSerializable(
  106. CssImportDependency,
  107. "webpack/lib/dependencies/CssImportDependency"
  108. );
  109. module.exports = CssImportDependency;