CssIcssImportDependency.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Ivan Kopeykin @vankop
  4. */
  5. "use strict";
  6. const WebpackError = require("../WebpackError");
  7. const makeSerializable = require("../util/makeSerializable");
  8. const CssImportDependency = require("./CssImportDependency");
  9. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  10. /** @typedef {import("../Dependency")} Dependency */
  11. /** @typedef {import("../DependencyTemplate").CssDependencyTemplateContext} DependencyTemplateContext */
  12. /** @typedef {import("../Module")} Module */
  13. /** @typedef {import("../CssModule")} CssModule */
  14. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  15. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  16. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  17. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  18. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  19. /** @typedef {import("../Dependency").ReferencedExports} ReferencedExports */
  20. /** @typedef {import("../../declarations/WebpackOptions").CssGeneratorExportsConvention} CssGeneratorExportsConvention */
  21. /** @typedef {import("./CssIcssExportDependency").ExportMode} ExportMode */
  22. /** @typedef {import("./CssIcssExportDependency").ExportType} ExportType */
  23. class CssIcssImportDependency extends CssImportDependency {
  24. /**
  25. * Example of dependency:
  26. *
  27. * :import('./style.css') { value: name }
  28. * @param {string} request request request path which needs resolving
  29. * @param {Range} range the range of dependency
  30. * @param {"local" | "global"} mode mode of the parsed CSS
  31. * @param {string} importName import name (`name` from example)
  32. * @param {string} localName local name (`value` from example)
  33. */
  34. constructor(request, range, mode, importName, localName) {
  35. super(request, range, mode);
  36. this.importName = importName;
  37. this.localName = localName;
  38. }
  39. get type() {
  40. return "css :import";
  41. }
  42. /**
  43. * Returns list of exports referenced by this dependency
  44. * @param {ModuleGraph} moduleGraph module graph
  45. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  46. * @returns {ReferencedExports} referenced exports
  47. */
  48. getReferencedExports(moduleGraph, runtime) {
  49. return [
  50. {
  51. name: [this.importName],
  52. canMangle: true
  53. }
  54. ];
  55. }
  56. /**
  57. * Returns warnings.
  58. * @param {ModuleGraph} moduleGraph module graph
  59. * @returns {WebpackError[] | null | undefined} warnings
  60. */
  61. getWarnings(moduleGraph) {
  62. const module = moduleGraph.getModule(this);
  63. if (
  64. module &&
  65. !moduleGraph.getExportsInfo(module).isExportProvided(this.importName)
  66. ) {
  67. const error = new WebpackError(
  68. `Referenced name "${this.importName}" in "${this.userRequest}" not found`
  69. );
  70. error.module = module;
  71. return [error];
  72. }
  73. return null;
  74. }
  75. /**
  76. * Serializes this instance into the provided serializer context.
  77. * @param {ObjectSerializerContext} context context
  78. */
  79. serialize(context) {
  80. const { write } = context;
  81. write(this.importName);
  82. write(this.localName);
  83. super.serialize(context);
  84. }
  85. /**
  86. * Restores this instance from the provided deserializer context.
  87. * @param {ObjectDeserializerContext} context context
  88. */
  89. deserialize(context) {
  90. const { read } = context;
  91. this.importName = read();
  92. this.localName = read();
  93. super.deserialize(context);
  94. }
  95. }
  96. CssIcssImportDependency.Template = class CssIcssImportDependencyTemplate extends (
  97. CssImportDependency.Template
  98. ) {
  99. /**
  100. * Applies the plugin by registering its hooks on the compiler.
  101. * @param {Dependency} dependency the dependency for which the template should be applied
  102. * @param {ReplaceSource} source the current replace source which can be modified
  103. * @param {DependencyTemplateContext} templateContext the context object
  104. * @returns {void}
  105. */
  106. apply(dependency, source, templateContext) {
  107. // Nothing
  108. }
  109. };
  110. makeSerializable(
  111. CssIcssImportDependency,
  112. "webpack/lib/dependencies/CssIcssImportDependency"
  113. );
  114. module.exports = CssIcssImportDependency;