CssIcssImportDependency.js 3.2 KB

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