CssLocalIdentifierDependency.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 NullDependency = require("./NullDependency");
  8. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  9. /** @typedef {import("../Dependency")} Dependency */
  10. /** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
  11. /** @typedef {import("../DependencyTemplate").CssDependencyTemplateContext} DependencyTemplateContext */
  12. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  13. class CssLocalIdentifierDependency extends NullDependency {
  14. /**
  15. * @param {string} name name
  16. * @param {[number, number]} range range
  17. * @param {string=} prefix prefix
  18. */
  19. constructor(name, range, prefix = "") {
  20. super();
  21. this.name = name;
  22. this.range = range;
  23. this.prefix = prefix;
  24. }
  25. get type() {
  26. return "css local identifier";
  27. }
  28. /**
  29. * Returns the exported names
  30. * @param {ModuleGraph} moduleGraph module graph
  31. * @returns {ExportsSpec | undefined} export names
  32. */
  33. getExports(moduleGraph) {
  34. const name = this.name;
  35. return {
  36. exports: [
  37. {
  38. name,
  39. canMangle: true
  40. }
  41. ],
  42. dependencies: undefined
  43. };
  44. }
  45. serialize(context) {
  46. const { write } = context;
  47. write(this.name);
  48. write(this.range);
  49. write(this.prefix);
  50. super.serialize(context);
  51. }
  52. deserialize(context) {
  53. const { read } = context;
  54. this.name = read();
  55. this.range = read();
  56. this.prefix = read();
  57. super.deserialize(context);
  58. }
  59. }
  60. const escapeCssIdentifier = (str, omitUnderscore) => {
  61. const escaped = `${str}`.replace(
  62. // cspell:word uffff
  63. /[^a-zA-Z0-9_\u0081-\uffff-]/g,
  64. s => `\\${s}`
  65. );
  66. return !omitUnderscore && /^(?!--)[0-9-]/.test(escaped)
  67. ? `_${escaped}`
  68. : escaped;
  69. };
  70. CssLocalIdentifierDependency.Template = class CssLocalIdentifierDependencyTemplate extends (
  71. NullDependency.Template
  72. ) {
  73. /**
  74. * @param {Dependency} dependency the dependency for which the template should be applied
  75. * @param {ReplaceSource} source the current replace source which can be modified
  76. * @param {DependencyTemplateContext} templateContext the context object
  77. * @returns {void}
  78. */
  79. apply(
  80. dependency,
  81. source,
  82. { module, moduleGraph, chunkGraph, runtime, runtimeTemplate, cssExports }
  83. ) {
  84. const dep = /** @type {CssLocalIdentifierDependency} */ (dependency);
  85. const used = moduleGraph
  86. .getExportInfo(module, dep.name)
  87. .getUsedName(dep.name, runtime);
  88. const moduleId = chunkGraph.getModuleId(module);
  89. const identifier =
  90. dep.prefix +
  91. (runtimeTemplate.outputOptions.uniqueName
  92. ? runtimeTemplate.outputOptions.uniqueName + "-"
  93. : "") +
  94. (used ? moduleId + "-" + used : "-");
  95. source.replace(
  96. dep.range[0],
  97. dep.range[1] - 1,
  98. escapeCssIdentifier(identifier, dep.prefix)
  99. );
  100. if (used) cssExports.set(used, identifier);
  101. }
  102. };
  103. makeSerializable(
  104. CssLocalIdentifierDependency,
  105. "webpack/lib/dependencies/CssLocalIdentifierDependency"
  106. );
  107. module.exports = CssLocalIdentifierDependency;