Skip to content
This repository was archived by the owner on Jan 26, 2019. It is now read-only.

Jest cache busting for ts/tsx files #79

Merged
merged 1 commit into from
Jun 23, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions packages/react-scripts/config/jest/typescriptTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
'use strict';

const fs = require('fs');
const crypto = require('crypto');
const tsc = require('typescript');
const tsconfigPath = require('app-root-path').resolve('/tsconfig.json');
const THIS_FILE = fs.readFileSync(__filename);

let compilerConfig = {
module: tsc.ModuleKind.CommonJS,
jsx: tsc.JsxEmit.React,
}
};

if (fs.existsSync(tsconfigPath)) {
try {
Expand All @@ -18,18 +20,32 @@ if (fs.existsSync(tsconfigPath)) {
if (tsconfig && tsconfig.compilerOptions) {
compilerConfig = tsconfig.compilerOptions;
}
} catch (e) { /* Do nothing - default is set */ }
} catch (e) {
/* Do nothing - default is set */
}
}

module.exports = {
process(src, path) {
if (path.endsWith('.ts') || path.endsWith('.tsx')) {
return tsc.transpile(
src,
compilerConfig,
path, []
);
return tsc.transpile(src, compilerConfig, path, []);
}
return src;
},
getCacheKey(fileData, filePath, configStr, options) {
return crypto
.createHash('md5')
.update(THIS_FILE)
.update('\0', 'utf8')
.update(fileData)
.update('\0', 'utf8')
.update(filePath)
.update('\0', 'utf8')
.update(configStr)
.update('\0', 'utf8')
.update(JSON.stringify(compilerConfig))
.update('\0', 'utf8')
.update(options.instrument ? 'instrument' : '')
.digest('hex');
},
};