-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbabel-jest.js
102 lines (89 loc) · 3 KB
/
babel-jest.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
/* eslint-disable */
/**
* 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
*/
const crypto = require('crypto')
const fs = require('fs')
const path = require('path')
const jestPreset = require('babel-preset-jest')
const {transform: babelTransform, util: babelUtil} = require('@babel/core')
const babelIstanbulPlugin = require('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 babelrc = fs.readFileSync(path.join(__dirname, '.babelrc'), 'utf8')
const createTransformer = options => {
const cache = Object.create(null)
options = Object.assign({}, options, {
plugins: (options && options.plugins) || [],
presets: ((options && options.presets) || []).concat([jestPreset]),
sourceMaps: 'both',
})
delete options.cacheDirectory
delete options.filename
return {
canInstrument: true,
getCacheKey(fileData, filename, configString, {instrument, rootDir}) {
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(babelrc)
.update('\0', 'utf8')
.update(instrument ? 'instrument' : '')
.digest('hex')
},
process(src, filename, config, transformOptions) {
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)
if (!transformResult) {
return src
}
const shouldReturnCodeOnly =
transformOptions == null ||
transformOptions.returnSourceString == null ||
transformOptions.returnSourceString === true
return shouldReturnCodeOnly ? transformResult.code : transformResult
},
}
}
const options = {
babelrc: false,
plugins: JSON.parse(babelrc).plugins,
presets: JSON.parse(babelrc).presets,
}
module.exports = createTransformer(options)