defaultConfigHash.js 842 B

123456789101112131415161718192021222324252627282930
  1. const crypto = require('crypto');
  2. const path = require('path');
  3. const nodeObjectHash = require('node-object-hash');
  4. const sort = nodeObjectHash({
  5. sort: false,
  6. }).sort;
  7. function relateContextToCacheDir(config) {
  8. const hardSourcePlugin = config.plugins.find(
  9. ({ constructor }) => constructor.name === 'HardSourceWebpackPlugin',
  10. );
  11. const cacheDir = hardSourcePlugin.getCachePath();
  12. const context = path.resolve(process.cwd(), config.context);
  13. const clone = Object.assign({}, config, {
  14. context: path.relative(cacheDir, context),
  15. });
  16. const sorted = sort(clone)
  17. .replace(new RegExp(`${context}[^,\\]}]*`, 'g'), match =>
  18. path.relative(cacheDir, match),
  19. )
  20. .replace(/\\/g, '/');
  21. return crypto
  22. .createHash('sha256')
  23. .update(sorted)
  24. .digest('hex');
  25. }
  26. module.exports = relateContextToCacheDir;