index.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. const fs = require('graceful-fs');
  2. const path = require('path');
  3. const logMessages = require('./log-messages');
  4. exports.cachePrefix = cachePrefix;
  5. const NS = fs.realpathSync(path.dirname(__dirname));
  6. const cachePrefixNS = `${NS}/cachePrefix`;
  7. function cachePrefix(compilation) {
  8. if (typeof compilation[cachePrefixNS] === 'undefined') {
  9. let prefix = '';
  10. let nextCompilation = compilation;
  11. while (nextCompilation.compiler.parentCompilation) {
  12. const parentCompilation = nextCompilation.compiler.parentCompilation;
  13. if (!nextCompilation.cache) {
  14. logMessages.childCompilerWithoutCache(compilation);
  15. prefix = null;
  16. break;
  17. }
  18. const cache = nextCompilation.cache;
  19. let parentCache = parentCompilation.cache;
  20. if (cache === parentCache) {
  21. nextCompilation = parentCompilation;
  22. continue;
  23. }
  24. let cacheKey;
  25. for (var key in parentCache) {
  26. if (key && parentCache[key] === cache) {
  27. cacheKey = key;
  28. break;
  29. }
  30. }
  31. // webpack 3 adds the children member containing compiler names paired
  32. // with arrays of compilation caches, one for each compilation sharing the
  33. // same name.
  34. if (!cacheKey && parentCache.children) {
  35. parentCache = parentCache.children;
  36. for (var key in parentCache) {
  37. if (key && parentCache[key]) {
  38. for (const index in parentCache[key]) {
  39. if (parentCache[key][index] === cache) {
  40. cacheKey = `${key}.${index}`;
  41. break;
  42. }
  43. if (
  44. parentCache[key][index] &&
  45. typeof parentCache[key][index] === 'object'
  46. ) {
  47. for (const subkey in parentCache[key][index]) {
  48. if (parentCache[key][index][subkey] === cache) {
  49. cacheKey = `${key}.${index}.${subkey}`;
  50. break;
  51. }
  52. }
  53. }
  54. }
  55. }
  56. }
  57. }
  58. if (!cacheKey) {
  59. logMessages.childCompilerUnnamedCache(compilation);
  60. prefix = null;
  61. break;
  62. } else {
  63. prefix = cacheKey + prefix;
  64. }
  65. nextCompilation = parentCompilation;
  66. }
  67. compilation[cachePrefixNS] =
  68. prefix !== null
  69. ? require('crypto')
  70. .createHash('md5')
  71. .update(prefix)
  72. .digest('base64')
  73. : null;
  74. }
  75. return compilation[cachePrefixNS];
  76. }