createCacheKeyFunction.js 1.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
/**
 * Copyright (c) 2013-present, Facebook, Inc.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

'use strict';

const crypto = require('crypto');
const fs = require('fs');
const path = require('path');

function getGlobalCacheKey(files, values) {
  const presetVersion = require('../package').dependencies['babel-preset-fbjs'];

  const chunks = [
    process.env.NODE_ENV,
    process.env.BABEL_ENV,
    presetVersion,
    ...values,
    ...files.map(file => fs.readFileSync(file)),
  ];

  return chunks
    .reduce(
      (hash, chunk) => hash.update('\0', 'utf-8').update(chunk || ''),
      crypto.createHash('md5')
    )
    .digest('hex');
}

function getCacheKeyFunction(globalCacheKey) {
  return (src, file, configString, options) => {
    const {instrument, config} = options;
    const rootDir = config && config.rootDir;

    return crypto
      .createHash('md5')
      .update(globalCacheKey)
      .update('\0', 'utf8')
      .update(src)
      .update('\0', 'utf8')
      .update(rootDir ? path.relative(config.rootDir, file) : '')
      .update('\0', 'utf8')
      .update(instrument ? 'instrument' : '')
      .digest('hex');
  };
}

module.exports = (files = [], values = []) => {
  return getCacheKeyFunction(getGlobalCacheKey(files, values));
};