UNPKG

1.35 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2013-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8'use strict';
9
10const crypto = require('crypto');
11const fs = require('fs');
12const path = require('path');
13
14function getGlobalCacheKey(files, values) {
15 const presetVersion = require('../package').dependencies['babel-preset-fbjs'];
16
17 const chunks = [
18 process.env.NODE_ENV,
19 process.env.BABEL_ENV,
20 presetVersion,
21 ...values,
22 ...files.map(file => fs.readFileSync(file)),
23 ];
24
25 return chunks
26 .reduce(
27 (hash, chunk) => hash.update('\0', 'utf-8').update(chunk || ''),
28 crypto.createHash('md5')
29 )
30 .digest('hex');
31}
32
33function getCacheKeyFunction(globalCacheKey) {
34 return (src, file, configString, options) => {
35 const {instrument, config} = options;
36 const rootDir = config && config.rootDir;
37
38 return crypto
39 .createHash('md5')
40 .update(globalCacheKey)
41 .update('\0', 'utf8')
42 .update(src)
43 .update('\0', 'utf8')
44 .update(rootDir ? path.relative(config.rootDir, file) : '')
45 .update('\0', 'utf8')
46 .update(instrument ? 'instrument' : '')
47 .digest('hex');
48 };
49}
50
51module.exports = (files = [], values = []) => {
52 return getCacheKeyFunction(getGlobalCacheKey(files, values));
53};