CssIcssExportDependency.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 generator = /** @type {CssGenerator} */ (module.generator);
  55. const names = this.getExportsConventionNames(
  56. this.name,
  57. /** @type {CssGeneratorExportsConvention} */
  58. (generator.convention)
  59. );
  60. return {
  61. exports: names.map(name => ({
  62. name,
  63. canMangle: true
  64. })),
  65. dependencies: undefined
  66. };
  67. }
  68. /**
  69. * Update the hash
  70. * @param {Hash} hash hash to be updated
  71. * @param {UpdateHashContext} context context
  72. * @returns {void}
  73. */
  74. updateHash(hash, { chunkGraph }) {
  75. if (this._hashUpdate === undefined) {
  76. const module =
  77. /** @type {CssModule} */
  78. (chunkGraph.moduleGraph.getParentModule(this));
  79. const generator = /** @type {CssGenerator} */ (module.generator);
  80. const names = this.getExportsConventionNames(
  81. this.name,
  82. /** @type {CssGeneratorExportsConvention} */
  83. (generator.convention)
  84. );
  85. this._hashUpdate = JSON.stringify(names);
  86. }
  87. hash.update("exportsConvention");
  88. hash.update(this._hashUpdate);
  89. }
  90. /**
  91. * @param {ObjectSerializerContext} context context
  92. */
  93. serialize(context) {
  94. const { write } = context;
  95. write(this.name);
  96. write(this.value);
  97. super.serialize(context);
  98. }
  99. /**
  100. * @param {ObjectDeserializerContext} context context
  101. */
  102. deserialize(context) {
  103. const { read } = context;
  104. this.name = read();
  105. this.value = read();
  106. super.deserialize(context);
  107. }
  108. }
  109. CssIcssExportDependency.Template = class CssIcssExportDependencyTemplate extends (
  110. NullDependency.Template
  111. ) {
  112. /**
  113. * @param {Dependency} dependency the dependency for which the template should be applied
  114. * @param {ReplaceSource} source the current replace source which can be modified
  115. * @param {DependencyTemplateContext} templateContext the context object
  116. * @returns {void}
  117. */
  118. apply(dependency, source, { cssData, module: m, runtime, moduleGraph }) {
  119. const dep = /** @type {CssIcssExportDependency} */ (dependency);
  120. const module = /** @type {CssModule} */ (m);
  121. const generator = /** @type {CssGenerator} */ (module.generator);
  122. const names = dep.getExportsConventionNames(
  123. dep.name,
  124. /** @type {CssGeneratorExportsConvention} */
  125. (generator.convention)
  126. );
  127. const usedNames =
  128. /** @type {string[]} */
  129. (
  130. names
  131. .map(name =>
  132. moduleGraph.getExportInfo(module, name).getUsedName(name, runtime)
  133. )
  134. .filter(Boolean)
  135. );
  136. for (const used of usedNames.concat(names)) {
  137. cssData.exports.set(used, dep.value);
  138. }
  139. }
  140. };
  141. makeSerializable(
  142. CssIcssExportDependency,
  143. "webpack/lib/dependencies/CssIcssExportDependency"
  144. );
  145. module.exports = CssIcssExportDependency;