ModuleFilenameHelpers.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const createHash = require("./util/createHash");
  7. const ModuleFilenameHelpers = exports;
  8. ModuleFilenameHelpers.ALL_LOADERS_RESOURCE = "[all-loaders][resource]";
  9. ModuleFilenameHelpers.REGEXP_ALL_LOADERS_RESOURCE = /\[all-?loaders\]\[resource\]/gi;
  10. ModuleFilenameHelpers.LOADERS_RESOURCE = "[loaders][resource]";
  11. ModuleFilenameHelpers.REGEXP_LOADERS_RESOURCE = /\[loaders\]\[resource\]/gi;
  12. ModuleFilenameHelpers.RESOURCE = "[resource]";
  13. ModuleFilenameHelpers.REGEXP_RESOURCE = /\[resource\]/gi;
  14. ModuleFilenameHelpers.ABSOLUTE_RESOURCE_PATH = "[absolute-resource-path]";
  15. ModuleFilenameHelpers.REGEXP_ABSOLUTE_RESOURCE_PATH = /\[abs(olute)?-?resource-?path\]/gi;
  16. ModuleFilenameHelpers.RESOURCE_PATH = "[resource-path]";
  17. ModuleFilenameHelpers.REGEXP_RESOURCE_PATH = /\[resource-?path\]/gi;
  18. ModuleFilenameHelpers.ALL_LOADERS = "[all-loaders]";
  19. ModuleFilenameHelpers.REGEXP_ALL_LOADERS = /\[all-?loaders\]/gi;
  20. ModuleFilenameHelpers.LOADERS = "[loaders]";
  21. ModuleFilenameHelpers.REGEXP_LOADERS = /\[loaders\]/gi;
  22. ModuleFilenameHelpers.QUERY = "[query]";
  23. ModuleFilenameHelpers.REGEXP_QUERY = /\[query\]/gi;
  24. ModuleFilenameHelpers.ID = "[id]";
  25. ModuleFilenameHelpers.REGEXP_ID = /\[id\]/gi;
  26. ModuleFilenameHelpers.HASH = "[hash]";
  27. ModuleFilenameHelpers.REGEXP_HASH = /\[hash\]/gi;
  28. ModuleFilenameHelpers.NAMESPACE = "[namespace]";
  29. ModuleFilenameHelpers.REGEXP_NAMESPACE = /\[namespace\]/gi;
  30. const getAfter = (str, token) => {
  31. const idx = str.indexOf(token);
  32. return idx < 0 ? "" : str.substr(idx);
  33. };
  34. const getBefore = (str, token) => {
  35. const idx = str.lastIndexOf(token);
  36. return idx < 0 ? "" : str.substr(0, idx);
  37. };
  38. const getHash = str => {
  39. const hash = createHash("md4");
  40. hash.update(str);
  41. const digest = /** @type {string} */ (hash.digest("hex"));
  42. return digest.substr(0, 4);
  43. };
  44. const asRegExp = test => {
  45. if (typeof test === "string") {
  46. test = new RegExp("^" + test.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"));
  47. }
  48. return test;
  49. };
  50. ModuleFilenameHelpers.createFilename = (module, options, requestShortener) => {
  51. const opts = Object.assign(
  52. {
  53. namespace: "",
  54. moduleFilenameTemplate: ""
  55. },
  56. typeof options === "object"
  57. ? options
  58. : {
  59. moduleFilenameTemplate: options
  60. }
  61. );
  62. let absoluteResourcePath;
  63. let hash;
  64. let identifier;
  65. let moduleId;
  66. let shortIdentifier;
  67. if (module === undefined) module = "";
  68. if (typeof module === "string") {
  69. shortIdentifier = requestShortener.shorten(module);
  70. identifier = shortIdentifier;
  71. moduleId = "";
  72. absoluteResourcePath = module.split("!").pop();
  73. hash = getHash(identifier);
  74. } else {
  75. shortIdentifier = module.readableIdentifier(requestShortener);
  76. identifier = requestShortener.shorten(module.identifier());
  77. moduleId = module.id;
  78. absoluteResourcePath = module
  79. .identifier()
  80. .split("!")
  81. .pop();
  82. hash = getHash(identifier);
  83. }
  84. const resource = shortIdentifier.split("!").pop();
  85. const loaders = getBefore(shortIdentifier, "!");
  86. const allLoaders = getBefore(identifier, "!");
  87. const query = getAfter(resource, "?");
  88. const resourcePath = resource.substr(0, resource.length - query.length);
  89. if (typeof opts.moduleFilenameTemplate === "function") {
  90. return opts.moduleFilenameTemplate({
  91. identifier: identifier,
  92. shortIdentifier: shortIdentifier,
  93. resource: resource,
  94. resourcePath: resourcePath,
  95. absoluteResourcePath: absoluteResourcePath,
  96. allLoaders: allLoaders,
  97. query: query,
  98. moduleId: moduleId,
  99. hash: hash,
  100. namespace: opts.namespace
  101. });
  102. }
  103. return opts.moduleFilenameTemplate
  104. .replace(ModuleFilenameHelpers.REGEXP_ALL_LOADERS_RESOURCE, identifier)
  105. .replace(ModuleFilenameHelpers.REGEXP_LOADERS_RESOURCE, shortIdentifier)
  106. .replace(ModuleFilenameHelpers.REGEXP_RESOURCE, resource)
  107. .replace(ModuleFilenameHelpers.REGEXP_RESOURCE_PATH, resourcePath)
  108. .replace(
  109. ModuleFilenameHelpers.REGEXP_ABSOLUTE_RESOURCE_PATH,
  110. absoluteResourcePath
  111. )
  112. .replace(ModuleFilenameHelpers.REGEXP_ALL_LOADERS, allLoaders)
  113. .replace(ModuleFilenameHelpers.REGEXP_LOADERS, loaders)
  114. .replace(ModuleFilenameHelpers.REGEXP_QUERY, query)
  115. .replace(ModuleFilenameHelpers.REGEXP_ID, moduleId)
  116. .replace(ModuleFilenameHelpers.REGEXP_HASH, hash)
  117. .replace(ModuleFilenameHelpers.REGEXP_NAMESPACE, opts.namespace);
  118. };
  119. ModuleFilenameHelpers.replaceDuplicates = (array, fn, comparator) => {
  120. const countMap = Object.create(null);
  121. const posMap = Object.create(null);
  122. array.forEach((item, idx) => {
  123. countMap[item] = countMap[item] || [];
  124. countMap[item].push(idx);
  125. posMap[item] = 0;
  126. });
  127. if (comparator) {
  128. Object.keys(countMap).forEach(item => {
  129. countMap[item].sort(comparator);
  130. });
  131. }
  132. return array.map((item, i) => {
  133. if (countMap[item].length > 1) {
  134. if (comparator && countMap[item][0] === i) return item;
  135. return fn(item, i, posMap[item]++);
  136. } else {
  137. return item;
  138. }
  139. });
  140. };
  141. ModuleFilenameHelpers.matchPart = (str, test) => {
  142. if (!test) return true;
  143. test = asRegExp(test);
  144. if (Array.isArray(test)) {
  145. return test.map(asRegExp).some(regExp => regExp.test(str));
  146. } else {
  147. return test.test(str);
  148. }
  149. };
  150. ModuleFilenameHelpers.matchObject = (obj, str) => {
  151. if (obj.test) {
  152. if (!ModuleFilenameHelpers.matchPart(str, obj.test)) {
  153. return false;
  154. }
  155. }
  156. if (obj.include) {
  157. if (!ModuleFilenameHelpers.matchPart(str, obj.include)) {
  158. return false;
  159. }
  160. }
  161. if (obj.exclude) {
  162. if (ModuleFilenameHelpers.matchPart(str, obj.exclude)) {
  163. return false;
  164. }
  165. }
  166. return true;
  167. };