-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
131 lines (111 loc) · 3.29 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import * as babel from "@babel/core";
import {
AsyncTransformer,
SyncTransformer,
TransformOptions,
TransformedSource,
Transformer,
TransformerCreator,
} from "@jest/transform";
import * as esbuild from "esbuild";
import { globsToMatcher } from "jest-util";
import { extname } from "path";
export interface TransformerConfig {
format?: "esm" | "cjs";
hoistMatch?: Array<string>;
}
export type EsbuildTransformOptions = TransformOptions<TransformerConfig>;
const nodeMajorVersion = process.version.match(/^v(\d+)\./)?.[1];
const esbuildOptions: esbuild.TransformOptions = {
sourcemap: "inline",
legalComments: "inline",
platform: "node",
target: nodeMajorVersion ? `node${nodeMajorVersion}` : undefined,
};
const babelOptions: babel.TransformOptions = {
plugins: ["jest-hoist"],
sourceMaps: "inline",
configFile: false,
};
const loaders: Record<string, esbuild.Loader> = {
".ts": "ts",
".tsx": "tsx",
".js": "js",
".cjs": "js",
".mjs": "js",
};
const getLoader = (path: string): esbuild.Loader =>
loaders[extname(path)] || "default";
const handleResult = (
esbuildResult: esbuild.TransformResult,
babelResult: babel.BabelFileResult | null | undefined
): TransformedSource => {
let result: TransformedSource;
if (babelResult === undefined) {
result = {
code: esbuildResult.code,
map: esbuildResult.map,
};
} else if (
babelResult === null ||
babelResult.code === null ||
babelResult.code === undefined
) {
throw new Error(`babel transform returned empty result`);
} else {
result = {
code: babelResult.code,
map: babelResult.map,
};
}
return {
code: result.code.replace(/\/\*!(\s*istanbul ignore .*?)\*\//, "/* $1*/"),
map: result.map,
};
};
const matcher = (path: string, options: EsbuildTransformOptions): boolean =>
globsToMatcher(
options?.transformerConfig?.hoistMatch || options.config.testMatch
)(path);
const createTransformer: TransformerCreator<
Transformer<TransformerConfig>,
TransformerConfig
> = (): SyncTransformer<TransformerConfig> &
AsyncTransformer<TransformerConfig> => {
return {
process(source: string, path: string, options: EsbuildTransformOptions) {
const esbuildResult = esbuild.transformSync(source, {
...esbuildOptions,
sourcefile: path,
format: options?.transformerConfig?.format || "cjs",
loader: getLoader(path),
});
let babelResult: babel.BabelFileResult | null | undefined;
if (matcher(path, options)) {
babelResult = babel.transformSync(esbuildResult.code, babelOptions);
}
return handleResult(esbuildResult, babelResult);
},
async processAsync(
source: string,
path: string,
options: EsbuildTransformOptions
) {
const esbuildResult = await esbuild.transform(source, {
...esbuildOptions,
sourcefile: path,
format: options?.transformerConfig?.format || "esm",
loader: getLoader(path),
});
let babelResult: babel.BabelFileResult | null | undefined;
if (matcher(path, options)) {
babelResult = await babel.transformAsync(
esbuildResult.code,
babelOptions
);
}
return handleResult(esbuildResult, babelResult);
},
};
};
export default { createTransformer };