CssIcssImportDependency.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 { cssExportConvention } = require("../util/conventions");
  8. const makeSerializable = require("../util/makeSerializable");
  9. const memoize = require("../util/memoize");
  10. const CssImportDependency = require("./CssImportDependency");
  11. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  12. /** @typedef {import("../Dependency")} Dependency */
  13. /** @typedef {import("../DependencyTemplate").CssDependencyTemplateContext} DependencyTemplateContext */
  14. /** @typedef {import("../Module")} Module */
  15. /** @typedef {import("../CssModule")} CssModule */
  16. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  17. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  18. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  19. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  20. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  21. /** @typedef {import("../Dependency").ReferencedExports} ReferencedExports */
  22. /** @typedef {import("../../declarations/WebpackOptions").CssGeneratorExportsConvention} CssGeneratorExportsConvention */
  23. /** @typedef {import("./CssIcssExportDependency").ExportMode} ExportMode */
  24. /** @typedef {import("./CssIcssExportDependency").ExportType} ExportType */
  25. const getCssIcssExportDependency = memoize(() =>
  26. require("./CssIcssExportDependency")
  27. );
  28. class CssIcssImportDependency extends CssImportDependency {
  29. /**
  30. * Example of dependency:
  31. *
  32. * :import('./style.css') { value: name }
  33. * @param {string} request request request path which needs resolving
  34. * @param {Range} range the range of dependency
  35. * @param {"local" | "global"} mode mode of the parsed CSS
  36. * @param {string} name name
  37. * @param {string=} exportName export value
  38. * @param {ExportMode=} exportMode export mode
  39. * @param {ExportType=} exportType export type
  40. */
  41. constructor(
  42. request,
  43. range,
  44. mode,
  45. name,
  46. exportName = undefined,
  47. exportMode = getCssIcssExportDependency().EXPORT_MODE.NONE,
  48. exportType = getCssIcssExportDependency().EXPORT_TYPE.NORMAL
  49. ) {
  50. super(request, range, mode);
  51. this.name = name;
  52. this.value = exportName;
  53. this.interpolate = true;
  54. this.exportMode = exportMode;
  55. this.exportType = exportType;
  56. }
  57. get type() {
  58. return "css :import";
  59. }
  60. /**
  61. * @returns {string | null} an identifier to merge equal requests
  62. */
  63. getResourceIdentifier() {
  64. return `${super.getResourceIdentifier()}|mode${this.mode}|name${this.name}`;
  65. }
  66. /**
  67. * @param {string} name export name
  68. * @param {CssGeneratorExportsConvention} convention convention of the export name
  69. * @returns {string[]} convention results
  70. */
  71. getExportsConventionNames(name, convention) {
  72. return cssExportConvention(name, convention);
  73. }
  74. /**
  75. * Returns list of exports referenced by this dependency
  76. * @param {ModuleGraph} moduleGraph module graph
  77. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  78. * @returns {ReferencedExports} referenced exports
  79. */
  80. getReferencedExports(moduleGraph, runtime) {
  81. return [
  82. {
  83. name: [this.name],
  84. canMangle: true
  85. }
  86. ];
  87. }
  88. /**
  89. * Returns warnings
  90. * @param {ModuleGraph} moduleGraph module graph
  91. * @returns {WebpackError[] | null | undefined} warnings
  92. */
  93. getWarnings(moduleGraph) {
  94. const module = moduleGraph.getModule(this);
  95. if (
  96. module &&
  97. !moduleGraph.getExportsInfo(module).isExportProvided(this.name)
  98. ) {
  99. const error = new WebpackError(
  100. `Referenced name "${this.name}" in "${this.userRequest}" not found`
  101. );
  102. error.module = module;
  103. return [error];
  104. }
  105. return null;
  106. }
  107. /**
  108. * @param {ObjectSerializerContext} context context
  109. */
  110. serialize(context) {
  111. const { write } = context;
  112. write(this.name);
  113. write(this.value);
  114. write(this.interpolate);
  115. write(this.exportMode);
  116. write(this.exportType);
  117. super.serialize(context);
  118. }
  119. /**
  120. * @param {ObjectDeserializerContext} context context
  121. */
  122. deserialize(context) {
  123. const { read } = context;
  124. this.name = read();
  125. this.value = read();
  126. this.interpolate = read();
  127. this.exportMode = read();
  128. this.exportType = read();
  129. super.deserialize(context);
  130. }
  131. }
  132. CssIcssImportDependency.Template = class CssIcssImportDependencyTemplate extends (
  133. CssImportDependency.Template
  134. ) {
  135. /**
  136. * @param {Dependency} dependency the dependency for which the template should be applied
  137. * @param {ReplaceSource} source the current replace source which can be modified
  138. * @param {DependencyTemplateContext} templateContext the context object
  139. * @returns {void}
  140. */
  141. apply(dependency, source, templateContext) {
  142. const dep = /** @type {CssIcssImportDependency} */ (dependency);
  143. if (dep.value) {
  144. const { moduleGraph } = templateContext;
  145. const module =
  146. /** @type {CssModule} */
  147. (moduleGraph.getModule(dep));
  148. const CssIcssExportDependency = getCssIcssExportDependency();
  149. const template = new CssIcssExportDependency.Template();
  150. const originalName = dep.name;
  151. const originalExportName = dep.value;
  152. dep.value = originalName;
  153. dep.name = originalExportName;
  154. template.apply(dep, source, { ...templateContext, module });
  155. dep.name = originalName;
  156. dep.value = originalExportName;
  157. }
  158. }
  159. };
  160. makeSerializable(
  161. CssIcssImportDependency,
  162. "webpack/lib/dependencies/CssIcssImportDependency"
  163. );
  164. module.exports = CssIcssImportDependency;