1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 |
|
10 | const crypto = require('crypto');
|
11 | const fs = require('fs');
|
12 | const path = require('path');
|
13 |
|
14 | function 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 |
|
33 | function 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 |
|
51 | module.exports = (files = [], values = []) => {
|
52 | return getCacheKeyFunction(getGlobalCacheKey(files, values));
|
53 | };
|