UNPKG

1.75 kBJavaScriptView Raw
1const crypto = require("crypto");
2const fs = require("fs");
3const jestPreset = require("babel-preset-jest");
4const babel = require("@babel/core");
5const babelIstanbulPlugin = require("babel-plugin-istanbul");
6
7const THIS_FILE = fs.readFileSync(__filename);
8
9module.exports = {
10 canInstrument: true,
11 getCacheKey(fileData, filename, configString, { instrument }) {
12 return crypto
13 .createHash("md5")
14 .update(THIS_FILE)
15 .update("\0", "utf8")
16 .update(fileData)
17 .update("\0", "utf8")
18 .update(configString)
19 .update("\0", "utf8")
20 .update(filename)
21 .update("\0", "utf8")
22 .update(instrument ? "instrument" : "")
23 .digest("hex");
24 },
25 process(src, filename, config, transformOptions) {
26 if (babel.util && !babel.util.canCompile(filename)) {
27 return src;
28 }
29
30 const options = {
31 ...config.globals.BABEL_OPTIONS,
32 babelrc: false,
33 plugins:
34 (config.globals.BABEL_OPTIONS &&
35 config.globals.BABEL_OPTIONS.plugins) ||
36 [],
37 presets: (
38 (config.globals.BABEL_OPTIONS &&
39 config.globals.BABEL_OPTIONS.presets) ||
40 []
41 ).concat([jestPreset]),
42 retainLines: true,
43 sourceMaps: "inline",
44 filename
45 };
46
47 if (transformOptions && transformOptions.instrument) {
48 options.auxiliaryCommentBefore = " istanbul ignore next ";
49 // Copied from jest-runtime transform.js
50 options.plugins = options.plugins.concat([
51 [
52 babelIstanbulPlugin.default,
53 {
54 // files outside `cwd` will not be instrumented
55 cwd: config.rootDir,
56 exclude: []
57 }
58 ]
59 ]);
60 }
61
62 return babel.transform(src, options).code;
63 }
64};