CssIcssImportDependency.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 CssIcssExportDependency = require("./CssIcssExportDependency");
  8. const CssLocalIdentifierDependency = require("./CssLocalIdentifierDependency");
  9. const ModuleDependency = require("./ModuleDependency");
  10. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  11. /** @typedef {import("../Dependency")} Dependency */
  12. /** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
  13. /** @typedef {import("../DependencyTemplate").CssDependencyTemplateContext} DependencyTemplateContext */
  14. /** @typedef {import("../Module")} Module */
  15. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  16. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  17. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  18. class CssIcssImportDependency extends ModuleDependency {
  19. /**
  20. * Example of dependency:
  21. *
  22. *:import('./style.css') { IMPORTED_NAME: v-primary }
  23. * @param {string} request request request path which needs resolving
  24. * @param {string} exportName export name
  25. * @param {[number, number]} range the range of dependency
  26. */
  27. constructor(request, exportName, range) {
  28. super(request);
  29. this.range = range;
  30. this.exportName = exportName;
  31. }
  32. get type() {
  33. return "css :import";
  34. }
  35. get category() {
  36. return "css-import";
  37. }
  38. /**
  39. * @param {ObjectSerializerContext} context context
  40. */
  41. serialize(context) {
  42. const { write } = context;
  43. write(this.range);
  44. write(this.exportName);
  45. super.serialize(context);
  46. }
  47. /**
  48. * @param {ObjectDeserializerContext} context context
  49. */
  50. deserialize(context) {
  51. const { read } = context;
  52. this.range = read();
  53. this.exportName = read();
  54. super.deserialize(context);
  55. }
  56. }
  57. CssIcssImportDependency.Template = class CssIcssImportDependencyTemplate extends (
  58. ModuleDependency.Template
  59. ) {
  60. /**
  61. * @param {Dependency} dependency the dependency for which the template should be applied
  62. * @param {ReplaceSource} source the current replace source which can be modified
  63. * @param {DependencyTemplateContext} templateContext the context object
  64. * @returns {void}
  65. */
  66. apply(dependency, source, templateContext) {
  67. const dep = /** @type {CssIcssImportDependency} */ (dependency);
  68. const { range } = dep;
  69. const module =
  70. /** @type {Module} */
  71. (templateContext.moduleGraph.getModule(dep));
  72. let value;
  73. for (const item of module.dependencies) {
  74. if (
  75. item instanceof CssLocalIdentifierDependency &&
  76. dep.exportName === item.name
  77. ) {
  78. value = CssLocalIdentifierDependency.Template.getIdentifier(
  79. item,
  80. dep.exportName,
  81. {
  82. ...templateContext,
  83. module
  84. }
  85. );
  86. break;
  87. } else if (
  88. item instanceof CssIcssExportDependency &&
  89. dep.exportName === item.name
  90. ) {
  91. value = item.value;
  92. break;
  93. }
  94. }
  95. if (!value) {
  96. throw new Error(
  97. `Imported '${dep.exportName}' name from '${dep.request}' not found`
  98. );
  99. }
  100. source.replace(range[0], range[1], value);
  101. }
  102. };
  103. makeSerializable(
  104. CssIcssImportDependency,
  105. "webpack/lib/dependencies/CssIcssImportDependency"
  106. );
  107. module.exports = CssIcssImportDependency;