CssIcssExportDependency.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Ivan Kopeykin @vankop
  4. */
  5. "use strict";
  6. const { cssExportConvention } = require("../util/conventions");
  7. const makeSerializable = require("../util/makeSerializable");
  8. const NullDependency = require("./NullDependency");
  9. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  10. /** @typedef {import("../../declarations/WebpackOptions").CssGeneratorExportsConvention} CssGeneratorExportsConvention */
  11. /** @typedef {import("../CssModule")} CssModule */
  12. /** @typedef {import("../Dependency")} Dependency */
  13. /** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
  14. /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
  15. /** @typedef {import("../DependencyTemplate").CssDependencyTemplateContext} DependencyTemplateContext */
  16. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  17. /** @typedef {import("../css/CssGenerator")} CssGenerator */
  18. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  19. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  20. /** @typedef {import("../util/Hash")} Hash */
  21. class CssIcssExportDependency extends NullDependency {
  22. /**
  23. * @param {string} name name
  24. * @param {string} value value
  25. */
  26. constructor(name, value) {
  27. super();
  28. this.name = name;
  29. this.value = value;
  30. this._hashUpdate = undefined;
  31. }
  32. get type() {
  33. return "css :export";
  34. }
  35. /**
  36. * @param {string} name export name
  37. * @param {CssGeneratorExportsConvention} convention convention of the export name
  38. * @returns {string[]} convention results
  39. */
  40. getExportsConventionNames(name, convention) {
  41. if (this._conventionNames) {
  42. return this._conventionNames;
  43. }
  44. this._conventionNames = cssExportConvention(name, convention);
  45. return this._conventionNames;
  46. }
  47. /**
  48. * Returns the exported names
  49. * @param {ModuleGraph} moduleGraph module graph
  50. * @returns {ExportsSpec | undefined} export names
  51. */
  52. getExports(moduleGraph) {
  53. const module = /** @type {CssModule} */ (moduleGraph.getParentModule(this));
  54. const convention =
  55. /** @type {CssGenerator} */
  56. (module.generator).convention;
  57. const names = this.getExportsConventionNames(this.name, convention);
  58. return {
  59. exports: names.map(name => ({
  60. name,
  61. canMangle: true
  62. })),
  63. dependencies: undefined
  64. };
  65. }
  66. /**
  67. * Update the hash
  68. * @param {Hash} hash hash to be updated
  69. * @param {UpdateHashContext} context context
  70. * @returns {void}
  71. */
  72. updateHash(hash, { chunkGraph }) {
  73. if (this._hashUpdate === undefined) {
  74. const module =
  75. /** @type {CssModule} */
  76. (chunkGraph.moduleGraph.getParentModule(this));
  77. const generator =
  78. /** @type {CssGenerator} */
  79. (module.generator);
  80. const names = this.getExportsConventionNames(
  81. this.name,
  82. generator.convention
  83. );
  84. this._hashUpdate = JSON.stringify(names);
  85. }
  86. hash.update("exportsConvention");
  87. hash.update(this._hashUpdate);
  88. }
  89. /**
  90. * @param {ObjectSerializerContext} context context
  91. */
  92. serialize(context) {
  93. const { write } = context;
  94. write(this.name);
  95. write(this.value);
  96. super.serialize(context);
  97. }
  98. /**
  99. * @param {ObjectDeserializerContext} context context
  100. */
  101. deserialize(context) {
  102. const { read } = context;
  103. this.name = read();
  104. this.value = read();
  105. super.deserialize(context);
  106. }
  107. }
  108. CssIcssExportDependency.Template = class CssIcssExportDependencyTemplate extends (
  109. NullDependency.Template
  110. ) {
  111. /**
  112. * @param {Dependency} dependency the dependency for which the template should be applied
  113. * @param {ReplaceSource} source the current replace source which can be modified
  114. * @param {DependencyTemplateContext} templateContext the context object
  115. * @returns {void}
  116. */
  117. apply(dependency, source, { cssData, module: m, runtime, moduleGraph }) {
  118. const dep = /** @type {CssIcssExportDependency} */ (dependency);
  119. const module = /** @type {CssModule} */ (m);
  120. const convention =
  121. /** @type {CssGenerator} */
  122. (module.generator).convention;
  123. const names = dep.getExportsConventionNames(dep.name, convention);
  124. const usedNames =
  125. /** @type {string[]} */
  126. (
  127. names
  128. .map(name =>
  129. moduleGraph.getExportInfo(module, name).getUsedName(name, runtime)
  130. )
  131. .filter(Boolean)
  132. );
  133. for (const used of usedNames.concat(names)) {
  134. cssData.exports.set(used, dep.value);
  135. }
  136. }
  137. };
  138. makeSerializable(
  139. CssIcssExportDependency,
  140. "webpack/lib/dependencies/CssIcssExportDependency"
  141. );
  142. module.exports = CssIcssExportDependency;