RecordIdsPlugin.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { compareNumbers } = require("./util/comparators");
  7. const identifierUtils = require("./util/identifier");
  8. /** @typedef {import("./Chunk")} Chunk */
  9. /** @typedef {import("./Compiler")} Compiler */
  10. /** @typedef {import("./Module")} Module */
  11. /**
  12. * @typedef {object} RecordsChunks
  13. * @property {Record<string, number>=} byName
  14. * @property {Record<string, number>=} bySource
  15. * @property {number[]=} usedIds
  16. */
  17. /**
  18. * @typedef {object} RecordsModules
  19. * @property {Record<string, number>=} byIdentifier
  20. * @property {Record<string, number>=} bySource
  21. * @property {number[]=} usedIds
  22. */
  23. /**
  24. * @typedef {object} Records
  25. * @property {RecordsChunks=} chunks
  26. * @property {RecordsModules=} modules
  27. */
  28. class RecordIdsPlugin {
  29. /**
  30. * @param {object} options Options object
  31. * @param {boolean=} options.portableIds true, when ids need to be portable
  32. */
  33. constructor(options) {
  34. this.options = options || {};
  35. }
  36. /**
  37. * @param {Compiler} compiler the Compiler
  38. * @returns {void}
  39. */
  40. apply(compiler) {
  41. const portableIds = this.options.portableIds;
  42. const makePathsRelative =
  43. identifierUtils.makePathsRelative.bindContextCache(
  44. compiler.context,
  45. compiler.root
  46. );
  47. /**
  48. * @param {Module} module the module
  49. * @returns {string} the (portable) identifier
  50. */
  51. const getModuleIdentifier = module => {
  52. if (portableIds) {
  53. return makePathsRelative(module.identifier());
  54. }
  55. return module.identifier();
  56. };
  57. compiler.hooks.compilation.tap("RecordIdsPlugin", compilation => {
  58. compilation.hooks.recordModules.tap(
  59. "RecordIdsPlugin",
  60. /**
  61. * @param {Iterable<Module>} modules the modules array
  62. * @param {Records} records the records object
  63. * @returns {void}
  64. */
  65. (modules, records) => {
  66. const chunkGraph = compilation.chunkGraph;
  67. if (!records.modules) records.modules = {};
  68. if (!records.modules.byIdentifier) records.modules.byIdentifier = {};
  69. /** @type {Set<number>} */
  70. const usedIds = new Set();
  71. for (const module of modules) {
  72. const moduleId = chunkGraph.getModuleId(module);
  73. if (typeof moduleId !== "number") continue;
  74. const identifier = getModuleIdentifier(module);
  75. records.modules.byIdentifier[identifier] = moduleId;
  76. usedIds.add(moduleId);
  77. }
  78. records.modules.usedIds = Array.from(usedIds).sort(compareNumbers);
  79. }
  80. );
  81. compilation.hooks.reviveModules.tap(
  82. "RecordIdsPlugin",
  83. /**
  84. * @param {Iterable<Module>} modules the modules array
  85. * @param {Records} records the records object
  86. * @returns {void}
  87. */
  88. (modules, records) => {
  89. if (!records.modules) return;
  90. if (records.modules.byIdentifier) {
  91. const chunkGraph = compilation.chunkGraph;
  92. /** @type {Set<number>} */
  93. const usedIds = new Set();
  94. for (const module of modules) {
  95. const moduleId = chunkGraph.getModuleId(module);
  96. if (moduleId !== null) continue;
  97. const identifier = getModuleIdentifier(module);
  98. const id = records.modules.byIdentifier[identifier];
  99. if (id === undefined) continue;
  100. if (usedIds.has(id)) continue;
  101. usedIds.add(id);
  102. chunkGraph.setModuleId(module, id);
  103. }
  104. }
  105. if (Array.isArray(records.modules.usedIds)) {
  106. compilation.usedModuleIds = new Set(records.modules.usedIds);
  107. }
  108. }
  109. );
  110. /**
  111. * @param {Chunk} chunk the chunk
  112. * @returns {string[]} sources of the chunk
  113. */
  114. const getChunkSources = chunk => {
  115. /** @type {string[]} */
  116. const sources = [];
  117. for (const chunkGroup of chunk.groupsIterable) {
  118. const index = chunkGroup.chunks.indexOf(chunk);
  119. if (chunkGroup.name) {
  120. sources.push(`${index} ${chunkGroup.name}`);
  121. } else {
  122. for (const origin of chunkGroup.origins) {
  123. if (origin.module) {
  124. if (origin.request) {
  125. sources.push(
  126. `${index} ${getModuleIdentifier(origin.module)} ${
  127. origin.request
  128. }`
  129. );
  130. } else if (typeof origin.loc === "string") {
  131. sources.push(
  132. `${index} ${getModuleIdentifier(origin.module)} ${
  133. origin.loc
  134. }`
  135. );
  136. } else if (
  137. origin.loc &&
  138. typeof origin.loc === "object" &&
  139. "start" in origin.loc
  140. ) {
  141. sources.push(
  142. `${index} ${getModuleIdentifier(
  143. origin.module
  144. )} ${JSON.stringify(origin.loc.start)}`
  145. );
  146. }
  147. }
  148. }
  149. }
  150. }
  151. return sources;
  152. };
  153. compilation.hooks.recordChunks.tap(
  154. "RecordIdsPlugin",
  155. /**
  156. * @param {Iterable<Chunk>} chunks the chunks array
  157. * @param {Records} records the records object
  158. * @returns {void}
  159. */
  160. (chunks, records) => {
  161. if (!records.chunks) records.chunks = {};
  162. if (!records.chunks.byName) records.chunks.byName = {};
  163. if (!records.chunks.bySource) records.chunks.bySource = {};
  164. /** @type {Set<number>} */
  165. const usedIds = new Set();
  166. for (const chunk of chunks) {
  167. if (typeof chunk.id !== "number") continue;
  168. const name = chunk.name;
  169. if (name) records.chunks.byName[name] = chunk.id;
  170. const sources = getChunkSources(chunk);
  171. for (const source of sources) {
  172. records.chunks.bySource[source] = chunk.id;
  173. }
  174. usedIds.add(chunk.id);
  175. }
  176. records.chunks.usedIds = Array.from(usedIds).sort(compareNumbers);
  177. }
  178. );
  179. compilation.hooks.reviveChunks.tap(
  180. "RecordIdsPlugin",
  181. /**
  182. * @param {Iterable<Chunk>} chunks the chunks array
  183. * @param {Records} records the records object
  184. * @returns {void}
  185. */
  186. (chunks, records) => {
  187. if (!records.chunks) return;
  188. /** @type {Set<number>} */
  189. const usedIds = new Set();
  190. if (records.chunks.byName) {
  191. for (const chunk of chunks) {
  192. if (chunk.id !== null) continue;
  193. if (!chunk.name) continue;
  194. const id = records.chunks.byName[chunk.name];
  195. if (id === undefined) continue;
  196. if (usedIds.has(id)) continue;
  197. usedIds.add(id);
  198. chunk.id = id;
  199. chunk.ids = [id];
  200. }
  201. }
  202. if (records.chunks.bySource) {
  203. for (const chunk of chunks) {
  204. if (chunk.id !== null) continue;
  205. const sources = getChunkSources(chunk);
  206. for (const source of sources) {
  207. const id = records.chunks.bySource[source];
  208. if (id === undefined) continue;
  209. if (usedIds.has(id)) continue;
  210. usedIds.add(id);
  211. chunk.id = id;
  212. chunk.ids = [id];
  213. break;
  214. }
  215. }
  216. }
  217. if (Array.isArray(records.chunks.usedIds)) {
  218. compilation.usedChunkIds = new Set(records.chunks.usedIds);
  219. }
  220. }
  221. );
  222. });
  223. }
  224. }
  225. module.exports = RecordIdsPlugin;