-
-
Notifications
You must be signed in to change notification settings - Fork 147
/
index.ts
58 lines (53 loc) · 1.74 KB
/
index.ts
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
54
55
56
57
58
import { IOptionalPreprocessOptions, preprocess, preprocessOptions } from '@aurelia/plugin-conventions';
import {
canInstrument as tsCanInstrument,
getCacheKey as tsGetCacheKey,
process as tsProcess
} from 'ts-jest';
import { Config } from '@jest/types';
import { TransformOptions, TransformedSource, CacheKeyOptions } from '@jest/transform';
import * as path from 'path';
function createTransformer(conventionsOptions = {}) {
const au2Options = preprocessOptions(conventionsOptions);
function getCacheKey(
fileData: string,
filePath: Config.Path,
configStr: string,
options: CacheKeyOptions
): string {
const tsKey = tsGetCacheKey!(fileData, filePath, configStr, options);
return `${tsKey}:${JSON.stringify(au2Options)}`;
}
// Wrap ts-jest process
function process(
sourceText: string,
sourcePath: Config.Path,
config: Config.ProjectConfig,
transformOptions?: TransformOptions
): TransformedSource {
try {
const result = preprocess(
{ path: sourcePath, contents: sourceText },
au2Options
);
let newSourcePath = sourcePath;
if (result !== undefined) {
if (au2Options.templateExtensions.includes(path.extname(sourcePath))) {
// Rewrite foo.html to foo.html.ts, or foo.md to foo.md.ts
newSourcePath += '.ts';
}
return tsProcess(result.code, newSourcePath, config, transformOptions);
}
} catch (e) {
// ignore
}
return tsProcess(sourceText, sourcePath, config, transformOptions);
}
return {
canInstrument: tsCanInstrument,
getCacheKey,
process
};
}
const { canInstrument, getCacheKey, process } = createTransformer();
export { canInstrument, getCacheKey, process, createTransformer };