RecordIdsPlugin.js 6.7 KB

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