RealContentHashPlugin.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { SyncBailHook } = require("tapable");
  7. const { RawSource, CachedSource, CompatSource } = require("webpack-sources");
  8. const Compilation = require("../Compilation");
  9. const WebpackError = require("../WebpackError");
  10. const { compareSelect, compareStrings } = require("../util/comparators");
  11. const createHash = require("../util/createHash");
  12. /** @typedef {import("webpack-sources").Source} Source */
  13. /** @typedef {import("../Cache").Etag} Etag */
  14. /** @typedef {import("../Compilation").AssetInfo} AssetInfo */
  15. /** @typedef {import("../Compiler")} Compiler */
  16. /** @typedef {typeof import("../util/Hash")} Hash */
  17. const EMPTY_SET = new Set();
  18. /**
  19. * @template T
  20. * @param {T | T[]} itemOrItems item or items
  21. * @param {Set<T>} list list
  22. */
  23. const addToList = (itemOrItems, list) => {
  24. if (Array.isArray(itemOrItems)) {
  25. for (const item of itemOrItems) {
  26. list.add(item);
  27. }
  28. } else if (itemOrItems) {
  29. list.add(itemOrItems);
  30. }
  31. };
  32. /**
  33. * @template T
  34. * @param {T[]} input list
  35. * @param {(item: T) => Buffer} fn map function
  36. * @returns {Buffer[]} buffers without duplicates
  37. */
  38. const mapAndDeduplicateBuffers = (input, fn) => {
  39. // Buffer.equals compares size first so this should be efficient enough
  40. // If it becomes a performance problem we can use a map and group by size
  41. // instead of looping over all assets.
  42. const result = [];
  43. outer: for (const value of input) {
  44. const buf = fn(value);
  45. for (const other of result) {
  46. if (buf.equals(other)) continue outer;
  47. }
  48. result.push(buf);
  49. }
  50. return result;
  51. };
  52. /**
  53. * Escapes regular expression metacharacters
  54. * @param {string} str String to quote
  55. * @returns {string} Escaped string
  56. */
  57. const quoteMeta = str => str.replace(/[-[\]\\/{}()*+?.^$|]/g, "\\$&");
  58. const cachedSourceMap = new WeakMap();
  59. /**
  60. * @param {Source} source source
  61. * @returns {CachedSource} cached source
  62. */
  63. const toCachedSource = source => {
  64. if (source instanceof CachedSource) {
  65. return source;
  66. }
  67. const entry = cachedSourceMap.get(source);
  68. if (entry !== undefined) return entry;
  69. const newSource = new CachedSource(CompatSource.from(source));
  70. cachedSourceMap.set(source, newSource);
  71. return newSource;
  72. };
  73. /** @typedef {Set<string>} OwnHashes */
  74. /** @typedef {Set<string>} ReferencedHashes */
  75. /** @typedef {Set<string>} Hashes */
  76. /**
  77. * @typedef {object} AssetInfoForRealContentHash
  78. * @property {string} name
  79. * @property {AssetInfo} info
  80. * @property {Source} source
  81. * @property {RawSource | undefined} newSource
  82. * @property {RawSource | undefined} newSourceWithoutOwn
  83. * @property {string} content
  84. * @property {OwnHashes | undefined} ownHashes
  85. * @property {Promise<void> | undefined} contentComputePromise
  86. * @property {Promise<void> | undefined} contentComputeWithoutOwnPromise
  87. * @property {ReferencedHashes | undefined} referencedHashes
  88. * @property {Hashes} hashes
  89. */
  90. /**
  91. * @typedef {object} CompilationHooks
  92. * @property {SyncBailHook<[Buffer[], string], string | void>} updateHash
  93. */
  94. /** @type {WeakMap<Compilation, CompilationHooks>} */
  95. const compilationHooksMap = new WeakMap();
  96. /**
  97. * @typedef {object} RealContentHashPluginOptions
  98. * @property {string | Hash} hashFunction the hash function to use
  99. * @property {string=} hashDigest the hash digest to use
  100. */
  101. class RealContentHashPlugin {
  102. /**
  103. * @param {Compilation} compilation the compilation
  104. * @returns {CompilationHooks} the attached hooks
  105. */
  106. static getCompilationHooks(compilation) {
  107. if (!(compilation instanceof Compilation)) {
  108. throw new TypeError(
  109. "The 'compilation' argument must be an instance of Compilation"
  110. );
  111. }
  112. let hooks = compilationHooksMap.get(compilation);
  113. if (hooks === undefined) {
  114. hooks = {
  115. updateHash: new SyncBailHook(["content", "oldHash"])
  116. };
  117. compilationHooksMap.set(compilation, hooks);
  118. }
  119. return hooks;
  120. }
  121. /**
  122. * @param {RealContentHashPluginOptions} options options
  123. */
  124. constructor({ hashFunction, hashDigest }) {
  125. this._hashFunction = hashFunction;
  126. this._hashDigest = hashDigest;
  127. }
  128. /**
  129. * Apply the plugin
  130. * @param {Compiler} compiler the compiler instance
  131. * @returns {void}
  132. */
  133. apply(compiler) {
  134. compiler.hooks.compilation.tap("RealContentHashPlugin", compilation => {
  135. const cacheAnalyse = compilation.getCache(
  136. "RealContentHashPlugin|analyse"
  137. );
  138. const cacheGenerate = compilation.getCache(
  139. "RealContentHashPlugin|generate"
  140. );
  141. const hooks = RealContentHashPlugin.getCompilationHooks(compilation);
  142. compilation.hooks.processAssets.tapPromise(
  143. {
  144. name: "RealContentHashPlugin",
  145. stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_HASH
  146. },
  147. async () => {
  148. const assets = compilation.getAssets();
  149. /** @type {AssetInfoForRealContentHash[]} */
  150. const assetsWithInfo = [];
  151. /** @type {Map<string, [AssetInfoForRealContentHash]>} */
  152. const hashToAssets = new Map();
  153. for (const { source, info, name } of assets) {
  154. const cachedSource = toCachedSource(source);
  155. const content = /** @type {string} */ (cachedSource.source());
  156. /** @type {Hashes} */
  157. const hashes = new Set();
  158. addToList(info.contenthash, hashes);
  159. /** @type {AssetInfoForRealContentHash} */
  160. const data = {
  161. name,
  162. info,
  163. source: cachedSource,
  164. newSource: undefined,
  165. newSourceWithoutOwn: undefined,
  166. content,
  167. ownHashes: undefined,
  168. contentComputePromise: undefined,
  169. contentComputeWithoutOwnPromise: undefined,
  170. referencedHashes: undefined,
  171. hashes
  172. };
  173. assetsWithInfo.push(data);
  174. for (const hash of hashes) {
  175. const list = hashToAssets.get(hash);
  176. if (list === undefined) {
  177. hashToAssets.set(hash, [data]);
  178. } else {
  179. list.push(data);
  180. }
  181. }
  182. }
  183. if (hashToAssets.size === 0) return;
  184. const hashRegExp = new RegExp(
  185. Array.from(hashToAssets.keys(), quoteMeta).join("|"),
  186. "g"
  187. );
  188. await Promise.all(
  189. assetsWithInfo.map(async asset => {
  190. const { name, source, content, hashes } = asset;
  191. if (Buffer.isBuffer(content)) {
  192. asset.referencedHashes = EMPTY_SET;
  193. asset.ownHashes = EMPTY_SET;
  194. return;
  195. }
  196. const etag = cacheAnalyse.mergeEtags(
  197. cacheAnalyse.getLazyHashedEtag(source),
  198. Array.from(hashes).join("|")
  199. );
  200. [asset.referencedHashes, asset.ownHashes] =
  201. await cacheAnalyse.providePromise(name, etag, () => {
  202. const referencedHashes = new Set();
  203. const ownHashes = new Set();
  204. const inContent = content.match(hashRegExp);
  205. if (inContent) {
  206. for (const hash of inContent) {
  207. if (hashes.has(hash)) {
  208. ownHashes.add(hash);
  209. continue;
  210. }
  211. referencedHashes.add(hash);
  212. }
  213. }
  214. return [referencedHashes, ownHashes];
  215. });
  216. })
  217. );
  218. /**
  219. * @param {string} hash the hash
  220. * @returns {undefined | ReferencedHashes} the referenced hashes
  221. */
  222. const getDependencies = hash => {
  223. const assets = hashToAssets.get(hash);
  224. if (!assets) {
  225. const referencingAssets = assetsWithInfo.filter(asset =>
  226. /** @type {ReferencedHashes} */ (asset.referencedHashes).has(
  227. hash
  228. )
  229. );
  230. const err = new WebpackError(`RealContentHashPlugin
  231. Some kind of unexpected caching problem occurred.
  232. An asset was cached with a reference to another asset (${hash}) that's not in the compilation anymore.
  233. Either the asset was incorrectly cached, or the referenced asset should also be restored from cache.
  234. Referenced by:
  235. ${referencingAssets
  236. .map(a => {
  237. const match = new RegExp(`.{0,20}${quoteMeta(hash)}.{0,20}`).exec(
  238. a.content
  239. );
  240. return ` - ${a.name}: ...${match ? match[0] : "???"}...`;
  241. })
  242. .join("\n")}`);
  243. compilation.errors.push(err);
  244. return;
  245. }
  246. const hashes = new Set();
  247. for (const { referencedHashes, ownHashes } of assets) {
  248. if (!(/** @type {OwnHashes} */ (ownHashes).has(hash))) {
  249. for (const hash of /** @type {OwnHashes} */ (ownHashes)) {
  250. hashes.add(hash);
  251. }
  252. }
  253. for (const hash of /** @type {ReferencedHashes} */ (
  254. referencedHashes
  255. )) {
  256. hashes.add(hash);
  257. }
  258. }
  259. return hashes;
  260. };
  261. /**
  262. * @param {string} hash the hash
  263. * @returns {string} the hash info
  264. */
  265. const hashInfo = hash => {
  266. const assets = hashToAssets.get(hash);
  267. return `${hash} (${Array.from(
  268. /** @type {AssetInfoForRealContentHash[]} */ (assets),
  269. a => a.name
  270. )})`;
  271. };
  272. const hashesInOrder = new Set();
  273. for (const hash of hashToAssets.keys()) {
  274. /**
  275. * @param {string} hash the hash
  276. * @param {Set<string>} stack stack of hashes
  277. */
  278. const add = (hash, stack) => {
  279. const deps = getDependencies(hash);
  280. if (!deps) return;
  281. stack.add(hash);
  282. for (const dep of deps) {
  283. if (hashesInOrder.has(dep)) continue;
  284. if (stack.has(dep)) {
  285. throw new Error(
  286. `Circular hash dependency ${Array.from(
  287. stack,
  288. hashInfo
  289. ).join(" -> ")} -> ${hashInfo(dep)}`
  290. );
  291. }
  292. add(dep, stack);
  293. }
  294. hashesInOrder.add(hash);
  295. stack.delete(hash);
  296. };
  297. if (hashesInOrder.has(hash)) continue;
  298. add(hash, new Set());
  299. }
  300. const hashToNewHash = new Map();
  301. /**
  302. * @param {AssetInfoForRealContentHash} asset asset info
  303. * @returns {Etag} etag
  304. */
  305. const getEtag = asset =>
  306. cacheGenerate.mergeEtags(
  307. cacheGenerate.getLazyHashedEtag(asset.source),
  308. Array.from(
  309. /** @type {ReferencedHashes} */ (asset.referencedHashes),
  310. hash => hashToNewHash.get(hash)
  311. ).join("|")
  312. );
  313. /**
  314. * @param {AssetInfoForRealContentHash} asset asset info
  315. * @returns {Promise<void>}
  316. */
  317. const computeNewContent = asset => {
  318. if (asset.contentComputePromise) return asset.contentComputePromise;
  319. return (asset.contentComputePromise = (async () => {
  320. if (
  321. /** @type {OwnHashes} */ (asset.ownHashes).size > 0 ||
  322. Array.from(
  323. /** @type {ReferencedHashes} */
  324. (asset.referencedHashes)
  325. ).some(hash => hashToNewHash.get(hash) !== hash)
  326. ) {
  327. const identifier = asset.name;
  328. const etag = getEtag(asset);
  329. asset.newSource = await cacheGenerate.providePromise(
  330. identifier,
  331. etag,
  332. () => {
  333. const newContent = asset.content.replace(hashRegExp, hash =>
  334. hashToNewHash.get(hash)
  335. );
  336. return new RawSource(newContent);
  337. }
  338. );
  339. }
  340. })());
  341. };
  342. /**
  343. * @param {AssetInfoForRealContentHash} asset asset info
  344. * @returns {Promise<void>}
  345. */
  346. const computeNewContentWithoutOwn = asset => {
  347. if (asset.contentComputeWithoutOwnPromise)
  348. return asset.contentComputeWithoutOwnPromise;
  349. return (asset.contentComputeWithoutOwnPromise = (async () => {
  350. if (
  351. /** @type {OwnHashes} */ (asset.ownHashes).size > 0 ||
  352. Array.from(
  353. /** @type {ReferencedHashes} */
  354. (asset.referencedHashes)
  355. ).some(hash => hashToNewHash.get(hash) !== hash)
  356. ) {
  357. const identifier = `${asset.name}|without-own`;
  358. const etag = getEtag(asset);
  359. asset.newSourceWithoutOwn = await cacheGenerate.providePromise(
  360. identifier,
  361. etag,
  362. () => {
  363. const newContent = asset.content.replace(
  364. hashRegExp,
  365. hash => {
  366. if (
  367. /** @type {OwnHashes} */ (asset.ownHashes).has(hash)
  368. ) {
  369. return "";
  370. }
  371. return hashToNewHash.get(hash);
  372. }
  373. );
  374. return new RawSource(newContent);
  375. }
  376. );
  377. }
  378. })());
  379. };
  380. const comparator = compareSelect(a => a.name, compareStrings);
  381. for (const oldHash of hashesInOrder) {
  382. const assets =
  383. /** @type {AssetInfoForRealContentHash[]} */
  384. (hashToAssets.get(oldHash));
  385. assets.sort(comparator);
  386. await Promise.all(
  387. assets.map(asset =>
  388. /** @type {OwnHashes} */ (asset.ownHashes).has(oldHash)
  389. ? computeNewContentWithoutOwn(asset)
  390. : computeNewContent(asset)
  391. )
  392. );
  393. const assetsContent = mapAndDeduplicateBuffers(assets, asset => {
  394. if (/** @type {OwnHashes} */ (asset.ownHashes).has(oldHash)) {
  395. return asset.newSourceWithoutOwn
  396. ? asset.newSourceWithoutOwn.buffer()
  397. : asset.source.buffer();
  398. }
  399. return asset.newSource
  400. ? asset.newSource.buffer()
  401. : asset.source.buffer();
  402. });
  403. let newHash = hooks.updateHash.call(assetsContent, oldHash);
  404. if (!newHash) {
  405. const hash = createHash(this._hashFunction);
  406. if (compilation.outputOptions.hashSalt) {
  407. hash.update(compilation.outputOptions.hashSalt);
  408. }
  409. for (const content of assetsContent) {
  410. hash.update(content);
  411. }
  412. const digest = hash.digest(this._hashDigest);
  413. newHash = /** @type {string} */ (digest.slice(0, oldHash.length));
  414. }
  415. hashToNewHash.set(oldHash, newHash);
  416. }
  417. await Promise.all(
  418. assetsWithInfo.map(async asset => {
  419. await computeNewContent(asset);
  420. const newName = asset.name.replace(hashRegExp, hash =>
  421. hashToNewHash.get(hash)
  422. );
  423. const infoUpdate = {};
  424. const hash = asset.info.contenthash;
  425. infoUpdate.contenthash = Array.isArray(hash)
  426. ? hash.map(hash => hashToNewHash.get(hash))
  427. : hashToNewHash.get(hash);
  428. if (asset.newSource !== undefined) {
  429. compilation.updateAsset(
  430. asset.name,
  431. asset.newSource,
  432. infoUpdate
  433. );
  434. } else {
  435. compilation.updateAsset(asset.name, asset.source, infoUpdate);
  436. }
  437. if (asset.name !== newName) {
  438. compilation.renameAsset(asset.name, newName);
  439. }
  440. })
  441. );
  442. }
  443. );
  444. });
  445. }
  446. }
  447. module.exports = RealContentHashPlugin;