CssImportDependency.js 3.4 KB

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