CssIcssImportDependency.js 3.3 KB

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