-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
index.js
138 lines (127 loc) · 4.27 KB
/
index.js
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
132
133
134
135
136
137
138
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import type {Path, ProjectConfig} from 'types/Config';
import type {CacheKeyOptions, TransformOptions} from 'types/Transform';
import crypto from 'crypto';
import fs from 'fs';
import path from 'path';
import jestPreset from 'babel-preset-jest';
import {transform as babelTransform, util as babelUtil} from 'babel-core';
import babelIstanbulPlugin from 'babel-plugin-istanbul';
const BABELRC_FILENAME = '.babelrc';
const BABELRC_JS_FILENAME = '.babelrc.js';
const BABEL_CONFIG_KEY = 'babel';
const PACKAGE_JSON = 'package.json';
const THIS_FILE = fs.readFileSync(__filename);
const createTransformer = (options: any) => {
const cache = Object.create(null);
const getBabelRC = filename => {
const paths = [];
let directory = filename;
while (directory !== (directory = path.dirname(directory))) {
if (cache[directory]) {
break;
}
paths.push(directory);
const configFilePath = path.join(directory, BABELRC_FILENAME);
if (fs.existsSync(configFilePath)) {
cache[directory] = fs.readFileSync(configFilePath, 'utf8');
break;
}
const configJsFilePath = path.join(directory, BABELRC_JS_FILENAME);
if (fs.existsSync(configJsFilePath)) {
// $FlowFixMe
cache[directory] = JSON.stringify(require(configJsFilePath));
break;
}
const resolvedJsonFilePath = path.join(directory, PACKAGE_JSON);
const packageJsonFilePath =
resolvedJsonFilePath === PACKAGE_JSON
? path.resolve(directory, PACKAGE_JSON)
: resolvedJsonFilePath;
if (fs.existsSync(packageJsonFilePath)) {
// $FlowFixMe
const packageJsonFileContents = require(packageJsonFilePath);
if (packageJsonFileContents[BABEL_CONFIG_KEY]) {
cache[directory] = JSON.stringify(
packageJsonFileContents[BABEL_CONFIG_KEY],
);
break;
}
}
}
paths.forEach(directoryPath => (cache[directoryPath] = cache[directory]));
return cache[directory] || '';
};
options = Object.assign({}, options, {
plugins: (options && options.plugins) || [],
presets: ((options && options.presets) || []).concat([jestPreset]),
retainLines: true,
sourceMaps: 'inline',
});
delete options.cacheDirectory;
delete options.filename;
return {
canInstrument: true,
getCacheKey(
fileData: string,
filename: Path,
configString: string,
{instrument, rootDir}: CacheKeyOptions,
): string {
return crypto
.createHash('md5')
.update(THIS_FILE)
.update('\0', 'utf8')
.update(fileData)
.update('\0', 'utf8')
.update(path.relative(rootDir, filename))
.update('\0', 'utf8')
.update(configString)
.update('\0', 'utf8')
.update(getBabelRC(filename))
.update('\0', 'utf8')
.update(instrument ? 'instrument' : '')
.digest('hex');
},
process(
src: string,
filename: Path,
config: ProjectConfig,
transformOptions: TransformOptions,
): string {
const altExts = config.moduleFileExtensions.map(
extension => '.' + extension,
);
if (babelUtil && !babelUtil.canCompile(filename, altExts)) {
return src;
}
const theseOptions = Object.assign({filename}, options);
if (transformOptions && transformOptions.instrument) {
theseOptions.auxiliaryCommentBefore = ' istanbul ignore next ';
// Copied from jest-runtime transform.js
theseOptions.plugins = theseOptions.plugins.concat([
[
babelIstanbulPlugin,
{
// files outside `cwd` will not be instrumented
cwd: config.rootDir,
exclude: [],
},
],
]);
}
// babel v7 might return null in the case when the file has been ignored.
const transformResult = babelTransform(src, theseOptions);
return transformResult ? transformResult.code : src;
},
};
};
module.exports = createTransformer();
(module.exports: any).createTransformer = createTransformer;