CssExportDependency.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/CssExportsGenerator")} CssExportsGenerator */
  18. /** @typedef {import("../css/CssGenerator")} CssGenerator */
  19. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  20. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  21. /** @typedef {import("../util/Hash")} Hash */
  22. class CssExportDependency extends NullDependency {
  23. /**
  24. * @param {string} name name
  25. * @param {string} value value
  26. */
  27. constructor(name, value) {
  28. super();
  29. this.name = name;
  30. this.value = value;
  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 = /** @type {CssGenerator | CssExportsGenerator} */ (
  55. module.generator
  56. ).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. const module = /** @type {CssModule} */ (
  74. chunkGraph.moduleGraph.getParentModule(this)
  75. );
  76. const generator = /** @type {CssGenerator | CssExportsGenerator} */ (
  77. module.generator
  78. );
  79. const names = this.getExportsConventionNames(
  80. this.name,
  81. generator.convention
  82. );
  83. hash.update(`exportsConvention`);
  84. hash.update(JSON.stringify(names));
  85. }
  86. /**
  87. * @param {ObjectSerializerContext} context context
  88. */
  89. serialize(context) {
  90. const { write } = context;
  91. write(this.name);
  92. write(this.value);
  93. super.serialize(context);
  94. }
  95. /**
  96. * @param {ObjectDeserializerContext} context context
  97. */
  98. deserialize(context) {
  99. const { read } = context;
  100. this.name = read();
  101. this.value = read();
  102. super.deserialize(context);
  103. }
  104. }
  105. CssExportDependency.Template = class CssExportDependencyTemplate extends (
  106. NullDependency.Template
  107. ) {
  108. /**
  109. * @param {Dependency} dependency the dependency for which the template should be applied
  110. * @param {ReplaceSource} source the current replace source which can be modified
  111. * @param {DependencyTemplateContext} templateContext the context object
  112. * @returns {void}
  113. */
  114. apply(
  115. dependency,
  116. source,
  117. { cssExportsData, module: m, runtime, moduleGraph }
  118. ) {
  119. const dep = /** @type {CssExportDependency} */ (dependency);
  120. const module = /** @type {CssModule} */ (m);
  121. const convention = /** @type {CssGenerator | CssExportsGenerator} */ (
  122. module.generator
  123. ).convention;
  124. const names = dep.getExportsConventionNames(dep.name, convention);
  125. const usedNames = /** @type {string[]} */ (
  126. names
  127. .map(name =>
  128. moduleGraph.getExportInfo(module, name).getUsedName(name, runtime)
  129. )
  130. .filter(Boolean)
  131. );
  132. if (usedNames.length === 0) return;
  133. for (const used of usedNames) {
  134. cssExportsData.exports.set(used, dep.value);
  135. }
  136. }
  137. };
  138. makeSerializable(
  139. CssExportDependency,
  140. "webpack/lib/dependencies/CssExportDependency"
  141. );
  142. module.exports = CssExportDependency;