SyncModuleIdsPlugin.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { WebpackError } = require("..");
  7. const { getUsedModuleIdsAndModules } = require("./IdHelpers");
  8. /** @typedef {import("../Compiler")} Compiler */
  9. /** @typedef {import("../Module")} Module */
  10. const plugin = "SyncModuleIdsPlugin";
  11. class SyncModuleIdsPlugin {
  12. /**
  13. * @param {object} options options
  14. * @param {string} options.path path to file
  15. * @param {string=} options.context context for module names
  16. * @param {function(Module): boolean} options.test selector for modules
  17. * @param {"read" | "create" | "merge" | "update"=} options.mode operation mode (defaults to merge)
  18. */
  19. constructor({ path, context, test, mode }) {
  20. this._path = path;
  21. this._context = context;
  22. this._test = test || (() => true);
  23. const readAndWrite = !mode || mode === "merge" || mode === "update";
  24. this._read = readAndWrite || mode === "read";
  25. this._write = readAndWrite || mode === "create";
  26. this._prune = mode === "update";
  27. }
  28. /**
  29. * Apply the plugin
  30. * @param {Compiler} compiler the compiler instance
  31. * @returns {void}
  32. */
  33. apply(compiler) {
  34. /** @type {Map<string, string | number>} */
  35. let data;
  36. let dataChanged = false;
  37. if (this._read) {
  38. compiler.hooks.readRecords.tapAsync(plugin, callback => {
  39. const fs = compiler.intermediateFileSystem;
  40. fs.readFile(this._path, (err, buffer) => {
  41. if (err) {
  42. if (err.code !== "ENOENT") {
  43. return callback(err);
  44. }
  45. return callback();
  46. }
  47. const json = JSON.parse(/** @type {Buffer} */ (buffer).toString());
  48. data = new Map();
  49. for (const key of Object.keys(json)) {
  50. data.set(key, json[key]);
  51. }
  52. dataChanged = false;
  53. return callback();
  54. });
  55. });
  56. }
  57. if (this._write) {
  58. compiler.hooks.emitRecords.tapAsync(plugin, callback => {
  59. if (!data || !dataChanged) return callback();
  60. /** @type {{[key: string]: string | number}} */
  61. const json = {};
  62. const sorted = Array.from(data).sort(([a], [b]) => (a < b ? -1 : 1));
  63. for (const [key, value] of sorted) {
  64. json[key] = value;
  65. }
  66. const fs = compiler.intermediateFileSystem;
  67. fs.writeFile(this._path, JSON.stringify(json), callback);
  68. });
  69. }
  70. compiler.hooks.thisCompilation.tap(plugin, compilation => {
  71. const associatedObjectForCache = compiler.root;
  72. const context = this._context || compiler.context;
  73. if (this._read) {
  74. compilation.hooks.reviveModules.tap(plugin, (_1, _2) => {
  75. if (!data) return;
  76. const { chunkGraph } = compilation;
  77. const [usedIds, modules] = getUsedModuleIdsAndModules(
  78. compilation,
  79. this._test
  80. );
  81. for (const module of modules) {
  82. const name = module.libIdent({
  83. context,
  84. associatedObjectForCache
  85. });
  86. if (!name) continue;
  87. const id = data.get(name);
  88. const idAsString = `${id}`;
  89. if (usedIds.has(idAsString)) {
  90. const err = new WebpackError(
  91. `SyncModuleIdsPlugin: Unable to restore id '${id}' from '${this._path}' as it's already used.`
  92. );
  93. err.module = module;
  94. compilation.errors.push(err);
  95. }
  96. chunkGraph.setModuleId(module, /** @type {string | number} */ (id));
  97. usedIds.add(idAsString);
  98. }
  99. });
  100. }
  101. if (this._write) {
  102. compilation.hooks.recordModules.tap(plugin, modules => {
  103. const { chunkGraph } = compilation;
  104. let oldData = data;
  105. if (!oldData) {
  106. oldData = data = new Map();
  107. } else if (this._prune) {
  108. data = new Map();
  109. }
  110. for (const module of modules) {
  111. if (this._test(module)) {
  112. const name = module.libIdent({
  113. context,
  114. associatedObjectForCache
  115. });
  116. if (!name) continue;
  117. const id = chunkGraph.getModuleId(module);
  118. if (id === null) continue;
  119. const oldId = oldData.get(name);
  120. if (oldId !== id) {
  121. dataChanged = true;
  122. } else if (data === oldData) {
  123. continue;
  124. }
  125. data.set(name, id);
  126. }
  127. }
  128. if (data.size !== oldData.size) dataChanged = true;
  129. });
  130. }
  131. });
  132. }
  133. }
  134. module.exports = SyncModuleIdsPlugin;