CssLocalIdentifierDependency.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 createHash = require("../util/createHash");
  8. const { makePathsRelative } = require("../util/identifier");
  9. const makeSerializable = require("../util/makeSerializable");
  10. const memoize = require("../util/memoize");
  11. const NullDependency = require("./NullDependency");
  12. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  13. /** @typedef {import("../../declarations/WebpackOptions").CssGeneratorExportsConvention} CssGeneratorExportsConvention */
  14. /** @typedef {import("../../declarations/WebpackOptions").CssGeneratorLocalIdentName} CssGeneratorLocalIdentName */
  15. /** @typedef {import("../ChunkGraph")} ChunkGraph */
  16. /** @typedef {import("../CssModule")} CssModule */
  17. /** @typedef {import("../Dependency")} Dependency */
  18. /** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
  19. /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
  20. /** @typedef {import("../DependencyTemplate").CssDependencyTemplateContext} DependencyTemplateContext */
  21. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  22. /** @typedef {import("../NormalModuleFactory").ResourceDataWithData} ResourceDataWithData */
  23. /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
  24. /** @typedef {import("../css/CssGenerator")} CssGenerator */
  25. /** @typedef {import("../css/CssParser").Range} Range */
  26. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  27. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  28. /** @typedef {import("../util/Hash")} Hash */
  29. /** @typedef {import("../util/createHash").Algorithm} Algorithm */
  30. const getCssParser = memoize(() => require("../css/CssParser"));
  31. /**
  32. * @param {string} local css local
  33. * @param {CssModule} module module
  34. * @param {ChunkGraph} chunkGraph chunk graph
  35. * @param {RuntimeTemplate} runtimeTemplate runtime template
  36. * @returns {string} local ident
  37. */
  38. const getLocalIdent = (local, module, chunkGraph, runtimeTemplate) => {
  39. const localIdentName =
  40. /** @type {CssGenerator} */
  41. (module.generator).localIdentName;
  42. const relativeResourcePath = makePathsRelative(
  43. /** @type {string} */
  44. (module.context),
  45. module.matchResource || module.resource,
  46. runtimeTemplate.compilation.compiler.root
  47. );
  48. const { hashFunction, hashDigest, hashDigestLength, hashSalt, uniqueName } =
  49. runtimeTemplate.outputOptions;
  50. const hash = createHash(/** @type {Algorithm} */ (hashFunction));
  51. if (hashSalt) {
  52. hash.update(hashSalt);
  53. }
  54. hash.update(relativeResourcePath);
  55. if (!/\[local\]/.test(localIdentName)) {
  56. hash.update(local);
  57. }
  58. const localIdentHash =
  59. /** @type {string} */
  60. (hash.digest(hashDigest)).slice(0, hashDigestLength);
  61. return runtimeTemplate.compilation
  62. .getPath(localIdentName, {
  63. filename: relativeResourcePath,
  64. hash: localIdentHash,
  65. contentHash: localIdentHash,
  66. chunkGraph,
  67. module
  68. })
  69. .replace(/\[local\]/g, local)
  70. .replace(/\[uniqueName\]/g, /** @type {string} */ (uniqueName))
  71. .replace(/^((-?[0-9])|--)/, "_$1");
  72. };
  73. class CssLocalIdentifierDependency extends NullDependency {
  74. /**
  75. * @param {string} name name
  76. * @param {Range} range range
  77. * @param {string=} prefix prefix
  78. */
  79. constructor(name, range, prefix = "") {
  80. super();
  81. this.name = name;
  82. this.range = range;
  83. this.prefix = prefix;
  84. this._conventionNames = undefined;
  85. this._hashUpdate = undefined;
  86. }
  87. get type() {
  88. return "css local identifier";
  89. }
  90. /**
  91. * @param {string} name export name
  92. * @param {CssGeneratorExportsConvention} convention convention of the export name
  93. * @returns {string[]} convention results
  94. */
  95. getExportsConventionNames(name, convention) {
  96. if (this._conventionNames) {
  97. return this._conventionNames;
  98. }
  99. this._conventionNames = cssExportConvention(this.name, convention);
  100. return this._conventionNames;
  101. }
  102. /**
  103. * Returns the exported names
  104. * @param {ModuleGraph} moduleGraph module graph
  105. * @returns {ExportsSpec | undefined} export names
  106. */
  107. getExports(moduleGraph) {
  108. const module = /** @type {CssModule} */ (moduleGraph.getParentModule(this));
  109. const convention =
  110. /** @type {CssGenerator} */
  111. (module.generator).convention;
  112. const names = this.getExportsConventionNames(this.name, convention);
  113. return {
  114. exports: names.map(name => ({
  115. name,
  116. canMangle: true
  117. })),
  118. dependencies: undefined
  119. };
  120. }
  121. /**
  122. * Update the hash
  123. * @param {Hash} hash hash to be updated
  124. * @param {UpdateHashContext} context context
  125. * @returns {void}
  126. */
  127. updateHash(hash, { chunkGraph }) {
  128. if (this._hashUpdate === undefined) {
  129. const module =
  130. /** @type {CssModule} */
  131. (chunkGraph.moduleGraph.getParentModule(this));
  132. const generator =
  133. /** @type {CssGenerator} */
  134. (module.generator);
  135. const names = this.getExportsConventionNames(
  136. this.name,
  137. generator.convention
  138. );
  139. this._hashUpdate = `exportsConvention|${JSON.stringify(names)}|localIdentName|${JSON.stringify(generator.localIdentName)}`;
  140. }
  141. hash.update(this._hashUpdate);
  142. }
  143. /**
  144. * @param {ObjectSerializerContext} context context
  145. */
  146. serialize(context) {
  147. const { write } = context;
  148. write(this.name);
  149. write(this.range);
  150. write(this.prefix);
  151. super.serialize(context);
  152. }
  153. /**
  154. * @param {ObjectDeserializerContext} context context
  155. */
  156. deserialize(context) {
  157. const { read } = context;
  158. this.name = read();
  159. this.range = read();
  160. this.prefix = read();
  161. super.deserialize(context);
  162. }
  163. }
  164. CssLocalIdentifierDependency.Template = class CssLocalIdentifierDependencyTemplate extends (
  165. NullDependency.Template
  166. ) {
  167. /**
  168. * @param {Dependency} dependency the dependency for which the template should be applied
  169. * @param {string} local local name
  170. * @param {DependencyTemplateContext} templateContext the context object
  171. * @returns {string} identifier
  172. */
  173. static getIdentifier(
  174. dependency,
  175. local,
  176. { module: m, chunkGraph, runtimeTemplate }
  177. ) {
  178. const dep = /** @type {CssLocalIdentifierDependency} */ (dependency);
  179. const module = /** @type {CssModule} */ (m);
  180. return (
  181. dep.prefix +
  182. getCssParser().escapeIdentifier(
  183. getLocalIdent(local, module, chunkGraph, runtimeTemplate)
  184. )
  185. );
  186. }
  187. /**
  188. * @param {Dependency} dependency the dependency for which the template should be applied
  189. * @param {ReplaceSource} source the current replace source which can be modified
  190. * @param {DependencyTemplateContext} templateContext the context object
  191. * @returns {void}
  192. */
  193. apply(dependency, source, templateContext) {
  194. const { module: m, moduleGraph, runtime, cssData } = templateContext;
  195. const dep = /** @type {CssLocalIdentifierDependency} */ (dependency);
  196. const module = /** @type {CssModule} */ (m);
  197. const convention =
  198. /** @type {CssGenerator} */
  199. (module.generator).convention;
  200. const names = dep.getExportsConventionNames(dep.name, convention);
  201. const usedNames =
  202. /** @type {(string)[]} */
  203. (
  204. names
  205. .map(name =>
  206. moduleGraph.getExportInfo(module, name).getUsedName(name, runtime)
  207. )
  208. .filter(Boolean)
  209. );
  210. const local = usedNames.length === 0 ? names[0] : usedNames[0];
  211. const identifier = CssLocalIdentifierDependencyTemplate.getIdentifier(
  212. dep,
  213. local,
  214. templateContext
  215. );
  216. source.replace(dep.range[0], dep.range[1] - 1, identifier);
  217. for (const used of usedNames.concat(names)) {
  218. cssData.exports.set(used, identifier);
  219. }
  220. }
  221. };
  222. makeSerializable(
  223. CssLocalIdentifierDependency,
  224. "webpack/lib/dependencies/CssLocalIdentifierDependency"
  225. );
  226. module.exports = CssLocalIdentifierDependency;