/ webpack_config / plugins / sortCache.js
sortCache.js
 1  // Makes for a deterministic cache file by sorting it
 2  'use strict';
 3  const fs = require('fs');
 4  const klawSync = require('klaw-sync');
 5  
 6  const CACHE_FILE_REGEX = /.*icons-[a-z0-9]*\/\.cache$/;
 7  const findCacheFile = item => CACHE_FILE_REGEX.test(item.path);
 8  
 9  function SortCachePlugin() {};
10  SortCachePlugin.prototype.apply = function(compiler) {
11    compiler.plugin('done', (stats) => {
12      const buildDir = stats.compilation.compiler.outputPath;
13      const cacheFilePaths = klawSync(buildDir, { filter: findCacheFile });
14  
15      if (!cacheFilePaths.length) {
16        throw new Error('Could not find .cache file');
17      }
18      if (cacheFilePaths.length > 1) {
19        throw new Error('More than one possible .cache file detected');
20      }
21  
22      const cacheFilePath = cacheFilePaths[0].path;
23      const rawCacheFile = fs.readFileSync(cacheFilePath, 'utf8');
24      const cache = JSON.parse(rawCacheFile);
25  
26      cache.result.files = cache.result.files.sort();
27  
28      fs.writeFileSync(cacheFilePath, JSON.stringify(cache), 'utf8');
29    });
30  };
31  
32  module.exports = SortCachePlugin;