Skip to content

Commit 0618b12

Browse files
committed
Jest cache busting for ts/tsx files
1 parent 1dcfa1d commit 0618b12

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

packages/react-scripts/config/jest/typescriptTransform.js

+23-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
'use strict';
44

55
const fs = require('fs');
6+
const crypto = require('crypto');
67
const tsc = require('typescript');
78
const tsconfigPath = require('app-root-path').resolve('/tsconfig.json');
9+
const THIS_FILE = fs.readFileSync(__filename);
810

911
let compilerConfig = {
1012
module: tsc.ModuleKind.CommonJS,
1113
jsx: tsc.JsxEmit.React,
12-
}
14+
};
1315

1416
if (fs.existsSync(tsconfigPath)) {
1517
try {
@@ -18,18 +20,32 @@ if (fs.existsSync(tsconfigPath)) {
1820
if (tsconfig && tsconfig.compilerOptions) {
1921
compilerConfig = tsconfig.compilerOptions;
2022
}
21-
} catch (e) { /* Do nothing - default is set */ }
23+
} catch (e) {
24+
/* Do nothing - default is set */
25+
}
2226
}
2327

2428
module.exports = {
2529
process(src, path) {
2630
if (path.endsWith('.ts') || path.endsWith('.tsx')) {
27-
return tsc.transpile(
28-
src,
29-
compilerConfig,
30-
path, []
31-
);
31+
return tsc.transpile(src, compilerConfig, path, []);
3232
}
3333
return src;
3434
},
35+
getCacheKey(fileData, filePath, configStr, options) {
36+
return crypto
37+
.createHash('md5')
38+
.update(THIS_FILE)
39+
.update('\0', 'utf8')
40+
.update(fileData)
41+
.update('\0', 'utf8')
42+
.update(filePath)
43+
.update('\0', 'utf8')
44+
.update(configStr)
45+
.update('\0', 'utf8')
46+
.update(JSON.stringify(compilerConfig))
47+
.update('\0', 'utf8')
48+
.update(options.instrument ? 'instrument' : '')
49+
.digest('hex');
50+
},
3551
};

0 commit comments

Comments
 (0)